Search in sources :

Example 1 with BinaryOperator

use of java.util.function.BinaryOperator in project ignite by apache.

the class CacheUtils method sparseFold.

/**
 * Sparse version of fold. This method also applicable to sparse zeroes.
 *
 * @param cacheName Cache name.
 * @param folder Folder.
 * @param keyFilter Key filter.
 * @param accumulator Accumulator.
 * @param zeroValSupp Zero value supplier.
 * @param defVal Default value.
 * @param defKey Default key.
 * @param defValCnt Def value count.
 * @param isNilpotent Is nilpotent.
 */
private static <K, V, A> A sparseFold(String cacheName, IgniteBiFunction<Cache.Entry<K, V>, A, A> folder, IgnitePredicate<K> keyFilter, BinaryOperator<A> accumulator, IgniteSupplier<A> zeroValSupp, V defVal, K defKey, long defValCnt, boolean isNilpotent) {
    A defRes = zeroValSupp.get();
    if (!isNilpotent)
        for (int i = 0; i < defValCnt; i++) defRes = folder.apply(new CacheEntryImpl<>(defKey, defVal), defRes);
    Collection<A> totalRes = bcast(cacheName, () -> {
        Ignite ignite = Ignition.localIgnite();
        IgniteCache<K, V> cache = ignite.getOrCreateCache(cacheName);
        int partsCnt = ignite.affinity(cacheName).partitions();
        // Use affinity in filter for ScanQuery. Otherwise we accept consumer in each node which is wrong.
        Affinity affinity = ignite.affinity(cacheName);
        ClusterNode locNode = ignite.cluster().localNode();
        A a = zeroValSupp.get();
        // Iterate over all partitions. Some of them will be stored on that local node.
        for (int part = 0; part < partsCnt; part++) {
            int p = part;
            // Query returns an empty cursor if this partition is not stored on this node.
            for (Cache.Entry<K, V> entry : cache.query(new ScanQuery<K, V>(part, (k, v) -> affinity.mapPartitionToNode(p) == locNode && (keyFilter == null || keyFilter.apply(k))))) a = folder.apply(entry, a);
        }
        return a;
    });
    return totalRes.stream().reduce(defRes, accumulator);
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) IgniteConsumer(org.apache.ignite.ml.math.functions.IgniteConsumer) IgniteFunction(org.apache.ignite.ml.math.functions.IgniteFunction) Affinity(org.apache.ignite.cache.affinity.Affinity) IgniteCallable(org.apache.ignite.lang.IgniteCallable) MatrixBlockEntry(org.apache.ignite.ml.math.impls.matrix.MatrixBlockEntry) RowColMatrixKey(org.apache.ignite.ml.math.distributed.keys.RowColMatrixKey) DataStructureCacheKey(org.apache.ignite.ml.math.distributed.keys.DataStructureCacheKey) ClusterNode(org.apache.ignite.cluster.ClusterNode) MatrixBlockKey(org.apache.ignite.ml.math.distributed.keys.impl.MatrixBlockKey) VectorBlockEntry(org.apache.ignite.ml.math.impls.vector.VectorBlockEntry) IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) Map(java.util.Map) Cache(javax.cache.Cache) IgniteBinaryOperator(org.apache.ignite.ml.math.functions.IgniteBinaryOperator) UnsupportedOperationException(org.apache.ignite.ml.math.exceptions.UnsupportedOperationException) ClusterGroup(org.apache.ignite.cluster.ClusterGroup) KeyMapper(org.apache.ignite.ml.math.KeyMapper) CacheEntryImpl(org.apache.ignite.internal.processors.cache.CacheEntryImpl) VectorBlockKey(org.apache.ignite.ml.math.distributed.keys.impl.VectorBlockKey) A(org.apache.ignite.internal.util.typedef.internal.A) IgniteSupplier(org.apache.ignite.ml.math.functions.IgniteSupplier) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) IgniteTriFunction(org.apache.ignite.ml.math.functions.IgniteTriFunction) Set(java.util.Set) IgniteRunnable(org.apache.ignite.lang.IgniteRunnable) UUID(java.util.UUID) Ignite(org.apache.ignite.Ignite) BinaryOperator(java.util.function.BinaryOperator) IgniteCache(org.apache.ignite.IgniteCache) IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) Objects(java.util.Objects) Stream(java.util.stream.Stream) Ignition(org.apache.ignite.Ignition) IgniteBiFunction(org.apache.ignite.ml.math.functions.IgniteBiFunction) IgniteDoubleFunction(org.apache.ignite.ml.math.functions.IgniteDoubleFunction) Collections(java.util.Collections) ScanQuery(org.apache.ignite.cache.query.ScanQuery) A(org.apache.ignite.internal.util.typedef.internal.A) CacheEntryImpl(org.apache.ignite.internal.processors.cache.CacheEntryImpl) Affinity(org.apache.ignite.cache.affinity.Affinity) Ignite(org.apache.ignite.Ignite) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache)

