Better code readability.
authorSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Wed, 11 Oct 2017 13:58:55 +0000 (16:58 +0300)
committerSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Wed, 11 Oct 2017 13:58:55 +0000 (16:58 +0300)
src/main/java/eu/svjatoslav/commons/string/tokenizer/Terminator.java
src/main/java/eu/svjatoslav/commons/string/tokenizer/Tokenizer.java

index f0a3fd1..2d0fd45 100755 (executable)
@@ -12,20 +12,22 @@ package eu.svjatoslav.commons.string.tokenizer;
 public class Terminator {
 
     final String startSequence;
-    final boolean ignoreTerminator;
     String endSequence;
+    TerminationStrategy termination;
 
-    public Terminator(final String startPattern, final boolean ignoreTerminator) {
+    public Terminator(final String startPattern, TerminationStrategy termination) {
         startSequence = startPattern;
-        this.ignoreTerminator = ignoreTerminator;
+        this.termination = termination;
     }
 
-    public Terminator(final String startSequence, final String endSequence,
-                      final boolean ignoreTerminator) {
-
+    public Terminator(final String startSequence, final String endSequence, TerminationStrategy termination) {
         this.startSequence = startSequence;
         this.endSequence = endSequence;
-        this.ignoreTerminator = ignoreTerminator;
+        this.termination = termination;
     }
 
+    enum TerminationStrategy {
+        PRESERVE,
+        DROP;
+    }
 }
index a89677b..899f8c4 100755 (executable)
@@ -13,6 +13,8 @@ import java.util.ArrayList;
 import java.util.List;
 import java.util.Stack;
 
+import static eu.svjatoslav.commons.string.tokenizer.Terminator.TerminationStrategy.DROP;
+
 public class Tokenizer {
 
     final Stack<Integer> tokenIndexes = new Stack<>();
@@ -33,15 +35,14 @@ public class Tokenizer {
     }
 
     public Tokenizer addTerminator(final String startSequence,
-                                   final boolean ignoreTerminator) {
-        terminators.add(new Terminator(startSequence, ignoreTerminator));
+                                   final Terminator.TerminationStrategy terminationStrategy) {
+        terminators.add(new Terminator(startSequence, terminationStrategy));
         return this;
     }
 
     public Tokenizer addTerminator(final String startSequence,
-                                   final String endSequence, final boolean ignoreTerminator) {
-        terminators.add(new Terminator(startSequence, endSequence,
-                ignoreTerminator));
+                                   final String endSequence, final Terminator.TerminationStrategy terminationStrategy) {
+        terminators.add(new Terminator(startSequence, endSequence, terminationStrategy));
         return this;
     }
 
@@ -66,7 +67,7 @@ public class Tokenizer {
             for (final Terminator terminator : terminators)
                 if (sequenceMatches(terminator.startSequence))
 
-                    if (terminator.ignoreTerminator) {
+                    if (terminator.termination == DROP) {
                         currentIndex += terminator.startSequence.length();
 
                         if (terminator.endSequence != null)
@@ -95,7 +96,7 @@ public class Tokenizer {
 
     }
 
-    public boolean isNextToken(final String token) {
+    public boolean consumeIfNextToken(final String token) {
         if (token.equals(getNextToken().token))
             return true;