From c378ab1acf1d7299a572e4a5fc38996ee0408602 Mon Sep 17 00:00:00 2001 From: Svjatoslav Agejenko Date: Thu, 12 Oct 2017 13:19:35 +0300 Subject: [PATCH] Code cleanup and formatting. --- .../commons/commandline/CLIHelper.java | 4 +-- .../parameterparser/Parameter.java | 8 +++--- .../commons/data/BitInputStream.java | 6 ++--- .../commons/data/BitOutputStream.java | 6 ++--- .../network/navigation/Navigation.java | 3 +-- .../commons/string/tokenizer/Terminator.java | 12 ++++----- .../commons/string/tokenizer/Tokenizer.java | 26 +++++++++---------- .../parameterparser/ParserTest.java | 2 +- .../string/tokenizer/TerminatorTest.java | 2 +- .../string/tokenizer/TokenizerTest.java | 13 ++++------ 10 files changed, 38 insertions(+), 44 deletions(-) diff --git a/src/main/java/eu/svjatoslav/commons/commandline/CLIHelper.java b/src/main/java/eu/svjatoslav/commons/commandline/CLIHelper.java index d6ec3ec..5749464 100755 --- a/src/main/java/eu/svjatoslav/commons/commandline/CLIHelper.java +++ b/src/main/java/eu/svjatoslav/commons/commandline/CLIHelper.java @@ -96,9 +96,7 @@ public class CLIHelper { System.out.print(prompt); try { - final String userInput = br.readLine(); - - return userInput; + return br.readLine(); } catch (final IOException ioe) { ioe.printStackTrace(); } diff --git a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/Parameter.java b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/Parameter.java index d55407b..32e9594 100755 --- a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/Parameter.java +++ b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/Parameter.java @@ -14,9 +14,9 @@ import java.util.Collections; public abstract class Parameter { - public String description; + public final String description; public final ArrayList arguments = new ArrayList<>(); - ArgumentCount argumentCount; + final ArgumentCount argumentCount; private final ArrayList aliases = new ArrayList<>(); /** * Indicates that at least one argument is mandatory for this parameter. @@ -52,7 +52,7 @@ public abstract class Parameter { // save aliases Collections.addAll(aliases, aliasArray); - return (I)this; + return (I) this; } /** @@ -182,7 +182,7 @@ public abstract class Parameter { @SuppressWarnings("unchecked") public I setMandatory() { mandatory = true; - return (I)this; + return (I) this; } /** diff --git a/src/main/java/eu/svjatoslav/commons/data/BitInputStream.java b/src/main/java/eu/svjatoslav/commons/data/BitInputStream.java index 664ffcb..66e3c0f 100755 --- a/src/main/java/eu/svjatoslav/commons/data/BitInputStream.java +++ b/src/main/java/eu/svjatoslav/commons/data/BitInputStream.java @@ -9,13 +9,13 @@ package eu.svjatoslav.commons.data; -/** - * Read individual bits from the input stream. - */ import java.io.IOException; import java.io.InputStream; +/** + * Read individual bits from the input stream. + */ public class BitInputStream { private final InputStream inputStream; diff --git a/src/main/java/eu/svjatoslav/commons/data/BitOutputStream.java b/src/main/java/eu/svjatoslav/commons/data/BitOutputStream.java index e5f4934..58e57f8 100755 --- a/src/main/java/eu/svjatoslav/commons/data/BitOutputStream.java +++ b/src/main/java/eu/svjatoslav/commons/data/BitOutputStream.java @@ -9,13 +9,13 @@ package eu.svjatoslav.commons.data; -/** - * Write individual bits to the output stream. - */ import java.io.IOException; import java.io.OutputStream; +/** + * Write individual bits to the output stream. + */ public class BitOutputStream { private final OutputStream outputStream; diff --git a/src/main/java/eu/svjatoslav/commons/network/navigation/Navigation.java b/src/main/java/eu/svjatoslav/commons/network/navigation/Navigation.java index e4d35a3..e6ed8ef 100755 --- a/src/main/java/eu/svjatoslav/commons/network/navigation/Navigation.java +++ b/src/main/java/eu/svjatoslav/commons/network/navigation/Navigation.java @@ -51,8 +51,7 @@ public class Navigation { try { final String requestPath = new URL(requestUrl).getPath(); - @SuppressWarnings("unchecked") - final NI match = (NI) rootNavigationItem.getMatchingNavigationItem(requestPath); + @SuppressWarnings("unchecked") final NI match = (NI) rootNavigationItem.getMatchingNavigationItem(requestPath); if (match != null) return match; 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 c1d1983..8dc20ac 100755 --- a/src/main/java/eu/svjatoslav/commons/string/tokenizer/Terminator.java +++ b/src/main/java/eu/svjatoslav/commons/string/tokenizer/Terminator.java @@ -40,12 +40,7 @@ public class Terminator { return true; } - public enum TerminationStrategy { - PRESERVE, - DROP - } - - public boolean hasEndSequence(){ + public boolean hasEndSequence() { return endSequence != null; } @@ -57,4 +52,9 @@ public class Terminator { ", termination=" + termination + '}'; } + + public 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 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(); } diff --git a/src/test/java/eu/svjatoslav/commons/commandline/parameterparser/ParserTest.java b/src/test/java/eu/svjatoslav/commons/commandline/parameterparser/ParserTest.java index 5df31b9..f792e10 100755 --- a/src/test/java/eu/svjatoslav/commons/commandline/parameterparser/ParserTest.java +++ b/src/test/java/eu/svjatoslav/commons/commandline/parameterparser/ParserTest.java @@ -34,7 +34,7 @@ public class ParserTest { // define allowed parameters final StringParameter helpParameter = parser.add(new StringParameter("Show help screen") - .addAliases("--help", "-h").setMandatory()); + .addAliases("--help", "-h").setMandatory()); final StringParameter compileParameter = parser.add(new StringParameter("Compile code")) .addAliases("--compile", "-c"); diff --git a/src/test/java/eu/svjatoslav/commons/string/tokenizer/TerminatorTest.java b/src/test/java/eu/svjatoslav/commons/string/tokenizer/TerminatorTest.java index f782949..1b6173a 100644 --- a/src/test/java/eu/svjatoslav/commons/string/tokenizer/TerminatorTest.java +++ b/src/test/java/eu/svjatoslav/commons/string/tokenizer/TerminatorTest.java @@ -8,7 +8,7 @@ import static org.junit.Assert.assertTrue; public class TerminatorTest { @Test - public void testMatches(){ + public void testMatches() { Terminator terminator = new Terminator( "/*", "*/", Terminator.TerminationStrategy.PRESERVE); diff --git a/src/test/java/eu/svjatoslav/commons/string/tokenizer/TokenizerTest.java b/src/test/java/eu/svjatoslav/commons/string/tokenizer/TokenizerTest.java index 84571e8..ae68386 100644 --- a/src/test/java/eu/svjatoslav/commons/string/tokenizer/TokenizerTest.java +++ b/src/test/java/eu/svjatoslav/commons/string/tokenizer/TokenizerTest.java @@ -4,9 +4,7 @@ import org.junit.Test; import static eu.svjatoslav.commons.string.tokenizer.Terminator.TerminationStrategy.DROP; import static eu.svjatoslav.commons.string.tokenizer.Terminator.TerminationStrategy.PRESERVE; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNull; +import static org.junit.Assert.*; public class TokenizerTest { @@ -30,10 +28,9 @@ public class TokenizerTest { Tokenizer tokenizer = new Tokenizer("\"hello\" /** comment **/ (( is a N'2015-03-18 09:48:54.360' test") .addTerminator(" ", DROP) .addTerminator("(", PRESERVE) - .addTerminator("\"", "\"" , PRESERVE) - .addTerminator("N'", "'" , PRESERVE) - .addTerminator("/*", "*/" , DROP) - ; + .addTerminator("\"", "\"", PRESERVE) + .addTerminator("N'", "'", PRESERVE) + .addTerminator("/*", "*/", DROP); assertTokenEquals("\"", "hello", tokenizer); assertTokenEquals("(", null, tokenizer); @@ -65,5 +62,5 @@ public class TokenizerTest { else System.out.println("T: \"" + nextToken.token + "\", R: \"" + nextToken.reminder + "\""); } - + } \ No newline at end of file -- 2.20.1