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 --- .../utils/funcdata/FunctionalStringTokenizer.java | 47 ++++++++++++++++++++-- 1 file changed, 44 insertions(+), 3 deletions(-) (limited to 'BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalStringTokenizer.java') diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalStringTokenizer.java b/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalStringTokenizer.java index 3c819cd..386b732 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalStringTokenizer.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalStringTokenizer.java @@ -19,6 +19,11 @@ public class FunctionalStringTokenizer { * @return A new tokenizer that splits the provided string on spaces. */ public static FunctionalStringTokenizer fromString(String strang) { + if (strang == null) { + throw new NullPointerException( + "String to tokenize must be non-null"); + } + return new FunctionalStringTokenizer( new StringTokenizer(strang, " ")); } @@ -35,6 +40,11 @@ public class FunctionalStringTokenizer { * The string to tokenize */ public FunctionalStringTokenizer(String inp) { + if (inp == null) { + throw new NullPointerException( + "String to tokenize must be non-null"); + } + this.input = new StringTokenizer(inp); } @@ -49,6 +59,14 @@ public class FunctionalStringTokenizer { */ public FunctionalStringTokenizer(String inputString, String seperators) { + if (inputString == null) { + throw new NullPointerException( + "String to tokenize must not be null"); + } else if (seperators == null) { + throw new NullPointerException( + "Tokens to split on must not be null"); + } + this.input = new StringTokenizer(inputString, seperators); } @@ -59,6 +77,11 @@ public class FunctionalStringTokenizer { * The non-functional string tokenizer to wrap */ public FunctionalStringTokenizer(StringTokenizer toWrap) { + if (toWrap == null) { + throw new NullPointerException( + "Wrapped tokenizer must not be null"); + } + this.input = toWrap; } @@ -69,6 +92,10 @@ public class FunctionalStringTokenizer { * The action to execute for each token */ public void forEachToken(Consumer action) { + if (action == null) { + throw new NullPointerException("Action must not be null"); + } + while (input.hasMoreTokens()) { action.accept(input.nextToken()); } @@ -84,7 +111,7 @@ public class FunctionalStringTokenizer { } /** - * Return the next token from the tokenizer Returns null if no more + * Return the next token from the tokenizer. Returns null if no more * tokens are available * * @return The next token from the tokenizer @@ -110,19 +137,33 @@ public class FunctionalStringTokenizer { * The function to use to convert tokens. * @return A list containing all of the converted tokens. */ - public FunctionalList toList(Function tokenTransformer) { + public FunctionalList + toList(Function tokenTransformer) { + if (tokenTransformer == null) { + throw new NullPointerException("Transformer must not be null"); + } + FunctionalList returnList = new FunctionalList<>(); // Add each token to the list after transforming it forEachToken(token -> { E transformedToken = tokenTransformer.apply(token); - + returnList.add(transformedToken); }); return returnList; } + /** + * Convert this tokenizer into a list of strings + * + * @return This tokenizer, converted into a list of strings + */ + public FunctionalList toList() { + return toList((String element) -> element); + } + /** * Check if this tokenizer has more tokens * -- cgit v1.2.3