Changed license to CC0
[svjatoslav_commons.git] / src / main / java / eu / svjatoslav / commons / string / tokenizer / Terminator.java
1 /*
2  * Svjatoslav Commons - shared library of common functionality. Author: Svjatoslav Agejenko.
3  * This project is released under Creative Commons Zero (CC0) license.
4  */
5 package eu.svjatoslav.commons.string.tokenizer;
6
7 public class Terminator {
8
9     public final String startSequence;
10     public final String endSequence;
11     public final TerminationStrategy termination;
12
13     public Terminator(final String startSequence, TerminationStrategy termination) {
14         this.startSequence = startSequence;
15         this.endSequence = null;
16         this.termination = termination;
17     }
18
19     public Terminator(final String startSequence, final String endSequence, TerminationStrategy termination) {
20         this.startSequence = startSequence;
21         this.endSequence = endSequence;
22         this.termination = termination;
23     }
24
25     public boolean matches(String source, int index) {
26         // boundary check
27         if (source.length() < (index + startSequence.length()))
28             return false;
29
30         // match check
31         for (int i = 0; i < startSequence.length(); i++)
32             if (startSequence.charAt(i) != source.charAt(index + i))
33                 return false;
34
35         return true;
36     }
37
38     public boolean hasEndSequence() {
39         return endSequence != null;
40     }
41
42     @Override
43     public String toString() {
44         return "Terminator{" +
45                 "startSequence='" + startSequence + '\'' +
46                 ", endSequence='" + endSequence + '\'' +
47                 ", termination=" + termination +
48                 '}';
49     }
50
51     public enum TerminationStrategy {
52         PRESERVE,
53         DROP
54     }
55 }