X-Git-Url: http://www2.svjatoslav.eu/gitweb/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Feu%2Fsvjatoslav%2Fcommons%2Fstring%2Ftokenizer%2FTokenizer.java;h=80f9c4f932ca600fae4ae707e299606e05e49976;hb=c378ab1acf1d7299a572e4a5fc38996ee0408602;hp=722e17a0a723fb2ed1de79626298600bb83f39bb;hpb=ba10494483d7eaf1e9d58ddb1402806c8fc58178;p=svjatoslav_commons.git diff --git a/src/main/java/eu/svjatoslav/commons/string/tokenizer/Tokenizer.java b/src/main/java/eu/svjatoslav/commons/string/tokenizer/Tokenizer.java index 722e17a..80f9c4f 100755 --- a/src/main/java/eu/svjatoslav/commons/string/tokenizer/Tokenizer.java +++ b/src/main/java/eu/svjatoslav/commons/string/tokenizer/Tokenizer.java @@ -19,21 +19,22 @@ import static eu.svjatoslav.commons.string.tokenizer.Terminator.TerminationStrat public class Tokenizer { - final Stack tokenIndexes = new Stack<>(); + private final Stack tokenIndexes = new Stack<>(); private final List terminators = new ArrayList<>(); private String source; private int currentIndex = 0; - int cachedTerminatorIndex = -1; - Terminator cachedTerminator; + private int cachedTerminatorIndex = -1; + private Terminator cachedTerminator; public Tokenizer(final String source) { this.source = source; } - public Tokenizer(){} + public Tokenizer() { + } - public Tokenizer setSource(String source){ + public Tokenizer setSource(String source) { this.source = source; currentIndex = 0; tokenIndexes.clear(); @@ -64,15 +65,14 @@ public class Tokenizer { } - public TokenizerMatch getNextToken() throws InvalidSyntaxException { tokenIndexes.push(currentIndex); StringBuilder tokenAccumulator = new StringBuilder(); - while (true){ + while (true) { - if (currentIndex >= source.length()){ // reached end of input + if (currentIndex >= source.length()) { // reached end of input if (hasAccumulatedToken(tokenAccumulator)) return new TokenizerMatch(tokenAccumulator.toString(), null, null); else @@ -89,7 +89,7 @@ public class Tokenizer { if (terminator.termination == PRESERVE) return buildPreservedToken(tokenAccumulator, terminator); - else if (terminator.termination == DROP){ + else if (terminator.termination == DROP) { skipUntilTerminatorEnd(terminator); if (hasAccumulatedToken(tokenAccumulator)) @@ -147,7 +147,7 @@ public class Tokenizer { return getOrFindTokenTerminator() == null; } - public boolean hasMoreTokens(){ + public boolean hasMoreTokens() { return currentIndex < source.length(); } @@ -184,16 +184,16 @@ public class Tokenizer { return result; } - public boolean peekIsOneOf(String ... possibilities) throws InvalidSyntaxException { + public boolean peekIsOneOf(String... possibilities) throws InvalidSyntaxException { String nextToken = peekNextToken().token; return Stream.of(possibilities).anyMatch(possibility -> possibility.equals(nextToken)); } - public void peekExpectNoneOf(String ... possibilities) throws InvalidSyntaxException { + public void peekExpectNoneOf(String... possibilities) throws InvalidSyntaxException { if (peekIsOneOf(possibilities)) throw new InvalidSyntaxException("Not expected \"" + peekNextToken().token + "\" here."); } - + public void unreadToken() { currentIndex = tokenIndexes.pop(); }