diff options
| author | Ben Culkin <scorpress@gmail.com> | 2020-10-06 20:05:00 -0400 |
|---|---|---|
| committer | Ben Culkin <scorpress@gmail.com> | 2020-10-06 20:05:00 -0400 |
| commit | 3a818da77acf50e7ea0d2c02d669cb67b9f114e3 (patch) | |
| tree | a50b5bfe0490d9d6c585c695fc6369ef8936f470 /base/src/main/java/bjc/utils/parserutils | |
| parent | a7a87f682a039d4761112f1dedb9351f3d7a2bbf (diff) | |
Add unit tests for defines
Adds unit tests for SimpleDefine and IteratedDefine.
This also fixes an issue with IteratedDefine, where once you had
consumed a replacer, it was consumed for good; you couldn't use it in
the future, even in a different call to apply().
This was fixed through the introduction of a new iterator type from
esodata - ResettableIterator. See that project/type for more details on
what exactly this does; but suffice to say, it allows to restore our
iterator and re-iterate over the same elements on every call to apply.
Diffstat (limited to 'base/src/main/java/bjc/utils/parserutils')
| -rw-r--r-- | base/src/main/java/bjc/utils/parserutils/defines/IteratedDefine.java | 46 | ||||
| -rw-r--r-- | base/src/main/java/bjc/utils/parserutils/defines/SimpleDefine.java | 12 |
2 files changed, 56 insertions, 2 deletions
diff --git a/base/src/main/java/bjc/utils/parserutils/defines/IteratedDefine.java b/base/src/main/java/bjc/utils/parserutils/defines/IteratedDefine.java index 2dad9c6..8bed45c 100644 --- a/base/src/main/java/bjc/utils/parserutils/defines/IteratedDefine.java +++ b/base/src/main/java/bjc/utils/parserutils/defines/IteratedDefine.java @@ -7,6 +7,7 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; import bjc.data.CircularIterator; +import bjc.data.ResettableIterator; /** * An iterated find/replace, using a circular assortment of replacements. @@ -16,7 +17,7 @@ import bjc.data.CircularIterator; public class IteratedDefine implements UnaryOperator<String> { private Pattern patt; - private Iterator<String> repls; + private ResettableIterator<String> repls; /** * Create a new iterated define. @@ -32,15 +33,56 @@ public class IteratedDefine implements UnaryOperator<String> { public IteratedDefine(Pattern pattern, boolean circular, String... replacers) { patt = pattern; - repls = new CircularIterator<>(Arrays.asList(replacers), circular); + Iterator<String> tmp = new CircularIterator<>(Arrays.asList(replacers), circular); + repls = new ResettableIterator<>(tmp); + } + + /** + * Create a new iterated define. + * + * @param pattern + * The pattern to use for matching. + * @param circular + * Whether or not to loop through the list of replacers, or + * just repeat the last one. + * @param replacers + * The set of replacement strings to use. + */ + public IteratedDefine(String pattern, boolean circular, String... replacers) { + this(Pattern.compile(pattern), circular, replacers); } @Override public String apply(String ln) { + /* + * NOTE Oct 6 2020 - Ben Culkin - Should this be configurable to do/not do? + */ + + /* + * Reset the iterator. This means that the fact that you iterated over a + * replacer previously, doesn't keep it from being used again. + */ + repls.reset(); + Matcher mat = patt.matcher(ln); StringBuffer sb = new StringBuffer(); while (mat.find()) { + /* + * @NOTE Oct 6, 2020 - Ben Culkin + * + * Policy question here. Should we throw an exception if we exhaust our + * replacers and we weren't supposed to? + * + * Other alternatives are: + * + * a) Default to the empty string + * + * b) Keep the last valid replacer. This seems to be what we do as of now, per + * the behavior of CircularIterator. + * + * c) Use the replacer "$0", which is the same as not doing a replace at all + */ String repl = repls.next(); mat.appendReplacement(sb, repl); diff --git a/base/src/main/java/bjc/utils/parserutils/defines/SimpleDefine.java b/base/src/main/java/bjc/utils/parserutils/defines/SimpleDefine.java index b31d937..f20e22a 100644 --- a/base/src/main/java/bjc/utils/parserutils/defines/SimpleDefine.java +++ b/base/src/main/java/bjc/utils/parserutils/defines/SimpleDefine.java @@ -26,6 +26,18 @@ public class SimpleDefine implements UnaryOperator<String> { repl = replace; } + + /** + * Create a new simple define. + * + * @param pattern + * The pattern to match against. + * @param replace + * The text to use as a replacement. + */ + public SimpleDefine(String pattern, String replace) { + this(Pattern.compile(pattern), replace); + } @Override public String apply(String line) { |
