use of com.hazelcast.function.BiConsumerEx in project hazelcast by hazelcast.
the class AggregateOperation3Test method when_withCombiningAccumulateFn_then_accumulateFnCombines.
@Test
public void when_withCombiningAccumulateFn_then_accumulateFnCombines() {
// Given
AggregateOperation3<Object, Object, Object, LongAccumulator, Long> aggrOp = AggregateOperation.withCreate(LongAccumulator::new).andAccumulate0((acc, item) -> acc.addAllowingOverflow(1)).andAccumulate1((acc, item) -> acc.addAllowingOverflow(10)).andAccumulate2((acc, item) -> acc.addAllowingOverflow(100)).andCombine(LongAccumulator::addAllowingOverflow).andExportFinish(LongAccumulator::get);
AggregateOperation1<LongAccumulator, LongAccumulator, Long> combiningAggrOp = aggrOp.withCombiningAccumulateFn(wholeItem());
BiConsumerEx<? super LongAccumulator, ? super LongAccumulator> accFn = combiningAggrOp.accumulateFn();
LongAccumulator partialAcc1 = combiningAggrOp.createFn().get();
LongAccumulator partialAcc2 = combiningAggrOp.createFn().get();
LongAccumulator combinedAcc = combiningAggrOp.createFn().get();
// When
partialAcc1.set(2);
partialAcc2.set(3);
accFn.accept(combinedAcc, partialAcc1);
accFn.accept(combinedAcc, partialAcc2);
// Then
assertEquals(5, combinedAcc.get());
}
use of com.hazelcast.function.BiConsumerEx in project hazelcast by hazelcast.
the class AggregateOperation2Test method when_withCombiningAccumulateFn_then_accumulateFnCombines.
@Test
public void when_withCombiningAccumulateFn_then_accumulateFnCombines() {
// Given
AggregateOperation2<Object, Object, LongAccumulator, Long> aggrOp = AggregateOperation.withCreate(LongAccumulator::new).andAccumulate0((acc, item) -> acc.addAllowingOverflow(1)).andAccumulate1((acc, item) -> acc.addAllowingOverflow(10)).andCombine(LongAccumulator::addAllowingOverflow).andExportFinish(LongAccumulator::get);
AggregateOperation1<LongAccumulator, LongAccumulator, Long> combiningAggrOp = aggrOp.withCombiningAccumulateFn(wholeItem());
BiConsumerEx<? super LongAccumulator, ? super LongAccumulator> accFn = combiningAggrOp.accumulateFn();
LongAccumulator partialAcc1 = combiningAggrOp.createFn().get();
LongAccumulator partialAcc2 = combiningAggrOp.createFn().get();
LongAccumulator combinedAcc = combiningAggrOp.createFn().get();
// When
partialAcc1.set(2);
partialAcc2.set(3);
accFn.accept(combinedAcc, partialAcc1);
accFn.accept(combinedAcc, partialAcc2);
// Then
assertEquals(5, combinedAcc.get());
}
use of com.hazelcast.function.BiConsumerEx in project hazelcast by hazelcast.
the class AggregateOperationTest method when_withCombiningAccumulateFn_then_accumulateFnCombines.
@Test
public void when_withCombiningAccumulateFn_then_accumulateFnCombines() {
// Given
AggregateOperation<LongAccumulator, Long> aggrOp = AggregateOperation.withCreate(LongAccumulator::new).andAccumulate(tag0(), (acc, item) -> acc.add(1)).andAccumulate(tag1(), (acc, item) -> acc.add(10)).andCombine(LongAccumulator::add).andExportFinish(LongAccumulator::get);
AggregateOperation1<LongAccumulator, LongAccumulator, Long> combiningAggrOp = aggrOp.withCombiningAccumulateFn(wholeItem());
BiConsumerEx<? super LongAccumulator, ? super Object> accFn = combiningAggrOp.accumulateFn(tag0());
LongAccumulator partialAcc1 = combiningAggrOp.createFn().get();
LongAccumulator partialAcc2 = combiningAggrOp.createFn().get();
LongAccumulator combinedAcc = combiningAggrOp.createFn().get();
// When
partialAcc1.set(2);
partialAcc2.set(3);
accFn.accept(combinedAcc, partialAcc1);
accFn.accept(combinedAcc, partialAcc2);
// Then
assertEquals(5, combinedAcc.get());
}
use of com.hazelcast.function.BiConsumerEx in project hazelcast by hazelcast.
the class CoAggregateOperationBuilder method build.
/**
* Builds and returns the multi-input {@link AggregateOperation}. It will
* call the supplied {@code exportFinishFn} to transform the {@link ItemsByTag}
* it creates to the result type it emits as the actual result.
*
* @param exportFinishFn function to convert {@link ItemsByTag} to the
* target result type. It must be stateless and {@linkplain
* Processor#isCooperative() cooperative}.
*/
@Nonnull
@SuppressWarnings({ "unchecked", "ConstantConditions" })
public <R> AggregateOperation<Object[], R> build(@Nonnull FunctionEx<? super ItemsByTag, ? extends R> exportFinishFn) {
checkSerializable(exportFinishFn, "exportFinishFn");
Tag[] tags = opsByTag.keySet().stream().sorted().toArray(Tag[]::new);
for (int i = 0; i < tags.length; i++) {
Preconditions.checkTrue(tags[i].index() == i, "Registered tags' indices are " + stream(tags).map(Tag::index).collect(toList()) + ", but should be " + range(0, tags.length).boxed().collect(toList()));
}
// Variable `sorted` extracted due to type inference failure
Stream<Entry<Tag, AggregateOperation1>> sorted = opsByTag.entrySet().stream().sorted(comparing(Entry::getKey));
List<AggregateOperation1> ops = sorted.map(Entry::getValue).collect(toList());
BiConsumerEx[] combineFns = ops.stream().map(AggregateOperation::combineFn).toArray(BiConsumerEx[]::new);
BiConsumerEx[] deductFns = ops.stream().map(AggregateOperation::deductFn).toArray(BiConsumerEx[]::new);
FunctionEx[] exportFns = ops.stream().map(AggregateOperation::exportFn).toArray(FunctionEx[]::new);
FunctionEx[] finishFns = ops.stream().map(AggregateOperation::finishFn).toArray(FunctionEx[]::new);
AggregateOperationBuilder.VarArity<Object[], Void> b = AggregateOperation.withCreate(() -> ops.stream().map(op -> op.createFn().get()).toArray()).varArity();
opsByTag.forEach((tag, op) -> {
int index = tag.index();
b.andAccumulate(tag, (acc, item) -> op.accumulateFn().accept(acc[index], item));
});
return b.andCombine(stream(combineFns).anyMatch(Objects::isNull) ? null : (acc1, acc2) -> {
for (int i = 0; i < combineFns.length; i++) {
combineFns[i].accept(acc1[i], acc2[i]);
}
}).andDeduct(stream(deductFns).anyMatch(Objects::isNull) ? null : (acc1, acc2) -> {
for (int i = 0; i < deductFns.length; i++) {
deductFns[i].accept(acc1[i], acc2[i]);
}
}).<R>andExport(acc -> {
ItemsByTag result = new ItemsByTag();
for (int i = 0; i < exportFns.length; i++) {
result.put(tags[i], exportFns[i].apply(acc[i]));
}
return exportFinishFn.apply(result);
}).andFinish(acc -> {
ItemsByTag result = new ItemsByTag();
for (int i = 0; i < finishFns.length; i++) {
result.put(tags[i], finishFns[i].apply(acc[i]));
}
return exportFinishFn.apply(result);
});
}
Aggregations