Search in sources :

Example 6 with BinaryOperator

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

the class ElementAggregatorTest method shouldAggregateElementUsingLambdaBinaryOperator.

@Test
public void shouldAggregateElementUsingLambdaBinaryOperator() {
    // Given
    final String propertyReference = "reference1";
    final BinaryOperator<String> function = (a, b) -> a + "," + b;
    final ElementAggregator aggregator = new ElementAggregator.Builder().select(propertyReference).execute(function).build();
    final Edge edge1 = createEdge(propertyReference, "value1");
    final Edge edge2 = createEdge(propertyReference, "value2");
    // When
    final Element result = aggregator.apply(edge1, edge2);
    // Then
    assertEquals("value1,value2", result.getProperty(propertyReference));
}
Also used : TupleAdaptedBinaryOperator(uk.gov.gchq.koryphe.tuple.binaryoperator.TupleAdaptedBinaryOperator) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Properties(uk.gov.gchq.gaffer.data.element.Properties) Element(uk.gov.gchq.gaffer.data.element.Element) KorypheBinaryOperator(uk.gov.gchq.koryphe.binaryoperator.KorypheBinaryOperator) BinaryOperator(java.util.function.BinaryOperator) Assertions.assertSame(org.junit.jupiter.api.Assertions.assertSame) Test(org.junit.jupiter.api.Test) ExampleTuple2BinaryOperator(uk.gov.gchq.gaffer.function.ExampleTuple2BinaryOperator) List(java.util.List) Tuple3(uk.gov.gchq.koryphe.tuple.n.Tuple3) Assertions.assertThatNoException(org.assertj.core.api.Assertions.assertThatNoException) BDDMockito.given(org.mockito.BDDMockito.given) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) Edge(uk.gov.gchq.gaffer.data.element.Edge) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Mockito.mock(org.mockito.Mockito.mock) Element(uk.gov.gchq.gaffer.data.element.Element) Edge(uk.gov.gchq.gaffer.data.element.Edge) Test(org.junit.jupiter.api.Test)

Example 7 with BinaryOperator

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

the class ReduceHandler method doOperation.

/**
 * Handles the {@link Reduce} operation. Applies the {@link BinaryOperator}
 * function contained within the Reduce operation and returns the resulting
 * object.
 *
 * @param operation the {@link uk.gov.gchq.gaffer.operation.Operation} to be executed
 * @param context   the operation chain context, containing the user who executed the operation
 * @param store     the {@link Store} the operation should be run on
 * @return the resulting object from the function
 * @throws OperationException if execution of the operation fails
 */
@Override
public T doOperation(final Reduce<T> operation, final Context context, final Store store) throws OperationException {
    if (null == operation) {
        throw new OperationException("Operation cannot be null");
    }
    Iterable<? extends T> input = operation.getInput();
    if (null == input) {
        throw new OperationException("Input cannot be null");
    }
    final T identity = operation.getIdentity();
    final BinaryOperator aggregateFunction = operation.getAggregateFunction();
    return (T) Streams.toStream(input).reduce(identity, aggregateFunction, aggregateFunction);
}
Also used : BinaryOperator(java.util.function.BinaryOperator) OperationException(uk.gov.gchq.gaffer.operation.OperationException)

Example 8 with BinaryOperator

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

the class CacheUtils method sparseFold.

private static <K, V, A> A sparseFold(String cacheName, IgniteBiFunction<Cache.Entry<K, V>, A, A> folder, IgnitePredicate<K> keyFilter, BinaryOperator<A> accumulator, A zeroVal, V defVal, K defKey, long defValCnt, boolean isNilpotent) {
    A defRes = zeroVal;
    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 localNode = ignite.cluster().localNode();
        A a = zeroVal;
        // 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) == localNode && (keyFilter == null || keyFilter.apply(k))))) a = folder.apply(entry, a);
        }
        return a;
    });
    totalRes.add(defRes);
    return totalRes.stream().reduce(zeroVal, 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) SparseDistributedMatrix(org.apache.ignite.ml.math.impls.matrix.SparseDistributedMatrix) SparseDistributedMatrixStorage(org.apache.ignite.ml.math.impls.storage.matrix.SparseDistributedMatrixStorage) IgniteCallable(org.apache.ignite.lang.IgniteCallable) ClusterNode(org.apache.ignite.cluster.ClusterNode) IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) ValueMapper(org.apache.ignite.ml.math.ValueMapper) Map(java.util.Map) Cache(javax.cache.Cache) ClusterGroup(org.apache.ignite.cluster.ClusterGroup) KeyMapper(org.apache.ignite.ml.math.KeyMapper) CacheEntryImpl(org.apache.ignite.internal.processors.cache.CacheEntryImpl) Collection(java.util.Collection) IgniteRunnable(org.apache.ignite.lang.IgniteRunnable) Ignite(org.apache.ignite.Ignite) BinaryOperator(java.util.function.BinaryOperator) IgniteCache(org.apache.ignite.IgniteCache) IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) Ignition(org.apache.ignite.Ignition) IgniteBiFunction(org.apache.ignite.ml.math.functions.IgniteBiFunction) Collections(java.util.Collections) ScanQuery(org.apache.ignite.cache.query.ScanQuery) IgniteUuid(org.apache.ignite.lang.IgniteUuid) 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 9 with BinaryOperator

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

the class MethodReference method test3.

public void test3() {
    String name = getName();
    Runnable runnable = () -> System.out.print(name);
    ActionListener actionListener = event -> System.out.print(1);
    Runnable runnable1 = () -> {
        System.out.print(1);
    };
    BinaryOperator<Integer> binaryOperator = (x, y) -> x + y;
    BinaryOperator<Long> binaryOperator1 = (Long x, Long y) -> x + y;
}
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)

Example 10 with BinaryOperator

use of java.util.function.BinaryOperator in project gaffer-doc by gchq.

the class PropertiesWalkthrough method listAggregators.

protected boolean listAggregators(final Class<?> clazz) {
    final List<String> aggregateClasses = new ArrayList<>();
    for (final BinaryOperator function : AGGREGATE_FUNCTIONS) {
        final Signature signature = Signature.getInputSignature(function);
        if (signature.assignable(clazz).isValid()) {
            aggregateClasses.add(WalkthroughStrSubstitutor.getJavaDocLink(function.getClass(), false));
        }
    }
    if (!aggregateClasses.isEmpty()) {
        print(AGGREGATORS_KEY, "\nAggregators:");
        print(AGGREGATORS_KEY, "\n- " + StringUtils.join(aggregateClasses, "\n- "));
    }
    return !aggregateClasses.isEmpty();
}
Also used : Signature(uk.gov.gchq.koryphe.signature.Signature) ArrayList(java.util.ArrayList) AdaptedBinaryOperator(uk.gov.gchq.koryphe.binaryoperator.AdaptedBinaryOperator) TupleAdaptedBinaryOperator(uk.gov.gchq.koryphe.tuple.binaryoperator.TupleAdaptedBinaryOperator) BinaryOperator(java.util.function.BinaryOperator)

Aggregations

BinaryOperator (java.util.function.BinaryOperator)15 TupleAdaptedBinaryOperator (uk.gov.gchq.koryphe.tuple.binaryoperator.TupleAdaptedBinaryOperator)6 List (java.util.List)5 Test (org.junit.jupiter.api.Test)5 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 Consumer (java.util.function.Consumer)2 Stream (java.util.stream.Stream)2 Cache (javax.cache.Cache)2 Ignite (org.apache.ignite.Ignite)2 IgniteCache (org.apache.ignite.IgniteCache)2