From d9c673d58d131597ec01c260b45131689059abc3 Mon Sep 17 00:00:00 2001 From: Svjatoslav Agejenko Date: Fri, 19 Dec 2014 21:47:06 +0200 Subject: [PATCH] use generics for more elegant code --- .../parameterparser/Parameter.java | 49 ++++--------------- .../parameter/DirectoryParameter.java | 14 +----- .../parameter/FileParameter.java | 14 +----- .../parameter/IntegerParameter.java | 14 +----- .../parameter/NullParameter.java | 14 +----- .../parameter/StringParameter.java | 14 +----- .../parameter/StringParameters.java | 14 +----- 7 files changed, 16 insertions(+), 117 deletions(-) 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 004a48a..f259f7f 100755 --- a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/Parameter.java +++ b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/Parameter.java @@ -11,7 +11,7 @@ package eu.svjatoslav.commons.commandline.parameterparser; import java.util.ArrayList; -public abstract class Parameter { +public abstract class Parameter { /** * Indicates that at least one argument is mandatory for this parameter. @@ -22,7 +22,7 @@ public abstract class Parameter { public final String description; - protected final ArrayList arguments = new ArrayList(); + public final ArrayList arguments = new ArrayList(); final ArgumentCount argumentCount; @@ -51,15 +51,13 @@ public abstract class Parameter { this.argumentCount = argumentCount; } - public abstract Object addAliases(final String... aliasArray); - - protected Parameter addAliasesProtected(final String... aliasArray) { + public T addAliases(final String... aliasArray) { // save aliases for (final String alias : aliasArray) aliases.add(alias); - return this; + return (T) this; } /** @@ -70,8 +68,8 @@ public abstract class Parameter { // check if arguments are allowed for this parameter if (argumentCount.equals(ArgumentCount.NONE)) { System.out - .println("Error! No arguments are allowed for parameters: " - + getAliases()); + .println("Error! No arguments are allowed for parameters: " + + getAliases()); return false; } @@ -79,8 +77,8 @@ public abstract class Parameter { if ((arguments.size() > 0) && (argumentCount.equals(ArgumentCount.SINGLE))) { System.out - .println("Error! Only single argument is allowed for parameters: " - + getAliases()); + .println("Error! Only single argument is allowed for parameters: " + + getAliases()); return false; } @@ -116,31 +114,6 @@ public abstract class Parameter { return buffer.toString(); } - // public List getArgumentsAsFiles() { - // final ArrayList result = new ArrayList(); - // - // for (final String argument : arguments) { - // final File file = new File(argument); - // result.add(file); - // } - // - // return result; - // } - // - // public List getArgumentsAsIntegers() { - // final ArrayList result = new ArrayList(); - // - // for (final String argument : arguments) - // result.add(Integer.valueOf(argument)); - // - // return result; - // } - // - // public List getArgumentsAsStrings() { - // final ArrayList result = new ArrayList(arguments); - // return result; - // } - public String getHelp() { final StringBuffer buffer = new StringBuffer(); @@ -204,11 +177,9 @@ public abstract class Parameter { return true; } - public abstract Parameter setMandatory(); - - protected Parameter setMandatoryProtected() { + public T setMandatory() { mandatory = true; - return this; + return (T) this; } /** 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 fc03263..1c11a48 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 java.io.File; import eu.svjatoslav.commons.commandline.parameterparser.ArgumentCount; import eu.svjatoslav.commons.commandline.parameterparser.Parameter; -public class DirectoryParameter extends Parameter { +public class DirectoryParameter extends Parameter { private ExistanceType existanceType = ExistanceType.DOES_NOT_MATTER; @@ -22,12 +22,6 @@ public class DirectoryParameter extends Parameter { super(description, ArgumentCount.SINGLE); } - @Override - public DirectoryParameter addAliases(final String... aliasArray) { - super.addAliasesProtected(aliasArray); - return this; - } - @Override public java.lang.String describeFormat() { return existanceType.description + "directory"; @@ -53,12 +47,6 @@ public class DirectoryParameter extends Parameter { return this; } - @Override - public DirectoryParameter setMandatory() { - setMandatoryProtected(); - return this; - } - @Override public boolean validate(final java.lang.String value) { final File file = new File(value); 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 98a9b42..79b0a10 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 java.io.File; import eu.svjatoslav.commons.commandline.parameterparser.ArgumentCount; import eu.svjatoslav.commons.commandline.parameterparser.Parameter; -public class FileParameter extends Parameter { +public class FileParameter extends Parameter { private ExistanceType existanceType = ExistanceType.DOES_NOT_MATTER; @@ -22,12 +22,6 @@ public class FileParameter extends Parameter { super(description, ArgumentCount.SINGLE); } - @Override - public FileParameter addAliases(final String... aliasArray) { - super.addAliasesProtected(aliasArray); - return this; - } - @Override public java.lang.String describeFormat() { return existanceType.description + " file"; @@ -53,12 +47,6 @@ public class FileParameter extends Parameter { return this; } - @Override - public FileParameter setMandatory() { - setMandatoryProtected(); - return this; - } - @Override public boolean validate(final java.lang.String value) { final File file = new File(value); 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 7c75151..ca8e06f 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,18 +12,12 @@ 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); } - @Override - public IntegerParameter addAliases(final String... aliasArray) { - super.addAliasesProtected(aliasArray); - return this; - } - @Override public java.lang.String describeFormat() { return "integer"; @@ -37,12 +31,6 @@ public class IntegerParameter extends Parameter { return Integer.parseInt(arguments.get(0)); } - @Override - public IntegerParameter setMandatory() { - setMandatoryProtected(); - return this; - } - @Override public boolean validate(final java.lang.String value) { try { 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 276ecdb..0531edc 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,18 +12,12 @@ 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); } - @Override - public NullParameter addAliases(final String... aliasArray) { - super.addAliasesProtected(aliasArray); - return this; - } - @Override public java.lang.String describeFormat() { return ""; @@ -34,12 +28,6 @@ public class NullParameter extends Parameter { return isParameterSpecified(); } - @Override - public NullParameter setMandatory() { - setMandatoryProtected(); - return this; - } - @Override public boolean validate(final java.lang.String value) { return true; 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 533763d..e3aafee 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,18 +12,12 @@ 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); } - @Override - public StringParameter addAliases(final String... aliasArray) { - super.addAliasesProtected(aliasArray); - return this; - } - @Override public java.lang.String describeFormat() { return "string"; @@ -39,12 +33,6 @@ public class StringParameter extends Parameter { return arguments.get(0); } - @Override - public StringParameter setMandatory() { - setMandatoryProtected(); - return this; - } - @Override public boolean validate(final java.lang.String value) { return true; 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 3de1b03..231bb32 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,18 +14,12 @@ import java.util.List; import eu.svjatoslav.commons.commandline.parameterparser.ArgumentCount; import eu.svjatoslav.commons.commandline.parameterparser.Parameter; -public class StringParameters extends Parameter { +public class StringParameters extends Parameter { public StringParameters(final String description) { super(description, ArgumentCount.MULTI); } - @Override - public StringParameters addAliases(final String... aliasArray) { - super.addAliasesProtected(aliasArray); - return this; - } - @Override public java.lang.String describeFormat() { return "one to many strings"; @@ -36,12 +30,6 @@ public class StringParameters extends Parameter { return arguments; } - @Override - public StringParameters setMandatory() { - setMandatoryProtected(); - return this; - } - @Override public boolean validate(final java.lang.String value) { return true; -- 2.20.1