use of java.util.function.BinaryOperator in project waltz by khartec.
the class BatchProcessingCollector_combinerTest method canGetCombinerForNullConsumerAndZeroBatchSize.
@Test
public void canGetCombinerForNullConsumerAndZeroBatchSize() {
BatchProcessingCollector batch = new BatchProcessingCollector(0, null);
BinaryOperator bin = batch.combiner();
assertNotNull(bin);
}
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 Gaffer by gchq.
the class SchemaElementDefinitionValidatorTest method shouldValidateAndReturnTrueWhenAggregatorIsValid.
@Test
public void shouldValidateAndReturnTrueWhenAggregatorIsValid() {
// Given
final SchemaElementDefinitionValidator validator = new SchemaElementDefinitionValidator();
final BinaryOperator<Integer> function1 = mock(BinaryOperator.class);
final BinaryOperator function2 = mock(BinaryOperator.class);
final Schema schema = new Schema.Builder().entity(TestGroups.ENTITY, new SchemaEntityDefinition.Builder().vertex("int1").property(TestPropertyNames.PROP_1, "int1").property(TestPropertyNames.PROP_2, "int2").build()).type("int1", new TypeDefinition.Builder().clazz(Integer.class).aggregateFunction(function1).build()).type("int2", new TypeDefinition.Builder().clazz(Integer.class).aggregateFunction(function2).build()).build();
// When
final ValidationResult result = validator.validate(schema.getEntity(TestGroups.ENTITY));
// Then
assertTrue(result.isValid());
}
use of java.util.function.BinaryOperator in project Gaffer by gchq.
the class ElementAggregatorTest method shouldAggregatePropertiesWithMultipleSelection.
@Test
public void shouldAggregatePropertiesWithMultipleSelection() {
// Given
final BinaryOperator<Tuple3<Integer, Integer, Integer>> maxMinRange = (t1, t2) -> new Tuple3<>(Math.max(t1.get0(), t2.get0()), Math.min(t1.get1(), t2.get1()), Math.max(t1.get0(), t2.get0()) - Math.min(t1.get1(), t2.get1()));
final ElementAggregator aggregator = new ElementAggregator.Builder().select("max", "min", "range").execute(maxMinRange).build();
final Properties properties1 = new Properties();
properties1.put("max", 10);
properties1.put("min", 10);
final Properties properties2 = new Properties();
properties2.put("max", 100);
properties2.put("min", 100);
final Properties properties3 = new Properties();
properties3.put("max", 1000);
properties3.put("min", 1000);
// When
Properties state = aggregator.apply(properties1, properties2);
state = aggregator.apply(state, properties3);
// Then
assertThat(state).hasFieldOrPropertyWithValue("max", 1000).hasFieldOrPropertyWithValue("min", 10).hasFieldOrPropertyWithValue("range", 990);
}
use of java.util.function.BinaryOperator in project Gaffer by gchq.
the class ElementAggregatorTest method shouldBuildAggregator.
@Test
public void shouldBuildAggregator() {
// Given
final String property1 = "property 1";
final String property2a = "property 2a";
final String property2b = "property 2b";
final String property3 = "property 3";
final BinaryOperator func1 = mock(BinaryOperator.class);
final BinaryOperator func2 = mock(BinaryOperator.class);
final BinaryOperator func3 = mock(BinaryOperator.class);
// When - check you can build the selection/function in any order,
// although normally it will be done - select then execute.
final ElementAggregator aggregator = new ElementAggregator.Builder().select(property1).execute(func1).select(property2a, property2b).execute(func2).select(property3).execute(func3).build();
// Then
int i = 0;
TupleAdaptedBinaryOperator<String, ?> adaptedFunction = aggregator.getComponents().get(i++);
assertEquals(1, adaptedFunction.getSelection().length);
assertEquals(property1, adaptedFunction.getSelection()[0]);
assertSame(func1, adaptedFunction.getBinaryOperator());
adaptedFunction = aggregator.getComponents().get(i++);
assertThat(adaptedFunction.getSelection()).hasSize(2);
assertEquals(property2a, adaptedFunction.getSelection()[0]);
assertEquals(property2b, adaptedFunction.getSelection()[1]);
assertSame(func2, adaptedFunction.getBinaryOperator());
adaptedFunction = aggregator.getComponents().get(i++);
assertSame(func3, adaptedFunction.getBinaryOperator());
assertThat(adaptedFunction.getSelection()).hasSize(1);
assertEquals(property3, adaptedFunction.getSelection()[0]);
assertEquals(i, aggregator.getComponents().size());
}
Aggregations