Reimplemented getNextToken
[svjatoslav_commons.git] / src / main / java / eu / svjatoslav / commons / string / tokenizer / Terminator.java
old mode 100644 (file)
new mode 100755 (executable)
index 14fcbb5..a298538
@@ -1,31 +1,56 @@
 /*
  * Svjatoslav Commons - shared library of common functionality.
- * Copyright ©2012-2013, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
+ * Copyright ©2012-2017, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
  * 
  * This program is free software; you can redistribute it and/or
- * modify it under the terms of version 2 of the GNU General Public License
- * as published by the Free Software Foundation.
+ * modify it under the terms of version 3 of the GNU Lesser General Public License
+ * or later as published by the Free Software Foundation.
  */
 
 package eu.svjatoslav.commons.string.tokenizer;
 
 public class Terminator {
 
-       String startSequence;
-       String endSequence;
-       boolean ignoreTerminator;
+    public final String startSequence;
+    public final String endSequence;
+    public final TerminationStrategy termination;
 
-       public Terminator(final String startPattern, final boolean ignoreTerminator) {
-               startSequence = startPattern;
-               this.ignoreTerminator = ignoreTerminator;
-       }
+    public Terminator(final String startSequence, TerminationStrategy termination) {
+        this.startSequence = startSequence;
+        this.endSequence = null;
+        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.termination = termination;
+    }
 
-               this.startSequence = startSequence;
-               this.endSequence = endSequence;
-               this.ignoreTerminator = ignoreTerminator;
-       }
+    public boolean matches(String source, int index) {
+        // boundary check
+        if (source.length() < (index + startSequence.length()))
+            return false;
 
+        // match check
+        for (int i = 0; i < startSequence.length(); i++)
+            if (startSequence.charAt(i) != source.charAt(index + i))
+                return false;
+
+        return true;
+    }
+
+    public enum TerminationStrategy {
+        PRESERVE,
+        DROP
+    }
+
+    @Override
+    public String toString() {
+        return "Terminator{" +
+                "startSequence='" + startSequence + '\'' +
+                ", endSequence='" + endSequence + '\'' +
+                ", termination=" + termination +
+                '}';
+    }
 }