net.morimekta.collect.collectors.InBatchesOfCollector Maven / Gradle / Ivy
package net.morimekta.collect.collectors;
import net.morimekta.collect.UnmodifiableList;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
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;
import static net.morimekta.collect.UnmodifiableList.toList;
import static net.morimekta.collect.UnmodifiableSet.setOf;
public class InBatchesOfCollector implements Collector>, List>> {
private final int itemsPerBatch;
public InBatchesOfCollector(int itemsPerBatch) {
this.itemsPerBatch = itemsPerBatch;
}
@Override
public Supplier>> supplier() {
return LinkedList::new;
}
@Override
public BiConsumer>, T> accumulator() {
return (l, i) -> {
List last = l.peekLast();
if (last == null || last.size() >= itemsPerBatch) {
last = new ArrayList<>(itemsPerBatch);
l.add(last);
}
last.add(i);
};
}
@Override
public BinaryOperator>> combiner() {
return (a, b) -> {
// Merge the two lists so the batches matches the order
// of the non-parallel inBatchesOf with (a1..an) + (b1..bn)
// as the set of items. It's not extremely efficient, but
// works fine as this is not optimized for parallel streams.
List last = a.peekLast();
while (!b.isEmpty()) {
for (T i : b.peekFirst()) {
if (last == null || last.size() >= itemsPerBatch) {
last = new ArrayList<>(itemsPerBatch);
a.add(last);
}
last.add(i);
}
b.pollFirst();
}
return a;
};
}
@Override
public Function>, List>> finisher() {
return batches -> batches.stream()
.map(UnmodifiableList::asList)
.collect(toList());
}
@Override
public Set characteristics() {
return setOf(Characteristics.UNORDERED);
}
}