Example 2 with BinaryOperator

use of java.util.function.BinaryOperator in project JDK8 by ABHSY.

the class MethodReference method test2.

@Test
public void test2() {
    System.out.println(sortList1().toString());
    System.out.println(sortList2().toString());
    System.out.println(sortList3().toString());
    BinaryOperator<Integer> tComparator = (x, y) -> x + y;
    Runnable runnable = () -> System.out.print(1);
    ActionListener tConsumer = event -> System.out.print(1);
    Comparator<Integer> tComparator1 = (x, y) -> x + y;
    String name = getName();
    Runnable runnable1 = () -> System.out.print(name);
    runnable1.run();
}
Also used : Consumer(java.util.function.Consumer) Arrays(java.util.Arrays) List(java.util.List) ActionListener(java.awt.event.ActionListener) Test(org.junit.Test) Comparator(java.util.Comparator) Supplier(java.util.function.Supplier) BinaryOperator(java.util.function.BinaryOperator) ActionListener(java.awt.event.ActionListener) Test(org.junit.Test)

Example 3 with BinaryOperator

use of java.util.function.BinaryOperator in project halyard by spinnaker.

the class DaemonTaskHandler method reduceChildren.

public static <U, T> DaemonResponse<U> reduceChildren(U base, BiFunction<U, ? super T, U> accumulator, BinaryOperator<U> combiner) {
    DaemonTask task = getTask();
    if (task != null) {
        U responseBody = base;
        ProblemSet problemSet = new ProblemSet();
        DaemonResponse<U> response = new DaemonResponse<>(responseBody, problemSet);
        return (DaemonResponse) task.getChildren().stream().reduce(response, (o, t) -> {
            DaemonResponse<U> collector = (DaemonResponse<U>) o;
            DaemonTask child = (DaemonTask) t;
            DaemonResponse<T> childResponse;
            try {
                childResponse = task.reapChild(child);
            } catch (InterruptedException e) {
                throw new DaemonTaskInterrupted("Interrupted during reap", e);
            }
            DaemonTask.State state = child.getState();
            if (!state.isTerminal()) {
                throw new IllegalStateException("Child task " + child + " reaped but non-terminal.");
            }
            switch(state) {
                case FAILED:
                    throw new HalException(childResponse.getProblemSet().getProblems());
                case INTERRUPTED:
                    task.interrupt();
                    throw new DaemonTaskInterrupted(child.getFatalError());
                case TIMED_OUT:
                    task.timeout();
                    throw new DaemonTaskInterrupted("Child task timed out");
                case SUCCEEDED:
                    break;
                default:
                    throw new IllegalStateException("Unknown terminal state " + state);
            }
            collector.getProblemSet().addAll(childResponse.getProblemSet());
            collector.setResponseBody(accumulator.apply(collector.getResponseBody(), childResponse.getResponseBody()));
            return collector;
        }, (Object o1, Object o2) -> {
            DaemonResponse<U> r1 = (DaemonResponse<U>) o1;
            DaemonResponse<U> r2 = (DaemonResponse<U>) o2;
            r1.setResponseBody(combiner.apply(r1.getResponseBody(), r2.getResponseBody()));
            r1.getProblemSet().addAll(r2.getProblemSet());
            return r1;
        });
    } else {
        throw new IllegalStateException("You must be running a DaemonTask to reduce child tasks");
    }
}
Also used : Slf4j(lombok.extern.slf4j.Slf4j) JobExecutor(com.netflix.spinnaker.halyard.core.job.v1.JobExecutor) BiFunction(java.util.function.BiFunction) DaemonResponse(com.netflix.spinnaker.halyard.core.DaemonResponse) HalException(com.netflix.spinnaker.halyard.core.error.v1.HalException) ProblemSet(com.netflix.spinnaker.halyard.core.problem.v1.ProblemSet) DaemonLocalJobExecutor(com.netflix.spinnaker.halyard.core.job.v1.DaemonLocalJobExecutor) Supplier(java.util.function.Supplier) BinaryOperator(java.util.function.BinaryOperator) DaemonResponse(com.netflix.spinnaker.halyard.core.DaemonResponse) HalException(com.netflix.spinnaker.halyard.core.error.v1.HalException) ProblemSet(com.netflix.spinnaker.halyard.core.problem.v1.ProblemSet)

Example 4 with BinaryOperator

use of java.util.function.BinaryOperator in project sw360portal by sw360.

