diff options
author | Dico Karssiens <dico.karssiens@gmail.com> | 2019-01-10 18:52:51 +0000 |
---|---|---|
committer | Dico Karssiens <dico.karssiens@gmail.com> | 2019-01-10 18:52:51 +0000 |
commit | 37fa1f9bbfc852dfa67862cde3d11a4e5415db11 (patch) | |
tree | 9350ab43d11b294cd7218fec3b61fa83ea3beda8 /dicore3/command/src/main/java/io/dico/dicore/command/parameter/ParameterList.java | |
parent | d80f69488656485a8b93b4982e678ed62c5df96b (diff) | |
parent | 778e301efc09eb8326146a084bb6788be748e6f1 (diff) |
Diffstat (limited to 'dicore3/command/src/main/java/io/dico/dicore/command/parameter/ParameterList.java')
-rw-r--r-- | dicore3/command/src/main/java/io/dico/dicore/command/parameter/ParameterList.java | 296 |
1 files changed, 148 insertions, 148 deletions
diff --git a/dicore3/command/src/main/java/io/dico/dicore/command/parameter/ParameterList.java b/dicore3/command/src/main/java/io/dico/dicore/command/parameter/ParameterList.java index 613d057..f35628c 100644 --- a/dicore3/command/src/main/java/io/dico/dicore/command/parameter/ParameterList.java +++ b/dicore3/command/src/main/java/io/dico/dicore/command/parameter/ParameterList.java @@ -1,148 +1,148 @@ -package io.dico.dicore.command.parameter; - -import java.util.*; - -/** - * IParameter definition for a command - */ -@SuppressWarnings("UnusedReturnValue") -public class ParameterList { - //private ParameterList parent; - private List<Parameter<?, ?>> indexedParameters; - private Map<String, Parameter<?, ?>> byName; - //private IArgumentPreProcessor argumentPreProcessor = IArgumentPreProcessor.NONE; - private int requiredCount = -1; - private boolean repeatFinalParameter; - - // if the final parameter is repeated and the command is implemented through reflection, - // the repeated parameter is simply the last parameter of the method, rather than the last - // indexed parameter. This might be a flag. As such, this field exists to ensure the correct - // parameter is taken for repeating - private boolean finalParameterMayBeFlag; - - /* - public ParameterList(ParameterList parent) { - this(); - if (parent.repeatFinalParameter) { - throw new IllegalArgumentException("Parent may not have repeating parameters"); - } - this.parent = parent; - }*/ - - public ParameterList() { - this.indexedParameters = new ArrayList<>(); - this.byName = new LinkedHashMap<>(); - this.repeatFinalParameter = false; - } - - /* - public IArgumentPreProcessor getArgumentPreProcessor() { - return argumentPreProcessor; - } - - public ParameterList setArgumentPreProcessor(IArgumentPreProcessor argumentPreProcessor) { - this.argumentPreProcessor = argumentPreProcessor == null ? IArgumentPreProcessor.NONE : argumentPreProcessor; - return this; - }*/ - - public boolean repeatFinalParameter() { - return repeatFinalParameter; - } - - public ParameterList setRepeatFinalParameter(boolean repeatFinalParameter) { - this.repeatFinalParameter = repeatFinalParameter; - return this; - } - - public boolean finalParameterMayBeFlag() { - return finalParameterMayBeFlag; - } - - public ParameterList setFinalParameterMayBeFlag(boolean finalParameterMayBeFlag) { - this.finalParameterMayBeFlag = finalParameterMayBeFlag; - return this; - } - - public int getRequiredCount() { - return requiredCount == -1 ? indexedParameters.size() : requiredCount; - } - - public ParameterList setRequiredCount(int requiredCount) { - this.requiredCount = requiredCount; - return this; - } - - public boolean hasAnyParameters() { - return !byName.isEmpty(); - } - - public int getIndexedParameterCount() { - return indexedParameters.size(); - } - - public List<Parameter<?, ?>> getIndexedParameters() { - return Collections.unmodifiableList(indexedParameters); - } - - public Parameter<?, ?> getParameterByName(String name) { - return byName.get(name); - } - - public String getIndexedParameterName(int index) { - return indexedParameters.get(index).getName(); - } - - public Map<String, Parameter<?, ?>> getParametersByName() { - return Collections.unmodifiableMap(byName); - } - - /** - * Add the given parameter to the end of this parameter list - * Can be a flag - * - * @param parameter the parameter - * @return this - */ - public ParameterList addParameter(Parameter<?, ?> parameter) { - return addParameter(-1, parameter); - } - - /** - * Add the given parameter to this parameter list - * If the parameter is a flag, the index is ignored - * - * @param index parameter index number, -1 if end - * @param parameter the parameter - * @return this - * @throws NullPointerException if parameter is null - */ - public ParameterList addParameter(int index, Parameter<?, ?> parameter) { - //System.out.println("Added parameter " + parameter.getName() + ", flag: " + parameter.isFlag()); - byName.put(parameter.getName(), parameter); - if (!parameter.isFlag()) { - indexedParameters.add(index == -1 ? indexedParameters.size() : index, parameter); - } - return this; - } - - public Parameter<?, ?> getRepeatedParameter() { - if (!repeatFinalParameter) { - return null; - } - if (finalParameterMayBeFlag) { - Iterator<Parameter<?, ?>> iterator = byName.values().iterator(); - Parameter<?, ?> result = null; - while (iterator.hasNext()) { - result = iterator.next(); - } - return result; - } - - if (indexedParameters.isEmpty()) { - return null; - } - - return indexedParameters.get(indexedParameters.size() - 1); - } - -} +package io.dico.dicore.command.parameter;
+
+import java.util.*;
+
+/**
+ * IParameter definition for a command
+ */
+@SuppressWarnings("UnusedReturnValue")
+public class ParameterList {
+ //private ParameterList parent;
+ private List<Parameter<?, ?>> indexedParameters;
+ private Map<String, Parameter<?, ?>> byName;
+ //private IArgumentPreProcessor argumentPreProcessor = IArgumentPreProcessor.NONE;
+ private int requiredCount = -1;
+ private boolean repeatFinalParameter;
+
+ // if the final parameter is repeated and the command is implemented through reflection,
+ // the repeated parameter is simply the last parameter of the method, rather than the last
+ // indexed parameter. This might be a flag. As such, this field exists to ensure the correct
+ // parameter is taken for repeating
+ private boolean finalParameterMayBeFlag;
+
+ /*
+ public ParameterList(ParameterList parent) {
+ this();
+ if (parent.repeatFinalParameter) {
+ throw new IllegalArgumentException("Parent may not have repeating parameters");
+ }
+ this.parent = parent;
+ }*/
+
+ public ParameterList() {
+ this.indexedParameters = new ArrayList<>();
+ this.byName = new LinkedHashMap<>();
+ this.repeatFinalParameter = false;
+ }
+
+ /*
+ public IArgumentPreProcessor getArgumentPreProcessor() {
+ return argumentPreProcessor;
+ }
+
+ public ParameterList setArgumentPreProcessor(IArgumentPreProcessor argumentPreProcessor) {
+ this.argumentPreProcessor = argumentPreProcessor == null ? IArgumentPreProcessor.NONE : argumentPreProcessor;
+ return this;
+ }*/
+
+ public boolean repeatFinalParameter() {
+ return repeatFinalParameter;
+ }
+
+ public ParameterList setRepeatFinalParameter(boolean repeatFinalParameter) {
+ this.repeatFinalParameter = repeatFinalParameter;
+ return this;
+ }
+
+ public boolean finalParameterMayBeFlag() {
+ return finalParameterMayBeFlag;
+ }
+
+ public ParameterList setFinalParameterMayBeFlag(boolean finalParameterMayBeFlag) {
+ this.finalParameterMayBeFlag = finalParameterMayBeFlag;
+ return this;
+ }
+
+ public int getRequiredCount() {
+ return requiredCount == -1 ? indexedParameters.size() : requiredCount;
+ }
+
+ public ParameterList setRequiredCount(int requiredCount) {
+ this.requiredCount = requiredCount;
+ return this;
+ }
+
+ public boolean hasAnyParameters() {
+ return !byName.isEmpty();
+ }
+
+ public int getIndexedParameterCount() {
+ return indexedParameters.size();
+ }
+
+ public List<Parameter<?, ?>> getIndexedParameters() {
+ return Collections.unmodifiableList(indexedParameters);
+ }
+
+ public Parameter<?, ?> getParameterByName(String name) {
+ return byName.get(name);
+ }
+
+ public String getIndexedParameterName(int index) {
+ return indexedParameters.get(index).getName();
+ }
+
+ public Map<String, Parameter<?, ?>> getParametersByName() {
+ return Collections.unmodifiableMap(byName);
+ }
+
+ /**
+ * Add the given parameter to the end of this parameter list
+ * Can be a flag
+ *
+ * @param parameter the parameter
+ * @return this
+ */
+ public ParameterList addParameter(Parameter<?, ?> parameter) {
+ return addParameter(-1, parameter);
+ }
+
+ /**
+ * Add the given parameter to this parameter list
+ * If the parameter is a flag, the index is ignored
+ *
+ * @param index parameter index number, -1 if end
+ * @param parameter the parameter
+ * @return this
+ * @throws NullPointerException if parameter is null
+ */
+ public ParameterList addParameter(int index, Parameter<?, ?> parameter) {
+ //System.out.println("Added parameter " + parameter.getName() + ", flag: " + parameter.isFlag());
+ byName.put(parameter.getName(), parameter);
+ if (!parameter.isFlag()) {
+ indexedParameters.add(index == -1 ? indexedParameters.size() : index, parameter);
+ }
+ return this;
+ }
+
+ public Parameter<?, ?> getRepeatedParameter() {
+ if (!repeatFinalParameter) {
+ return null;
+ }
+ if (finalParameterMayBeFlag) {
+ Iterator<Parameter<?, ?>> iterator = byName.values().iterator();
+ Parameter<?, ?> result = null;
+ while (iterator.hasNext()) {
+ result = iterator.next();
+ }
+ return result;
+ }
+
+ if (indexedParameters.isEmpty()) {
+ return null;
+ }
+
+ return indexedParameters.get(indexedParameters.size() - 1);
+ }
+
+}
|