package bjc.utils.funcutils; import java.util.function.Consumer; import java.util.function.Function; import bjc.utils.data.IHolder; import bjc.utils.funcdata.FunctionalList; import bjc.utils.funcdata.IFunctionalList; /** * Implements a single group partitioning pass on a list * * @author ben * * @param * The type of element in the list being partitioned */ final class GroupPartIteration implements Consumer { private IFunctionalList> returnedList; private IHolder> currentPartition; private IFunctionalList rejectedItems; private IHolder numberInCurrentPartition; private int numberPerPartition; private Function elementCounter; public GroupPartIteration( IFunctionalList> returned, IHolder> currPart, IFunctionalList rejects, IHolder numInCurrPart, int nPerPart, Function eleCount) { this.returnedList = returned; this.currentPartition = currPart; this.rejectedItems = rejects; this.numberInCurrentPartition = numInCurrPart; this.numberPerPartition = nPerPart; this.elementCounter = eleCount; } @Override public void accept(E value) { if (numberInCurrentPartition .unwrap((number) -> number >= numberPerPartition)) { returnedList.add( currentPartition.unwrap((partition) -> partition)); currentPartition .transform((partition) -> new FunctionalList<>()); numberInCurrentPartition.transform((number) -> 0); } else { int currentElementCount = elementCounter.apply(value); if (numberInCurrentPartition.unwrap((number) -> (number + currentElementCount) >= numberPerPartition)) { rejectedItems.add(value); } else { currentPartition .unwrap((partition) -> partition.add(value)); numberInCurrentPartition.transform( (number) -> number + currentElementCount); } } } }