From 0b3d733f44e95ee093985531072a1f8965f7c09a Mon Sep 17 00:00:00 2001 From: Svjatoslav Agejenko Date: Wed, 11 Oct 2017 16:58:55 +0300 Subject: [PATCH] Better code readability. --- .../commons/string/tokenizer/Terminator.java | 16 +++++++++------- .../commons/string/tokenizer/Tokenizer.java | 15 ++++++++------- 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/src/main/java/eu/svjatoslav/commons/string/tokenizer/Terminator.java b/src/main/java/eu/svjatoslav/commons/string/tokenizer/Terminator.java index f0a3fd1..2d0fd45 100755 --- a/src/main/java/eu/svjatoslav/commons/string/tokenizer/Terminator.java +++ b/src/main/java/eu/svjatoslav/commons/string/tokenizer/Terminator.java @@ -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; + } } 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 a89677b..899f8c4 100755 --- a/src/main/java/eu/svjatoslav/commons/string/tokenizer/Tokenizer.java +++ b/src/main/java/eu/svjatoslav/commons/string/tokenizer/Tokenizer.java @@ -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 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; -- 2.20.1