the class XhtmlGeneratorTest method findLicenses.

private String findLicenses(Document document, String releaseNameString) {
    List list = document.selectNodes("//*[local-name()='li'][@id='" + releaseNameString + "']/*[local-name()='ul'][@class='licenseEntries']");
    StringBuffer result = new StringBuffer();
    for (Iterator iter = list.iterator(); iter.hasNext(); ) {
        Element element = (Element) iter.next();
        for (Object liObject : element.content()) {
            Element liElement = (Element) liObject;
            String licenseEntryId = liElement.attribute("id").getValue();
            String licenseTextId = licenseEntryId.replace("licenseEntry", "licenseText");
            List licenseTexts = document.selectNodes("//*[local-name()='pre'][@id='" + licenseTextId + "']/text()");
            Object licenseText = licenseTexts.stream().map(l -> ((Text) l).getStringValue()).reduce("", (BinaryOperator<String>) (l1, l2) -> (String) (l1 + l2));
            result.append(((String) licenseText).trim());
            result.append("\n");
        }
    }
    return result.toString();
}
Also used : InputSource(org.xml.sax.InputSource) Document(org.dom4j.Document) java.util(java.util) BeforeClass(org.junit.BeforeClass) Text(org.dom4j.Text) Test(org.junit.Test) SAXReader(org.dom4j.io.SAXReader) BinaryOperator(java.util.function.BinaryOperator) StringReader(java.io.StringReader) org.eclipse.sw360.datahandler.thrift.licenseinfo(org.eclipse.sw360.datahandler.thrift.licenseinfo) Element(org.dom4j.Element) Matchers.is(org.hamcrest.Matchers.is) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Matchers.containsString(org.hamcrest.Matchers.containsString) EntityResolver(org.xml.sax.EntityResolver) InputStream(java.io.InputStream) Element(org.dom4j.Element) Text(org.dom4j.Text) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 5 with BinaryOperator

use of java.util.function.BinaryOperator in project Gaffer by gchq.

the class ElementAggregatorTest method shouldAggregateWithTuple2BinaryOperator.

@Test
public void shouldAggregateWithTuple2BinaryOperator() {
    // Given
    final String property1 = "property 1";
    final String property2 = "property 2";
    final BinaryOperator func1 = new ExampleTuple2BinaryOperator();
    final ElementAggregator aggregator = new ElementAggregator.Builder().select(property1, property2).execute(func1).build();
    final Properties props1 = new Properties();
    props1.put(property1, 1);
    props1.put(property2, "value1");
    final Properties props2 = new Properties();
    props2.put(property1, 10);
    props2.put(property2, "value10");
    final Properties props3 = new Properties();
    props3.put(property1, 5);
    props3.put(property2, "value5");
    // When
    Properties state = props1;
    state = aggregator.apply(state, props2);
    state = aggregator.apply(state, props3);
    // Then
    assertEquals(props2, state);
}
Also used : ExampleTuple2BinaryOperator(uk.gov.gchq.gaffer.function.ExampleTuple2BinaryOperator) TupleAdaptedBinaryOperator(uk.gov.gchq.koryphe.tuple.binaryoperator.TupleAdaptedBinaryOperator) KorypheBinaryOperator(uk.gov.gchq.koryphe.binaryoperator.KorypheBinaryOperator) BinaryOperator(java.util.function.BinaryOperator) ExampleTuple2BinaryOperator(uk.gov.gchq.gaffer.function.ExampleTuple2BinaryOperator) Properties(uk.gov.gchq.gaffer.data.element.Properties) Test(org.junit.jupiter.api.Test)

Aggregations

BinaryOperator (java.util.function.BinaryOperator)19 Test (org.junit.jupiter.api.Test)9 List (java.util.List)7 TupleAdaptedBinaryOperator (uk.gov.gchq.koryphe.tuple.binaryoperator.TupleAdaptedBinaryOperator)6 Consumer (java.util.function.Consumer)4 ExampleTuple2BinaryOperator (uk.gov.gchq.gaffer.function.ExampleTuple2BinaryOperator)4 KorypheBinaryOperator (uk.gov.gchq.koryphe.binaryoperator.KorypheBinaryOperator)4 ArrayList (java.util.ArrayList)3 Arrays (java.util.Arrays)3 Map (java.util.Map)3 Supplier (java.util.function.Supplier)3 ActionListener (java.awt.event.ActionListener)2 Collection (java.util.Collection)2 Collections (java.util.Collections)2 Comparator (java.util.Comparator)2 Set (java.util.Set)2 Stream (java.util.stream.Stream)2 Cache (javax.cache.Cache)2 Ignite (org.apache.ignite.Ignite)2 IgniteCache (org.apache.ignite.IgniteCache)2