co.unruly.control.pair.PairReducingCollector Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of control Show documentation
Show all versions of control Show documentation
A collection of functional utilities around error handling, wrapping a successfully returned value or
error
The newest version!
package co.unruly.control.pair;
import java.util.EnumSet;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collector;
/**
* A collector which performs a reduction on a stream of Pairs. This is provided as an alternative to reduce()
* as there is no concept of a Reducer in the streams library, but this does act as a reducer, retaining only
* the reduced value as we reduce over the stream (as opposed to building up a collection, then reducing over that).
*/
public class PairReducingCollector implements Collector, PairReducingCollector.MutablePair, Pair> {
private final L leftIdentity;
private final R rightIdentity;
private final BinaryOperator leftReducer;
private final BinaryOperator rightReducer;
public PairReducingCollector(L leftIdentity, R rightIdentity, BinaryOperator leftReducer, BinaryOperator rightReducer) {
this.leftIdentity = leftIdentity;
this.rightIdentity = rightIdentity;
this.leftReducer = leftReducer;
this.rightReducer = rightReducer;
}
@Override
public Supplier> supplier() {
return () -> new MutablePair<>(leftIdentity, rightIdentity);
}
@Override
public BiConsumer, Pair> accumulator() {
return (acc, item) -> {
acc.left = leftReducer.apply(acc.left, item.left);
acc.right = rightReducer.apply(acc.right, item.right);
};
}
@Override
public BinaryOperator> combiner() {
return (acc1, acc2) -> {
acc1.left = leftReducer.apply(acc1.left, acc2.left);
acc1.right = rightReducer.apply(acc1.right, acc2.right);
return acc1;
};
}
@Override
public Function, Pair> finisher() {
return acc -> Pair.of(acc.left, acc.right);
}
@Override
public Set characteristics() {
return EnumSet.noneOf(Characteristics.class);
}
static class MutablePair {
L left;
R right;
private MutablePair(L left, R right) {
this.left = left;
this.right = right;
}
}
}