/*
* Svjatoslav Commons - shared library of common functionality.
* 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 3 of the GNU Lesser General Public License
* or later as published by the Free Software Foundation.
public class Terminator {
- final String startSequence;
- String endSequence;
- TerminationStrategy termination;
+ public final String startSequence;
+ public final String endSequence;
+ public final TerminationStrategy termination;
- public Terminator(final String startPattern, TerminationStrategy termination) {
- startSequence = startPattern;
+ public Terminator(final String startSequence, TerminationStrategy termination) {
+ this.startSequence = startSequence;
+ this.endSequence = null;
this.termination = termination;
}
this.termination = termination;
}
+ 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 boolean hasEndSequence() {
+ return endSequence != null;
+ }
+
+ @Override
+ public String toString() {
+ return "Terminator{" +
+ "startSequence='" + startSequence + '\'' +
+ ", endSequence='" + endSequence + '\'' +
+ ", termination=" + termination +
+ '}';
+ }
+
public enum TerminationStrategy {
PRESERVE,
DROP