From: Svjatoslav Agejenko Date: Fri, 23 Jun 2017 13:00:57 +0000 (+0200) Subject: Improved generics usage. X-Git-Tag: svjatoslavcommons-1.8~63 X-Git-Url: http://www2.svjatoslav.eu/gitweb/?a=commitdiff_plain;h=41647e2ea4fbbe65049d79c6b2bc73ecdd2a193c;p=svjatoslav_commons.git Improved generics usage. --- 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 6e29ba6..d55407b 100755 --- a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/Parameter.java +++ b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/Parameter.java @@ -12,11 +12,11 @@ package eu.svjatoslav.commons.commandline.parameterparser; import java.util.ArrayList; import java.util.Collections; -public abstract class Parameter { +public abstract class Parameter { - public final String description; + public String description; public final ArrayList arguments = new ArrayList<>(); - final ArgumentCount argumentCount; + ArgumentCount argumentCount; private final ArrayList aliases = new ArrayList<>(); /** * Indicates that at least one argument is mandatory for this parameter. @@ -26,7 +26,7 @@ public abstract class Parameter { * If this parameter was present in the commandline, then this boolean will * be set to true. */ - private boolean parameterSpecified; + private boolean specified; public Parameter(final boolean mandatory, final ArgumentCount argumentCount, final String description, @@ -47,12 +47,12 @@ public abstract class Parameter { } @SuppressWarnings("unchecked") - public T addAliases(final String... aliasArray) { + public I addAliases(final String... aliasArray) { // save aliases Collections.addAll(aliases, aliasArray); - return (T) this; + return (I)this; } /** @@ -129,7 +129,7 @@ public abstract class Parameter { return buffer.toString(); } - public abstract Object getValue(); + public abstract T getValue(); public boolean isMandatory() { return mandatory; @@ -138,15 +138,15 @@ public abstract class Parameter { /** * @return the parameterSpecified */ - public boolean isParameterSpecified() { - return parameterSpecified; + public boolean isSpecified() { + return specified; } /** - * @param parameterSpecified the parameterSpecified to set + * @param specified the parameterSpecified to set */ - public void setParameterSpecified(final boolean parameterSpecified) { - this.parameterSpecified = parameterSpecified; + protected void setSpecified(final boolean specified) { + this.specified = specified; } /** @@ -180,9 +180,9 @@ public abstract class Parameter { } @SuppressWarnings("unchecked") - public T setMandatory() { + public I setMandatory() { mandatory = true; - return (T) this; + return (I)this; } /** diff --git a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/Parser.java b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/Parser.java index ff15a4e..3094403 100755 --- a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/Parser.java +++ b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/Parser.java @@ -18,9 +18,9 @@ import java.util.ArrayList; */ public class Parser { - private final ArrayList> parameters = new ArrayList<>(); + private final ArrayList> parameters = new ArrayList<>(); - public > E add(final E parameter) { + public E add(final E parameter) { parameters.add(parameter); return parameter; } @@ -31,8 +31,8 @@ public class Parser { */ private boolean checkMandatoryArgumentsPresent() { - for (final Parameter parameter : parameters) - if (parameter.isMandatory() && (!parameter.isParameterSpecified())) { + for (final Parameter parameter : parameters) + if (parameter.isMandatory() && (!parameter.isSpecified())) { System.out.println("Error! Mandatory parameter (" + parameter.getAliases() + ") is not specified."); return false; @@ -48,9 +48,9 @@ public class Parser { * @param alias parameter alias * @return found parameter or null if parameter was not found. */ - public Parameter findParameterByAlias(final String alias) { + public Parameter findParameterByAlias(final String alias) { - for (final Parameter parameter : parameters) + for (final Parameter parameter : parameters) if (parameter.matchesAlias(alias)) return parameter; @@ -64,11 +64,11 @@ public class Parser { */ public boolean parse(final String[] args) { - Parameter currentParameter = null; + Parameter currentParameter = null; for (final String argument : args) { - final Parameter parameterForAlias = findParameterByAlias(argument); + final Parameter parameterForAlias = findParameterByAlias(argument); if (parameterForAlias == null) { if (currentParameter == null) { System.out.println("Unknown commandline parameter: " @@ -84,7 +84,7 @@ public class Parser { if (!currentParameter.noMoreArguments()) return false; - parameterForAlias.setParameterSpecified(true); + parameterForAlias.setSpecified(true); currentParameter = parameterForAlias; } @@ -94,7 +94,7 @@ public class Parser { } public void showHelp() { - for (final Parameter parameter : parameters) + for (final Parameter parameter : parameters) System.out.println(parameter.getHelp()); } diff --git a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/DirectoryParameter.java b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/DirectoryParameter.java index 0cdf852..1832334 100755 --- a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/DirectoryParameter.java +++ b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/DirectoryParameter.java @@ -14,7 +14,7 @@ import eu.svjatoslav.commons.commandline.parameterparser.Parameter; import java.io.File; -public class DirectoryParameter extends Parameter { +public class DirectoryParameter extends Parameter { private ExistenceType existenceType = ExistenceType.DOES_NOT_MATTER; diff --git a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/FileParameter.java b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/FileParameter.java index 609b25a..fc8c450 100755 --- a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/FileParameter.java +++ b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/FileParameter.java @@ -14,7 +14,7 @@ import eu.svjatoslav.commons.commandline.parameterparser.Parameter; import java.io.File; -public class FileParameter extends Parameter { +public class FileParameter extends Parameter { private ExistenceType existenceType = ExistenceType.DOES_NOT_MATTER; diff --git a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/IntegerParameter.java b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/IntegerParameter.java index e8a3cfe..6daa894 100755 --- a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/IntegerParameter.java +++ b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/IntegerParameter.java @@ -12,7 +12,7 @@ package eu.svjatoslav.commons.commandline.parameterparser.parameter; import eu.svjatoslav.commons.commandline.parameterparser.ArgumentCount; import eu.svjatoslav.commons.commandline.parameterparser.Parameter; -public class IntegerParameter extends Parameter { +public class IntegerParameter extends Parameter { public IntegerParameter(final String description) { super(description, ArgumentCount.SINGLE); diff --git a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/NullParameter.java b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/NullParameter.java index e082ac1..97c1c79 100755 --- a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/NullParameter.java +++ b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/NullParameter.java @@ -12,7 +12,7 @@ package eu.svjatoslav.commons.commandline.parameterparser.parameter; import eu.svjatoslav.commons.commandline.parameterparser.ArgumentCount; import eu.svjatoslav.commons.commandline.parameterparser.Parameter; -public class NullParameter extends Parameter { +public class NullParameter extends Parameter { public NullParameter(final String description) { super(description, ArgumentCount.NONE); @@ -25,7 +25,7 @@ public class NullParameter extends Parameter { @Override public Boolean getValue() { - return isParameterSpecified(); + return isSpecified(); } @Override diff --git a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/StringParameter.java b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/StringParameter.java index 47f3762..64c56fc 100755 --- a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/StringParameter.java +++ b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/StringParameter.java @@ -12,7 +12,7 @@ package eu.svjatoslav.commons.commandline.parameterparser.parameter; import eu.svjatoslav.commons.commandline.parameterparser.ArgumentCount; import eu.svjatoslav.commons.commandline.parameterparser.Parameter; -public class StringParameter extends Parameter { +public class StringParameter extends Parameter { public StringParameter(final String description) { super(description, ArgumentCount.SINGLE); diff --git a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/StringParameters.java b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/StringParameters.java index 5fcdf69..1287186 100644 --- a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/StringParameters.java +++ b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/StringParameters.java @@ -14,7 +14,7 @@ import eu.svjatoslav.commons.commandline.parameterparser.Parameter; import java.util.List; -public class StringParameters extends Parameter { +public class StringParameters extends Parameter, StringParameters> { public StringParameters(final String description) { super(description, ArgumentCount.MULTI); 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 330eb18..5df31b9 100755 --- a/src/test/java/eu/svjatoslav/commons/commandline/parameterparser/ParserTest.java +++ b/src/test/java/eu/svjatoslav/commons/commandline/parameterparser/ParserTest.java @@ -9,10 +9,15 @@ package eu.svjatoslav.commons.commandline.parameterparser; +import eu.svjatoslav.commons.commandline.parameterparser.parameter.FileParameter; import eu.svjatoslav.commons.commandline.parameterparser.parameter.StringParameter; import org.junit.Before; import org.junit.Test; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; + import static org.junit.Assert.*; public class ParserTest { @@ -25,31 +30,44 @@ public class ParserTest { } @Test - public void testParse() { + public void testParse() throws IOException { // define allowed parameters - final StringParameter helpParameter = parser - .add(new StringParameter("Show help screen")) - .addAliases("--help", "-h").setMandatory(); + final StringParameter helpParameter = parser.add(new StringParameter("Show help screen") + .addAliases("--help", "-h").setMandatory()); + + final StringParameter compileParameter = parser.add(new StringParameter("Compile code")) + .addAliases("--compile", "-c"); + + FileParameter fileParameter = parser.add(new FileParameter("Input file") + .addAliases("-i").mustExist()); - final StringParameter compileParameter = parser.add( - new StringParameter("Compile code")).addAliases("--compile", - "-c"); + createTemporaryFile(); // check help generation parser.showHelp(); // parse arguments - parser.parse(new String[]{"--help", "section"}); + parser.parse(new String[]{"--help", "section", "-i", "/tmp/file with spaces"}); // --help was in the arguments - assertTrue(helpParameter.isParameterSpecified()); + assertTrue(helpParameter.isSpecified()); // compile was not present - assertFalse(compileParameter.isParameterSpecified()); + assertFalse(compileParameter.isSpecified()); // validate that help argument was "section" assertEquals("section", helpParameter.getValue()); + assertTrue(fileParameter.isSpecified()); + assertEquals("/tmp/file with spaces", fileParameter.getValue().getAbsolutePath()); + + } + + private void createTemporaryFile() throws IOException { + File fileWithSpaces = new File("/tmp/file with spaces"); + FileWriter writer = new FileWriter(fileWithSpaces); + writer.write("test"); + writer.close(); } }