diff options
| author | Benjamin J. Culkin <bjculkin@mix.wvu.edu> | 2017-09-01 11:48:01 -0300 |
|---|---|---|
| committer | Benjamin J. Culkin <bjculkin@mix.wvu.edu> | 2017-09-01 11:48:01 -0300 |
| commit | 5f7597c878c90bf13dea73d8dd8aed6f0f693702 (patch) | |
| tree | 44cd6287c1962b1f344da7f3c4b6c9809a9f146b /BJC-Utils2/src/main/java/bjc/utils/ioutils | |
| parent | 23a6e61279266f80aba68844acc95c3965b80f95 (diff) | |
Commenting
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc/utils/ioutils')
| -rw-r--r-- | BJC-Utils2/src/main/java/bjc/utils/ioutils/RegexStringEditor.java | 48 | ||||
| -rw-r--r-- | BJC-Utils2/src/main/java/bjc/utils/ioutils/SimpleProperties.java | 8 |
2 files changed, 47 insertions, 9 deletions
diff --git a/BJC-Utils2/src/main/java/bjc/utils/ioutils/RegexStringEditor.java b/BJC-Utils2/src/main/java/bjc/utils/ioutils/RegexStringEditor.java index ee1e2ea..71f6782 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/ioutils/RegexStringEditor.java +++ b/BJC-Utils2/src/main/java/bjc/utils/ioutils/RegexStringEditor.java @@ -79,13 +79,22 @@ public class RegexStringEditor { */ public static String reduceOccurances(final String input, final Pattern rPatt, final UnaryOperator<String> betweenAction, final UnaryOperator<String> onAction) { + /* + * Get all of the occurances. + */ final IList<String> occurances = listOccurances(input, rPatt); + /* + * Execute the correct action on every occurance. + */ final Toggle<UnaryOperator<String>> actions = new ValueToggle<>(onAction, betweenAction); final BiFunction<String, StringBuilder, StringBuilder> reducer = (strang, state) -> { return state.append(actions.get().apply(strang)); }; + /* + * Convert the list back to a string. + */ return occurances.reduceAux(new StringBuilder(), reducer, StringBuilder::toString); } @@ -108,9 +117,15 @@ public class RegexStringEditor { */ public static IList<String> mapOccurances(final String input, final Pattern rPatt, final UnaryOperator<String> betweenAction, final UnaryOperator<String> onAction) { + /* + * Get all of the occurances. + */ final IList<String> occurances = listOccurances(input, rPatt); - final Toggle<UnaryOperator<String>> actions = new ValueToggle<>(onAction, betweenAction); + /* + * Execute the correct action on every occurance. + */ + final Toggle<UnaryOperator<String>> actions = new ValueToggle<>(onAction, betweenAction); return occurances.map(strang -> actions.get().apply(strang)); } @@ -129,23 +144,36 @@ public class RegexStringEditor { public static IList<String> listOccurances(final String input, final Pattern rPatt) { final IList<String> res = new FunctionalList<>(); + /* + * Create the matcher and work buffer. + */ final Matcher matcher = rPatt.matcher(input); - StringBuffer work = new StringBuffer(); + /* + * For every match. + */ while (matcher.find()) { final String match = matcher.group(); + /* + * Append the text until the match to the buffer. + */ matcher.appendReplacement(work, ""); res.add(work.toString()); res.add(match); + /* + * Clear the buffer. + */ work = new StringBuffer(); } + /* + * Add the text after the last match to the buffer. + */ matcher.appendTail(work); - res.add(work.toString()); return res; @@ -168,9 +196,11 @@ public class RegexStringEditor { public static String ifMatches(final String input, final Pattern patt, final UnaryOperator<String> action) { final Matcher matcher = patt.matcher(input); - if (matcher.matches()) + if (matcher.matches()) { return action.apply(input); - else return input; + } else { + return input; + } } /** @@ -191,8 +221,10 @@ public class RegexStringEditor { public static String ifNotMatches(final String input, final Pattern patt, final UnaryOperator<String> action) { final Matcher matcher = patt.matcher(input); - if (matcher.matches()) + if (matcher.matches()) { return input; - else return action.apply(input); + } else { + return action.apply(input); + } } -}
\ No newline at end of file +} diff --git a/BJC-Utils2/src/main/java/bjc/utils/ioutils/SimpleProperties.java b/BJC-Utils2/src/main/java/bjc/utils/ioutils/SimpleProperties.java index e142ea3..e6279c4 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/ioutils/SimpleProperties.java +++ b/BJC-Utils2/src/main/java/bjc/utils/ioutils/SimpleProperties.java @@ -54,6 +54,9 @@ public class SimpleProperties implements Map<String, String> { final int sepIdx = ln.indexOf(' '); + /* + * Complain about improperly formatted lines. + */ if (sepIdx == -1) { final String fmt = "Properties must be a name, a space, then the body.\n\tOffending line is '%s'"; final String msg = String.format(fmt, ln); @@ -64,6 +67,9 @@ public class SimpleProperties implements Map<String, String> { final String name = ln.substring(0, sepIdx).trim(); final String body = ln.substring(sepIdx).trim(); + /* + * Complain about duplicates, if that is wanted. + */ if (!allowDuplicates && containsKey(name)) { final String msg = String.format("Duplicate key '%s'", name); @@ -161,4 +167,4 @@ public class SimpleProperties implements Map<String, String> { public int hashCode() { return props.hashCode(); } -}
\ No newline at end of file +} |
