summaryrefslogtreecommitdiff
path: root/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalStringTokenizer.java
diff options
context:
space:
mode:
authorbculkin2442 <bjculkin@mix.wvu.edu>2016-04-03 19:22:48 -0400
committerbculkin2442 <bjculkin@mix.wvu.edu>2016-04-03 19:22:48 -0400
commit1c8bc7132d980c1ff2dbd6b9af579c3b2fd8c63e (patch)
treea29777f07ebd81fbef61b5ae02f13f1a9d8f65a2 /BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalStringTokenizer.java
parenta023de85aa08c8f2b8b2441c6b14064eabee2775 (diff)
General code refactoring and maintenance
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalStringTokenizer.java')
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalStringTokenizer.java47
1 files changed, 44 insertions, 3 deletions
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<String> 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,13 +137,18 @@ public class FunctionalStringTokenizer {
* The function to use to convert tokens.
* @return A list containing all of the converted tokens.
*/
- public <E> FunctionalList<E> toList(Function<String, E> tokenTransformer) {
+ public <E> FunctionalList<E>
+ toList(Function<String, E> tokenTransformer) {
+ if (tokenTransformer == null) {
+ throw new NullPointerException("Transformer must not be null");
+ }
+
FunctionalList<E> returnList = new FunctionalList<>();
// Add each token to the list after transforming it
forEachToken(token -> {
E transformedToken = tokenTransformer.apply(token);
-
+
returnList.add(transformedToken);
});
@@ -124,6 +156,15 @@ public class FunctionalStringTokenizer {
}
/**
+ * Convert this tokenizer into a list of strings
+ *
+ * @return This tokenizer, converted into a list of strings
+ */
+ public FunctionalList<String> toList() {
+ return toList((String element) -> element);
+ }
+
+ /**
* Check if this tokenizer has more tokens
*
* @return Whether or not this tokenizer has more tokens