From 1c8bc7132d980c1ff2dbd6b9af579c3b2fd8c63e Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Sun, 3 Apr 2016 19:22:48 -0400 Subject: General code refactoring and maintenance --- .../main/java/bjc/utils/funcutils/ListUtils.java | 129 +++++++++++++++------ .../main/java/bjc/utils/funcutils/StringUtils.java | 6 + 2 files changed, 99 insertions(+), 36 deletions(-) (limited to 'BJC-Utils2/src/main/java/bjc/utils/funcutils') 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 5215576..5eb488a 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcutils/ListUtils.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcutils/ListUtils.java @@ -28,16 +28,24 @@ public class ListUtils { } @Override - public FunctionalList apply(String opName, - String opRegex) { - if (StringUtils.containsOnly(token, opRegex)) { + public FunctionalList 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(opName)) { - return new FunctionalList<>(opName, - token.split(opRegex)[1]); - } else if (token.endsWith(opName)) { - return new FunctionalList<>(token.split(opRegex)[0], - opName); + } 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); } @@ -55,13 +63,22 @@ public class ListUtils { @Override public FunctionalList 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); } else { - FunctionalList splitTokens = new FunctionalList<>( - tokenToSplit.split(operatorRegex)); + FunctionalList splitTokens = + new FunctionalList<>( + tokenToSplit.split(operatorRegex)); FunctionalList result = new FunctionalList<>(); @@ -161,16 +178,29 @@ public class ListUtils { public static FunctionalList> groupPartition( FunctionalList input, Function elementCounter, int numberPerPartition) { + if (input == null) { + throw new NullPointerException("Input list must not be null"); + } else if (elementCounter == null) { + throw new NullPointerException("Counter must not be null"); + } else if (numberPerPartition < 1 + || numberPerPartition > input.getSize()) { + throw new IllegalArgumentException( + "" + numberPerPartition + " is not a valid" + + " partition size. Must be between 1 and " + + input.getSize()); + } + /* * List that holds our results */ - FunctionalList> returnedList = new FunctionalList<>(); + FunctionalList> returnedList = + new FunctionalList<>(); /* * List that holds current partition */ - GenHolder> currentPartition = new GenHolder<>( - new FunctionalList<>()); + GenHolder> currentPartition = + new GenHolder<>(new FunctionalList<>()); /* * List that holds elements rejected during current pass */ @@ -184,8 +214,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, @@ -226,14 +258,22 @@ public class ListUtils { public static FunctionalList splitTokens( FunctionalList input, Deque> operators) { - GenHolder> ret = new GenHolder<>(input); + if (input == null) { + throw new NullPointerException("Input must not be null"); + } else if (operators == null) { + throw new NullPointerException( + "Set of operators must not be null"); + } + + GenHolder> returnedList = + new GenHolder<>(input); - operators.forEach( - (op) -> ret.transform((oldRet) -> oldRet.flatMap((tok) -> { - return op.merge(new TokenSplitter(tok)); + operators.forEach((operator) -> returnedList + .transform((oldReturn) -> oldReturn.flatMap((token) -> { + return operator.merge(new TokenSplitter(token)); }))); - return ret.unwrap((list) -> list); + return returnedList.unwrap((list) -> list); } /** @@ -241,20 +281,27 @@ public class ListUtils { * * @param input * The tokens to deaffix - * @param ops + * @param operators * The affixes to remove * @return The tokens that have been deaffixed * */ public static FunctionalList deAffixTokens( FunctionalList input, - Deque> ops) { - GenHolder> returnedList = new GenHolder<>( - input); + Deque> operators) { + if (input == null) { + throw new NullPointerException("Input must not be null"); + } else if (operators == null) { + throw new NullPointerException( + "Set of operators must not be null"); + } + + GenHolder> returnedList = + new GenHolder<>(input); - ops.forEach((op) -> returnedList - .transform((oldRet) -> oldRet.flatMap((tok) -> { - return op.merge(new TokenDeaffixer(tok)); + operators.forEach((operator) -> returnedList + .transform((oldReturn) -> oldReturn.flatMap((token) -> { + return operator.merge(new TokenDeaffixer(token)); }))); return returnedList.unwrap((list) -> list); @@ -269,6 +316,10 @@ public class ListUtils { * @return The collapsed string of tokens */ public static String collapseTokens(FunctionalList input) { + if (input == null) { + throw new NullPointerException("Input must not be null"); + } + return collapseTokens(input, ""); } @@ -278,23 +329,29 @@ public class ListUtils { * * @param input * The list of tokens to collapse - * @param sep + * @param seperator * The seperator to use for seperating tokens * @return The collapsed string of tokens */ public static String collapseTokens(FunctionalList input, - String sep) { + String seperator) { + if (input == null) { + throw new NullPointerException("Input must not be null"); + } else if (seperator == null) { + throw new NullPointerException("Seperator must not be null"); + } + if (input.getSize() < 1) { return ""; } else if (input.getSize() == 1) { return input.first(); } else { - return input.reduceAux("", - (currentString, state) -> state + currentString + sep, - (strang) -> { - return strang.substring(0, - strang.length() - sep.length()); - }); + return input.reduceAux("", (currentString, state) -> { + return state + currentString + seperator; + }, (strang) -> { + return strang.substring(0, + strang.length() - seperator.length()); + }); } } } \ No newline at end of file diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcutils/StringUtils.java b/BJC-Utils2/src/main/java/bjc/utils/funcutils/StringUtils.java index b7d20aa..a73292c 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcutils/StringUtils.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcutils/StringUtils.java @@ -28,6 +28,12 @@ public class StringUtils { * group is then matched one or more times and the pattern matches * to the end of the string */ + if (input == null) { + throw new NullPointerException("Input must not be null"); + } else if (regex == null) { + throw new NullPointerException("Regex must not be null"); + } + return input.matches("\\A(?:" + regex + ")+\\Z"); } -- cgit v1.2.3