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));
}
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);
}
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);
}
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;
}
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();
}
Aggregations