summaryrefslogtreecommitdiff
path: root/src/de/lmu/ifi/dbs/elki/utilities/optionhandling
diff options
context:
space:
mode:
Diffstat (limited to 'src/de/lmu/ifi/dbs/elki/utilities/optionhandling')
-rw-r--r--src/de/lmu/ifi/dbs/elki/utilities/optionhandling/constraints/AbstractNumberConstraint.java7
-rw-r--r--src/de/lmu/ifi/dbs/elki/utilities/optionhandling/constraints/EqualStringConstraint.java2
-rw-r--r--src/de/lmu/ifi/dbs/elki/utilities/optionhandling/constraints/IntervalConstraint.java249
-rw-r--r--src/de/lmu/ifi/dbs/elki/utilities/optionhandling/constraints/ListSizeConstraint.java3
-rw-r--r--src/de/lmu/ifi/dbs/elki/utilities/optionhandling/constraints/ParameterFlagGlobalConstraint.java4
-rw-r--r--src/de/lmu/ifi/dbs/elki/utilities/optionhandling/constraints/StringLengthConstraint.java2
-rw-r--r--src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameterization/EmptyParameterization.java3
-rw-r--r--src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameterization/ListParameterization.java2
-rw-r--r--src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameterization/SerializedParameterization.java2
-rw-r--r--src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameterization/UnParameterization.java5
-rw-r--r--src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameters/EnumParameter.java167
11 files changed, 304 insertions, 142 deletions
diff --git a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/constraints/AbstractNumberConstraint.java b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/constraints/AbstractNumberConstraint.java
index a631a316..6b49faea 100644
--- a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/constraints/AbstractNumberConstraint.java
+++ b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/constraints/AbstractNumberConstraint.java
@@ -23,15 +23,16 @@ package de.lmu.ifi.dbs.elki.utilities.optionhandling.constraints;
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-
/**
* Abstract super class for constraints dealing with a certain number value.
*
* @author Elke Achtert
+ *
+ * @apiviz.uses de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.NumberParameter
+ *
* @param <P> the type of the parameter to be tested by this constraint (e.g., Number, List<Number>)
*/
public abstract class AbstractNumberConstraint<P> implements ParameterConstraint<P> {
-
/**
* The constraint value.
*/
@@ -45,4 +46,4 @@ public abstract class AbstractNumberConstraint<P> implements ParameterConstraint
public AbstractNumberConstraint(Number constraintValue) {
this.constraintValue = constraintValue;
}
-}
+} \ No newline at end of file
diff --git a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/constraints/EqualStringConstraint.java b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/constraints/EqualStringConstraint.java
index ea952a30..342ffd00 100644
--- a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/constraints/EqualStringConstraint.java
+++ b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/constraints/EqualStringConstraint.java
@@ -35,6 +35,8 @@ import de.lmu.ifi.dbs.elki.utilities.optionhandling.WrongParameterValueException
* ) to be tested is equal to the specified constraint-strings.
*
* @author Steffi Wanka
+ *
+ * @apiviz.uses de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.StringParameter
*/
public class EqualStringConstraint implements ParameterConstraint<String> {
/**
diff --git a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/constraints/IntervalConstraint.java b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/constraints/IntervalConstraint.java
index a2d5b8b3..8a9f1bcf 100644
--- a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/constraints/IntervalConstraint.java
+++ b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/constraints/IntervalConstraint.java
@@ -27,148 +27,135 @@ import de.lmu.ifi.dbs.elki.utilities.optionhandling.ParameterException;
import de.lmu.ifi.dbs.elki.utilities.optionhandling.WrongParameterValueException;
/**
- * Represents an interval parameter constraint testing if a given value lies within
- * the specified interval.
- * The value of the number parameter ({@link de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.NumberParameter})
- * tested has to be greater than (or equal to, if specified) than the specified low constraint value
- * and less than (or equal to, if specified) than the specified high constraint value.
- *
+ * Represents an interval parameter constraint testing if a given value lies
+ * within the specified interval. The value of the number parameter (
+ * {@link de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.NumberParameter}
+ * ) tested has to be greater than (or equal to, if specified) than the
+ * specified low constraint value and less than (or equal to, if specified) than
+ * the specified high constraint value.
+ *
* @author Steffi Wanka
+ *
+ * @apiviz.uses de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.NumberParameter
*/
public class IntervalConstraint implements ParameterConstraint<Number> {
+ /**
+ * Available interval boundary types types:
+ * {@link IntervalConstraint.IntervalBoundary#OPEN} denotes an opend interval,
+ * i.e. less than (or greater than) comparison
+ * {@link IntervalConstraint.IntervalBoundary#CLOSE} denotes a closed
+ * interval, i.e. an equal to or less than (or equal to or greater than)
+ * comparison
+ *
+ * @apiviz.exclude
+ */
+ public enum IntervalBoundary {
/**
- * Available interval boundary types types:
- * {@link IntervalConstraint.IntervalBoundary#OPEN} denotes an opend interval,
- * i.e. less than (or greater than) comparison
- * {@link IntervalConstraint.IntervalBoundary#CLOSE} denotes a closed interval,
- * i.e. an equal to or less than (or equal to or greater than) comparison
- *
- * @apiviz.exclude
+ * Open interval boundary
*/
- public enum IntervalBoundary {
- /**
- * Open interval boundary
- */
- OPEN,
- /**
- * Closed interval boundary
- */
- CLOSE
- }
-
- /**
- * The low constraint value (left interval boundary).
- */
- private final Number lowConstraintValue;
-
- /**
- * The interval boundary for the low constraint value (@see IntervalBoundary)
- */
- private final IntervalBoundary lowBoundary;
-
- /**
- * The high constraint value (right interval boundary).
- */
- private final Number highConstraintValue;
-
+ OPEN,
/**
- * The interval boundary for the high constraint value (@see IntervalBoundary)
+ * Closed interval boundary
*/
- private final IntervalBoundary highBoundary;
+ CLOSE
+ }
+
+ /**
+ * The low constraint value (left interval boundary).
+ */
+ private final Number lowConstraintValue;
+
+ /**
+ * The interval boundary for the low constraint value (@see IntervalBoundary)
+ */
+ private final IntervalBoundary lowBoundary;
+
+ /**
+ * The high constraint value (right interval boundary).
+ */
+ private final Number highConstraintValue;
+
+ /**
+ * The interval boundary for the high constraint value (@see IntervalBoundary)
+ */
+ private final IntervalBoundary highBoundary;
+
+ /**
+ * Creates an IntervalConstraint parameter constraint.
+ * <p/>
+ * That is, the value of the number parameter given has to be greater than (or
+ * equal to, if specified) than the specified low constraint value and less
+ * than (or equal to, if specified) than the specified high constraint value.
+ *
+ * @param lowConstraintValue the low constraint value (left interval boundary)
+ * @param lowBoundary the interval boundary for the low constraint value (@see
+ * IntervalBoundary)
+ * @param highConstraintValue the high constraint value (right interval
+ * boundary)
+ * @param highBoundary the interval boundary for the high constraint value
+ * (@see IntervalBoundary)
+ */
+ public IntervalConstraint(Number lowConstraintValue, IntervalBoundary lowBoundary, Number highConstraintValue, IntervalBoundary highBoundary) {
+ if(lowConstraintValue.doubleValue() >= highConstraintValue.doubleValue()) {
+ throw new IllegalArgumentException("Left interval boundary is greater than " + "or equal to right interval boundary!");
+ }
- /**
- * Creates an IntervalConstraint parameter constraint.
- * <p/>
- * That is, the value of the number parameter given
- * has to be greater than (or equal to, if specified) than the specified low constraint value
- * and less than (or equal to, if specified) than the specified high constraint value.
- *
- * @param lowConstraintValue the low constraint value (left interval boundary)
- * @param lowBoundary the interval boundary for the low constraint value (@see IntervalBoundary)
- * @param highConstraintValue the high constraint value (right interval boundary)
- * @param highBoundary the interval boundary for the high constraint value (@see IntervalBoundary)
- */
- public IntervalConstraint(Number lowConstraintValue, IntervalBoundary lowBoundary,
- Number highConstraintValue, IntervalBoundary highBoundary) {
- if (lowConstraintValue.doubleValue() >= highConstraintValue.doubleValue()) {
- throw new IllegalArgumentException("Left interval boundary is greater than " +
- "or equal to right interval boundary!");
- }
-
- this.lowConstraintValue = lowConstraintValue;
- this.lowBoundary = lowBoundary;
- this.highConstraintValue = highConstraintValue;
- this.highBoundary = highBoundary;
+ this.lowConstraintValue = lowConstraintValue;
+ this.lowBoundary = lowBoundary;
+ this.highConstraintValue = highConstraintValue;
+ this.highBoundary = highBoundary;
+ }
+
+ /**
+ * Checks if the number value given by the number parameter is greater equal
+ * than the constraint value. If not, a parameter exception is thrown.
+ *
+ */
+ @Override
+ public void test(Number t) throws ParameterException {
+ // lower value
+ if(lowBoundary.equals(IntervalBoundary.CLOSE)) {
+ if(t.doubleValue() < lowConstraintValue.doubleValue()) {
+ throw new WrongParameterValueException("Parameter Constraint Error: \n" + "The parameter value specified has to be " + "equal to or greater than " + lowConstraintValue.toString() + ". (current value: " + t.doubleValue() + ")\n");
+ }
+ }
+ else if(lowBoundary.equals(IntervalBoundary.OPEN)) {
+ if(t.doubleValue() <= lowConstraintValue.doubleValue()) {
+ throw new WrongParameterValueException("Parameter Constraint Error: \n" + "The parameter value specified has to be " + "greater than " + lowConstraintValue.toString() + ". (current value: " + t.doubleValue() + ")\n");
+ }
}
- /**
- * Checks if the number value given by the number parameter is
- * greater equal than the constraint
- * value. If not, a parameter exception is thrown.
- *
- */
- @Override
- public void test(Number t) throws ParameterException {
- // lower value
- if (lowBoundary.equals(IntervalBoundary.CLOSE)) {
- if (t.doubleValue() < lowConstraintValue.doubleValue()) {
- throw new WrongParameterValueException("Parameter Constraint Error: \n" +
- "The parameter value specified has to be " +
- "equal to or greater than " +
- lowConstraintValue.toString() +
- ". (current value: " + t.doubleValue() + ")\n");
- }
- }
- else if (lowBoundary.equals(IntervalBoundary.OPEN)) {
- if (t.doubleValue() <= lowConstraintValue.doubleValue()) {
- throw new WrongParameterValueException("Parameter Constraint Error: \n" +
- "The parameter value specified has to be " +
- "greater than " +
- lowConstraintValue.toString() +
- ". (current value: " + t.doubleValue() + ")\n");
- }
- }
-
- // higher value
- if (highBoundary.equals(IntervalBoundary.CLOSE)) {
- if (t.doubleValue() > highConstraintValue.doubleValue()) {
- throw new WrongParameterValueException("Parameter Constraint Error: \n" +
- "The parameter value specified has to be " +
- "equal to or less than " +
- highConstraintValue.toString() +
- ". (current value: " + t.doubleValue() + ")\n");
- }
- }
- else if (highBoundary.equals(IntervalBoundary.OPEN)) {
- if (t.doubleValue() >= highConstraintValue.doubleValue()) {
- throw new WrongParameterValueException("Parameter Constraint Error: \n" +
- "The parameter value specified has to be " +
- "less than " +
- highConstraintValue.toString() +
- ". (current value: " + t.doubleValue() + ")\n");
- }
- }
+ // higher value
+ if(highBoundary.equals(IntervalBoundary.CLOSE)) {
+ if(t.doubleValue() > highConstraintValue.doubleValue()) {
+ throw new WrongParameterValueException("Parameter Constraint Error: \n" + "The parameter value specified has to be " + "equal to or less than " + highConstraintValue.toString() + ". (current value: " + t.doubleValue() + ")\n");
+ }
}
+ else if(highBoundary.equals(IntervalBoundary.OPEN)) {
+ if(t.doubleValue() >= highConstraintValue.doubleValue()) {
+ throw new WrongParameterValueException("Parameter Constraint Error: \n" + "The parameter value specified has to be " + "less than " + highConstraintValue.toString() + ". (current value: " + t.doubleValue() + ")\n");
+ }
+ }
+ }
- @Override
- public String getDescription(String parameterName) {
- String description = parameterName + " in ";
- if (lowBoundary.equals(IntervalBoundary.CLOSE)) {
- description += "[";
- }
- else if (lowBoundary.equals(IntervalBoundary.OPEN)) {
- description += "(";
- }
-
- description += lowConstraintValue.toString() + ", " + highConstraintValue;
-
- if (highBoundary.equals(IntervalBoundary.CLOSE)) {
- description += "]";
- }
- if (highBoundary.equals(IntervalBoundary.OPEN)) {
- description += ")";
- }
- return description;
+ @Override
+ public String getDescription(String parameterName) {
+ String description = parameterName + " in ";
+ if(lowBoundary.equals(IntervalBoundary.CLOSE)) {
+ description += "[";
+ }
+ else if(lowBoundary.equals(IntervalBoundary.OPEN)) {
+ description += "(";
}
-}
+ description += lowConstraintValue.toString() + ", " + highConstraintValue;
+
+ if(highBoundary.equals(IntervalBoundary.CLOSE)) {
+ description += "]";
+ }
+ if(highBoundary.equals(IntervalBoundary.OPEN)) {
+ description += ")";
+ }
+ return description;
+ }
+} \ No newline at end of file
diff --git a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/constraints/ListSizeConstraint.java b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/constraints/ListSizeConstraint.java
index f2fef29d..5c6a5156 100644
--- a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/constraints/ListSizeConstraint.java
+++ b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/constraints/ListSizeConstraint.java
@@ -35,6 +35,9 @@ import de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.ListParameter;
* size constraint.
*
* @author Steffi Wanka
+ *
+ * @apiviz.uses de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.ListParameter
+ *
* @param <T> Parameter type
*/
public class ListSizeConstraint<T> implements ParameterConstraint<List<T>> {
diff --git a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/constraints/ParameterFlagGlobalConstraint.java b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/constraints/ParameterFlagGlobalConstraint.java
index bb2239b3..4c5b9262 100644
--- a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/constraints/ParameterFlagGlobalConstraint.java
+++ b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/constraints/ParameterFlagGlobalConstraint.java
@@ -36,6 +36,10 @@ import de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.Parameter;
* the flag the parameter is tested for keeping its constraints or not.
*
* @author Steffi Wanka
+ *
+ * @apiviz.uses de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.Flag
+ * @apiviz.uses de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.Parameter
+ *
* @param <C> Constraint type
* @param <S> Parameter type
*/
diff --git a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/constraints/StringLengthConstraint.java b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/constraints/StringLengthConstraint.java
index 4fc2592c..ac8c48b3 100644
--- a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/constraints/StringLengthConstraint.java
+++ b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/constraints/StringLengthConstraint.java
@@ -32,6 +32,8 @@ import de.lmu.ifi.dbs.elki.utilities.optionhandling.WrongParameterValueException
* .
*
* @author Erich Schubert
+ *
+ * @apiviz.uses de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.StringParameter
*/
public class StringLengthConstraint implements ParameterConstraint<String> {
/**
diff --git a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameterization/EmptyParameterization.java b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameterization/EmptyParameterization.java
index 6def810a..05a85b49 100644
--- a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameterization/EmptyParameterization.java
+++ b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameterization/EmptyParameterization.java
@@ -37,7 +37,6 @@ public class EmptyParameterization extends AbstractParameterization {
return false;
}
- @SuppressWarnings("unused")
@Override
public boolean setValueForOption(Parameter<?,?> opt) throws ParameterException {
// Always return false, we don't have extra parameters,
@@ -49,7 +48,7 @@ public class EmptyParameterization extends AbstractParameterization {
* Default implementation, for flat parameterizations.
*/
@Override
- public Parameterization descend(@SuppressWarnings("unused") Object option) {
+ public Parameterization descend(Object option) {
return this;
}
} \ No newline at end of file
diff --git a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameterization/ListParameterization.java b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameterization/ListParameterization.java
index 8366a089..8e751fe7 100644
--- a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameterization/ListParameterization.java
+++ b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameterization/ListParameterization.java
@@ -138,7 +138,7 @@ public class ListParameterization extends AbstractParameterization {
* Default implementation, for flat parameterizations.
*/
@Override
- public Parameterization descend(@SuppressWarnings("unused") Object option) {
+ public Parameterization descend(Object option) {
return this;
}
diff --git a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameterization/SerializedParameterization.java b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameterization/SerializedParameterization.java
index 6d19cfd5..a4eeda1a 100644
--- a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameterization/SerializedParameterization.java
+++ b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameterization/SerializedParameterization.java
@@ -166,7 +166,7 @@ public class SerializedParameterization extends AbstractParameterization {
* Default implementation, for flat parameterizations.
*/
@Override
- public Parameterization descend(@SuppressWarnings("unused") Object option) {
+ public Parameterization descend(Object option) {
return this;
}
} \ No newline at end of file
diff --git a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameterization/UnParameterization.java b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameterization/UnParameterization.java
index 4704e1da..5fc930db 100644
--- a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameterization/UnParameterization.java
+++ b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameterization/UnParameterization.java
@@ -51,7 +51,6 @@ public class UnParameterization implements Parameterization {
}
@Override
- @SuppressWarnings("unused")
public boolean checkConstraint(GlobalParameterConstraint constraint) {
return false;
}
@@ -67,7 +66,6 @@ public class UnParameterization implements Parameterization {
}
@Override
- @SuppressWarnings("unused")
public boolean grab(Parameter<?, ?> opt) {
return false;
}
@@ -78,13 +76,12 @@ public class UnParameterization implements Parameterization {
}
@Override
- @SuppressWarnings("unused")
public boolean setValueForOption(Parameter<?, ?> opt) throws ParameterException {
return false;
}
@Override
- public Parameterization descend(@SuppressWarnings("unused") Object option) {
+ public Parameterization descend(Object option) {
return this;
}
diff --git a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameters/EnumParameter.java b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameters/EnumParameter.java
new file mode 100644
index 00000000..afab86c8
--- /dev/null
+++ b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameters/EnumParameter.java
@@ -0,0 +1,167 @@
+package de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters;
+
+/*
+ This file is part of ELKI:
+ Environment for Developing KDD-Applications Supported by Index-Structures
+
+ Copyright (C) 2011
+ Ludwig-Maximilians-Universität München
+ Lehr- und Forschungseinheit für Datenbanksysteme
+ ELKI Development Team
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU Affero General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU Affero General Public License for more details.
+
+ You should have received a copy of the GNU Affero General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+import java.util.ArrayList;
+import java.util.Collection;
+
+import de.lmu.ifi.dbs.elki.utilities.optionhandling.OptionID;
+import de.lmu.ifi.dbs.elki.utilities.optionhandling.ParameterException;
+import de.lmu.ifi.dbs.elki.utilities.optionhandling.UnspecifiedParameterException;
+import de.lmu.ifi.dbs.elki.utilities.optionhandling.WrongParameterValueException;
+
+/**
+ * Parameter class for a parameter specifying an enum type.
+ *
+ * <p>
+ * Usage:
+ *
+ * <pre>
+ * // Enum declaration.
+ * enum MyEnum { VALUE1, VALUE2 };
+ * // Parameter value holder.
+ * MyEnum myEnumParameter;
+ *
+ * // ...
+ *
+ * // Parameterization.
+ * EnumParameter&lt;MyEnum&gt; param = new EnumParameter&lt;MyEnum&gt;(ENUM_PROPERTY_ID, MyEnum.class);
+ * // OR
+ * EnumParameter&lt;MyEnum&gt; param = new EnumParameter&lt;MyEnum&gt;(ENUM_PROPERTY_ID, MyEnum.class, MyEnum.VALUE1);
+ * // OR
+ * EnumParameter&lt;MyEnum&gt; param = new EnumParameter&lt;MyEnum&gt;(ENUM_PROPERTY_ID, MyEnum.class, true);
+ *
+ * if(config.grab(param)) {
+ * myEnumParameter = param.getValue();
+ * }
+ *
+ * </p>
+ *
+ * @author Florian Nuecke
+ *
+ * @param <E> Enum type
+ */
+public class EnumParameter<E extends Enum<E>> extends Parameter<Enum<E>, E> {
+
+ /**
+ * Reference to the actual enum type, for T.valueOf().
+ */
+ protected Class<E> enumClass;
+
+ /**
+ * Constructs an enum parameter with the given optionID, constraints and
+ * default value.
+ *
+ * @param optionID the unique id of the parameter
+ * @param defaultValue the default value of the parameter
+ */
+ public EnumParameter(OptionID optionID, Class<E> enumClass, E defaultValue) {
+ super(optionID, defaultValue);
+ this.enumClass = enumClass;
+ }
+
+ /**
+ * Constructs an enum parameter with the given optionID, constraints and
+ * default value.
+ *
+ * @param optionID the unique id of the parameter
+ * @param optional Flag to signal an optional parameter.
+ */
+ public EnumParameter(OptionID optionID, Class<E> enumClass, boolean optional) {
+ super(optionID, optional);
+ this.enumClass = enumClass;
+ }
+
+ /**
+ * Constructs an enum parameter with the given optionID, constraints and
+ * default value.
+ *
+ * @param optionID the unique id of the parameter
+ */
+ public EnumParameter(OptionID optionID, Class<E> enumClass) {
+ super(optionID);
+ this.enumClass = enumClass;
+ }
+
+ @Override
+ public String getSyntax() {
+ return "<" + joinEnumNames(" | ") + ">";
+ }
+
+ @Override
+ protected E parseValue(Object obj) throws ParameterException {
+ if(obj == null) {
+ throw new UnspecifiedParameterException("Parameter \"" + getName() + "\": Null value given!");
+ }
+ if(obj instanceof String) {
+ try {
+ return Enum.valueOf(enumClass, (String) obj);
+ }
+ catch(IllegalArgumentException ex) {
+ throw new WrongParameterValueException("Enum parameter " + getName() + " is invalid (must be one of [" + joinEnumNames(", ") + "].");
+ }
+ }
+ throw new WrongParameterValueException("Enum parameter " + getName() + " is not given as a string.");
+ }
+
+ @Override
+ public String getValueAsString() {
+ return getValue().name();
+ }
+
+ /**
+ * Get a list of possible values for this enum parameter.
+ *
+ * @return list of strings representing possible enum values.
+ */
+ public Collection<String> getPossibleValues() {
+ // Convert to string array
+ final E[] enums = enumClass.getEnumConstants();
+ ArrayList<String> values = new ArrayList<String>(enums.length);
+ for(E t : enums) {
+ values.add(t.name());
+ }
+ return values;
+ }
+
+ /**
+ * Utility method for merging possible values into a string for informational
+ * messages.
+ *
+ * @param separator char sequence to use as a separator for enum values.
+ * @return <code>{VAL1}{separator}{VAL2}{separator}...</code>
+ */
+ private String joinEnumNames(String separator) {
+ E[] enumTypes = enumClass.getEnumConstants();
+ StringBuilder sb = new StringBuilder();
+ for(int i = 0; i < enumTypes.length; ++i) {
+ if(i > 0) {
+ sb.append(separator);
+ }
+ sb.append(enumTypes[i].name());
+ }
+ return sb.toString();
+ }
+
+}