From 77fcc58d1facffbc3af50be8c05985350e9f1355 Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Sun, 17 Apr 2016 15:01:44 -0400 Subject: Code maintenace and changes --- .../main/java/bjc/utils/funcutils/ListUtils.java | 198 +++++---------------- 1 file changed, 40 insertions(+), 158 deletions(-) (limited to 'BJC-Utils2/src/main/java/bjc/utils/funcutils/ListUtils.java') diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcutils/ListUtils.java b/BJC-Utils2/src/main/java/bjc/utils/funcutils/ListUtils.java index 977186a..95873e9 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcutils/ListUtils.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcutils/ListUtils.java @@ -2,13 +2,11 @@ package bjc.utils.funcutils; import java.util.ArrayList; import java.util.Deque; -import java.util.function.BiFunction; -import java.util.function.Consumer; import java.util.function.Function; -import bjc.utils.data.experimental.IHolder; -import bjc.utils.data.experimental.IPair; -import bjc.utils.data.experimental.Identity; +import bjc.utils.data.IHolder; +import bjc.utils.data.IPair; +import bjc.utils.data.Identity; import bjc.utils.funcdata.FunctionalList; import bjc.utils.funcdata.IFunctionalList; @@ -22,146 +20,6 @@ import bjc.utils.funcdata.IFunctionalList; public class ListUtils { private static final int MAX_NTRIESPART = 50; - private static final class TokenDeaffixer implements - BiFunction> { - private String token; - - public TokenDeaffixer(String tok) { - token = tok; - } - - @Override - public IFunctionalList apply(String operatorName, - String operatorRegex) { - if (operatorName == null) { - throw new NullPointerException( - "Operator name must not be null"); - } else if (operatorRegex == null) { - throw new NullPointerException( - "Operator regex must not be null"); - } - - if (StringUtils.containsOnly(token, operatorRegex)) { - return new FunctionalList<>(token); - } else if (token.startsWith(operatorName)) { - return new FunctionalList<>(operatorName, - token.split(operatorRegex)[1]); - } else if (token.endsWith(operatorName)) { - return new FunctionalList<>(token.split(operatorRegex)[0], - operatorName); - } else { - return new FunctionalList<>(token); - } - } - } - - private static final class TokenSplitter implements - BiFunction> { - private String tokenToSplit; - - public TokenSplitter(String tok) { - this.tokenToSplit = tok; - } - - @Override - public IFunctionalList apply(String operatorName, - String operatorRegex) { - if (operatorName == null) { - throw new NullPointerException( - "Operator name must not be null"); - } else if (operatorRegex == null) { - throw new NullPointerException( - "Operator regex must not be null"); - } - - if (tokenToSplit.contains(operatorName)) { - if (StringUtils.containsOnly(tokenToSplit, - operatorRegex)) { - return new FunctionalList<>(tokenToSplit); - } - - IFunctionalList splitTokens = new FunctionalList<>( - tokenToSplit.split(operatorRegex)); - - IFunctionalList result = new FunctionalList<>(); - - int tokenExpansionSize = splitTokens.getSize(); - - splitTokens.forEachIndexed((tokenIndex, token) -> { - - if (tokenIndex != tokenExpansionSize - && tokenIndex != 0) { - result.add(operatorName); - result.add(token); - } else { - result.add(token); - } - }); - - return result; - } - - return new FunctionalList<>(tokenToSplit); - } - } - - /** - * Implements a single group partitioning pass on a list - * - * @author ben - * - * @param - * The type of element in the list being partitioned - */ - private static 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); - } - } - } - } - /** * Partition a list into a list of lists, where each element can count * for more than one element in a partition @@ -195,13 +53,14 @@ public class ListUtils { /* * List that holds our results */ - IFunctionalList> returnedList = new FunctionalList<>(); + IFunctionalList> returnedList = + new FunctionalList<>(); /* * List that holds current partition */ - IHolder> currentPartition = new Identity<>( - new FunctionalList<>()); + IHolder> currentPartition = + new Identity<>(new FunctionalList<>()); /* * List that holds elements rejected during current pass */ @@ -215,8 +74,10 @@ public class ListUtils { /* * Run up to a certain number of passes */ - for (int numberOfIterations = 0; numberOfIterations < MAX_NTRIESPART - && !rejectedElements.isEmpty(); numberOfIterations++) { + for (int numberOfIterations = + 0; numberOfIterations < MAX_NTRIESPART + && !rejectedElements + .isEmpty(); numberOfIterations++) { input.forEach(new GroupPartIteration<>(returnedList, currentPartition, rejectedElements, numberInCurrentPartition, numberPerPartition, @@ -264,8 +125,8 @@ public class ListUtils { "Set of operators must not be null"); } - IHolder> returnedList = new Identity<>( - input); + IHolder> returnedList = + new Identity<>(input); operators.forEach((operator) -> returnedList .transform((oldReturn) -> oldReturn.flatMap((token) -> { @@ -295,8 +156,8 @@ public class ListUtils { "Set of operators must not be null"); } - IHolder> returnedList = new Identity<>( - input); + IHolder> returnedList = + new Identity<>(input); operators.forEach((operator) -> returnedList .transform((oldReturn) -> oldReturn.flatMap((token) -> { @@ -372,8 +233,8 @@ public class ListUtils { public static IFunctionalList drawWithReplacement( IFunctionalList list, int numberOfItems, Function rng) { - IFunctionalList selectedItems = new FunctionalList<>( - new ArrayList<>(numberOfItems)); + IFunctionalList selectedItems = + new FunctionalList<>(new ArrayList<>(numberOfItems)); for (int i = 0; i < numberOfItems; i++) { selectedItems.add(list.randItem(rng)); @@ -401,8 +262,8 @@ public class ListUtils { public static IFunctionalList drawWithoutReplacement( IFunctionalList list, int numberOfItems, Function rng) { - IFunctionalList selectedItems = new FunctionalList<>( - new ArrayList<>(numberOfItems)); + IFunctionalList selectedItems = + new FunctionalList<>(new ArrayList<>(numberOfItems)); int totalItems = list.getSize(); @@ -420,4 +281,25 @@ public class ListUtils { return selectedItems; } + + /** + * Merge the contents of a bunch of lists together into a single list + * + * @param + * The type of value in this lists + * @param lists + * The values in the lists to merge + * @return A list containing all the elements of the lists + */ + @SafeVarargs + public static IFunctionalList + mergeLists(IFunctionalList... lists) { + IFunctionalList returnedList = new FunctionalList<>(); + + for (IFunctionalList list : lists) { + list.forEach(returnedList::add); + } + + return returnedList; + } } \ No newline at end of file -- cgit v1.2.3