Code cleanup and formatting.
authorSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Thu, 12 Oct 2017 10:19:35 +0000 (13:19 +0300)
committerSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Thu, 12 Oct 2017 10:19:35 +0000 (13:19 +0300)
src/main/java/eu/svjatoslav/commons/commandline/CLIHelper.java
src/main/java/eu/svjatoslav/commons/commandline/parameterparser/Parameter.java
src/main/java/eu/svjatoslav/commons/data/BitInputStream.java
src/main/java/eu/svjatoslav/commons/data/BitOutputStream.java
src/main/java/eu/svjatoslav/commons/network/navigation/Navigation.java
src/main/java/eu/svjatoslav/commons/string/tokenizer/Terminator.java
src/main/java/eu/svjatoslav/commons/string/tokenizer/Tokenizer.java
src/test/java/eu/svjatoslav/commons/commandline/parameterparser/ParserTest.java
src/test/java/eu/svjatoslav/commons/string/tokenizer/TerminatorTest.java
src/test/java/eu/svjatoslav/commons/string/tokenizer/TokenizerTest.java

index d6ec3ec..5749464 100755 (executable)
@@ -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();
             }
index d55407b..32e9594 100755 (executable)
@@ -14,9 +14,9 @@ import java.util.Collections;
 
 public abstract class Parameter<T, I extends Parameter> {
 
-    public String description;
+    public final String description;
     public final ArrayList<String> arguments = new ArrayList<>();
-    ArgumentCount argumentCount;
+    final ArgumentCount argumentCount;
     private final ArrayList<String> aliases = new ArrayList<>();
     /**
      * Indicates that at least one argument is mandatory for this parameter.
@@ -52,7 +52,7 @@ public abstract class Parameter<T, I extends Parameter> {
         // save aliases
         Collections.addAll(aliases, aliasArray);
 
-        return (I)this;
+        return (I) this;
     }
 
     /**
@@ -182,7 +182,7 @@ public abstract class Parameter<T, I extends Parameter> {
     @SuppressWarnings("unchecked")
     public I setMandatory() {
         mandatory = true;
-        return (I)this;
+        return (I) this;
     }
 
     /**
index 664ffcb..66e3c0f 100755 (executable)
@@ -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;
index e5f4934..58e57f8 100755 (executable)
@@ -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;
index e4d35a3..e6ed8ef 100755 (executable)
@@ -51,8 +51,7 @@ public class Navigation<NI extends NavigationItem> {
         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;
index c1d1983..8dc20ac 100755 (executable)
@@ -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
+    }
 }
index 722e17a..80f9c4f 100755 (executable)
@@ -19,21 +19,22 @@ import static eu.svjatoslav.commons.string.tokenizer.Terminator.TerminationStrat
 
 public class Tokenizer {
 
-    final Stack<Integer> tokenIndexes = new Stack<>();
+    private final Stack<Integer> tokenIndexes = new Stack<>();
     private final List<Terminator> 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();
     }
index 5df31b9..f792e10 100755 (executable)
@@ -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");
index f782949..1b6173a 100644 (file)
@@ -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);
 
index 84571e8..ae68386 100644 (file)
@@ -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