diff options
Diffstat (limited to 'src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameters')
23 files changed, 718 insertions, 1356 deletions
diff --git a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameters/AbstractParameter.java b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameters/AbstractParameter.java new file mode 100644 index 00000000..ce4b094d --- /dev/null +++ b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameters/AbstractParameter.java @@ -0,0 +1,353 @@ +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) 2012 + 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.security.InvalidParameterException; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +import de.lmu.ifi.dbs.elki.logging.LoggingUtil; +import de.lmu.ifi.dbs.elki.utilities.FormatUtil; +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.constraints.ParameterConstraint; + +/** + * Abstract class for specifying a parameter. + * + * A parameter is defined as an option having a specific value. + * + * See the {@link de.lmu.ifi.dbs.elki.utilities.optionhandling} package for + * documentation! + * + * @author Steffi Wanka + * @author Erich Schubert + * + * @apiviz.composedOf OptionID + * @apiviz.uses ParameterConstraint + * + * @param <T> the type of a possible value (i.e., the type of the option) + */ +public abstract class AbstractParameter<T> implements Parameter<T> { + /** + * The default value of the parameter (may be null). + */ + protected T defaultValue = null; + + /** + * Specifies if the default value of this parameter was taken as parameter + * value. + */ + private boolean defaultValueTaken = false; + + /** + * Specifies if this parameter is an optional parameter. + */ + protected boolean optionalParameter = false; + + /** + * Holds parameter constraints for this parameter. + */ + protected List<ParameterConstraint<? super T>> constraints; + + /** + * The option name. + */ + protected final OptionID optionid; + + /** + * The short description of the option. An extended description is provided by + * the method {@link #getFullDescription()} + */ + protected String shortDescription; + + /** + * The value last passed to this option. + */ + protected T givenValue = null; + + /** + * The value of this option. + */ + private T value; + + /** + * Constructs a parameter with the given optionID, constraints, and default + * value. + * + * @param optionID the unique id of this parameter + * @param defaultValue the default value of this parameter (may be null) + */ + public AbstractParameter(OptionID optionID, T defaultValue) { + this.optionid = optionID; + this.shortDescription = optionID.getDescription(); + this.optionalParameter = true; + this.defaultValue = defaultValue; + } + + /** + * Constructs a parameter with the given optionID, constraints, and optional + * flag. + * + * @param optionID the unique id of this parameter + * @param optional specifies if this parameter is an optional parameter + */ + public AbstractParameter(OptionID optionID, boolean optional) { + this.optionid = optionID; + this.shortDescription = optionID.getDescription(); + this.optionalParameter = optional; + this.defaultValue = null; + } + + /** + * Constructs a parameter with the given optionID, and constraints. + * + * @param optionID the unique id of this parameter + */ + public AbstractParameter(OptionID optionID) { + this(optionID, false); + } + + @Override + public void setDefaultValue(T defaultValue) { + this.defaultValue = defaultValue; + this.optionalParameter = true; + } + + @Override + public boolean hasDefaultValue() { + return !(defaultValue == null); + } + + // TODO: can we do this more elegantly? + @Override + public void useDefaultValue() { + setValueInternal(defaultValue); + defaultValueTaken = true; + } + + @Override + public boolean tryDefaultValue() throws UnspecifiedParameterException { + // Assume default value instead. + if (hasDefaultValue()) { + useDefaultValue(); + return true; + } else if (isOptional()) { + // Optional is fine, but not successful + return false; + } else { + throw new UnspecifiedParameterException(this); + } + } + + @Override + public void setOptional(boolean opt) { + this.optionalParameter = opt; + } + + @Override + public boolean isOptional() { + return this.optionalParameter; + } + + @Override + public boolean tookDefaultValue() { + return defaultValueTaken; + } + + @Override + public boolean isDefined() { + return (this.value != null); + } + + @Override + public T getDefaultValue() { + return defaultValue; + } + + @Override + public boolean hasValuesDescription() { + return false; + } + + @Override + public String getValuesDescription() { + return ""; + } + + @Override + public String getFullDescription() { + StringBuilder description = new StringBuilder(); + // description.append(getParameterType()).append(" "); + description.append(shortDescription); + description.append(FormatUtil.NEWLINE); + if (hasValuesDescription()) { + final String valuesDescription = getValuesDescription(); + description.append(valuesDescription); + if (!valuesDescription.endsWith(FormatUtil.NEWLINE)) { + description.append(FormatUtil.NEWLINE); + } + } + if (hasDefaultValue()) { + description.append("Default: "); + description.append(getDefaultValueAsString()); + description.append(FormatUtil.NEWLINE); + } + if (constraints != null && !constraints.isEmpty()) { + if (constraints.size() == 1) { + description.append("Constraint: "); + } else if (constraints.size() > 1) { + description.append("Constraints: "); + } + for (int i = 0; i < constraints.size(); i++) { + ParameterConstraint<? super T> constraint = constraints.get(i); + if (i > 0) { + description.append(", "); + } + description.append(constraint.getDescription(getName())); + if (i == constraints.size() - 1) { + description.append('.'); + } + } + description.append(FormatUtil.NEWLINE); + } + return description.toString(); + } + + /** + * Validate a value after parsing (e.g. do constrain checks!) + * + * @param obj Object to validate + * @return true iff the object is valid for this parameter. + * @throws ParameterException when the object is not valid. + */ + protected boolean validate(T obj) throws ParameterException { + if (constraints != null) { + for (ParameterConstraint<? super T> cons : this.constraints) { + cons.test(obj); + } + } + return true; + } + + @Override + public OptionID getOptionID() { + return optionid; + } + + @Override + public String getName() { + return optionid.getName(); + } + + @Override + public String getShortDescription() { + return shortDescription; + } + + @Override + public void setShortDescription(String description) { + this.shortDescription = description; + } + + @Override + public void setValue(Object obj) throws ParameterException { + T val = parseValue(obj); + if (validate(val)) { + setValueInternal(val); + } else { + throw new InvalidParameterException("Value for option \"" + getName() + "\" did not validate: " + obj.toString()); + } + } + + /** + * Internal setter for the value. + * + * @param val Value + */ + protected final void setValueInternal(T val) { + this.value = this.givenValue = val; + } + + @Override + public final T getValue() { + if (this.value == null) { + LoggingUtil.warning("Programming error: Parameter#getValue() called for unset parameter \"" + this.optionid.getName() + "\"", new Throwable()); + } + return this.value; + } + + @Override + public Object getGivenValue() { + return this.givenValue; + } + + @Override + public final boolean isValid(Object obj) throws ParameterException { + T val = parseValue(obj); + return validate(val); + } + + @Override + public abstract String getSyntax(); + + /** + * Parse a given value into the destination type. + * + * @param obj Object to parse (may be a string representation!) + * @return Parsed object + * @throws ParameterException when the object cannot be parsed. + */ + protected abstract T parseValue(Object obj) throws ParameterException; + + @Override + public abstract String getValueAsString(); + + @Override + public String getDefaultValueAsString() { + return getDefaultValue().toString(); + } + + @Override + public void addConstraint(ParameterConstraint<? super T> constraint) { + if (constraints == null) { + this.constraints = new ArrayList<ParameterConstraint<? super T>>(1); + } + constraints.add(constraint); + } + + /** + * Add a collection of constraints. + * + * @param cs Constraints to add + */ + public void addConstraints(Collection<? extends ParameterConstraint<? super T>> cs) { + if (constraints == null) { + this.constraints = new ArrayList<ParameterConstraint<? super T>>(cs.size()); + } + constraints.addAll(cs); + } +} diff --git a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameters/ClassListParameter.java b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameters/ClassListParameter.java index 85851e5b..f87e3973 100644 --- a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameters/ClassListParameter.java +++ b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameters/ClassListParameter.java @@ -31,7 +31,6 @@ import de.lmu.ifi.dbs.elki.utilities.FormatUtil; import de.lmu.ifi.dbs.elki.utilities.InspectionUtil; 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.UnusedParameterException; import de.lmu.ifi.dbs.elki.utilities.optionhandling.WrongParameterValueException; import de.lmu.ifi.dbs.elki.utilities.optionhandling.parameterization.Parameterization; @@ -85,10 +84,9 @@ public class ClassListParameter<C> extends ListParameter<Class<? extends C>> { this.restrictionClass = (Class<C>) restrictionClass; } - /** {@inheritDoc} */ @Override public String getValueAsString() { - StringBuffer buf = new StringBuffer(); + StringBuilder buf = new StringBuilder(); final String defPackage = restrictionClass.getPackage().getName() + "."; for(Class<? extends C> c : getValue()) { if(buf.length() > 0) { @@ -103,7 +101,6 @@ public class ClassListParameter<C> extends ListParameter<Class<? extends C>> { return buf.toString(); } - /** {@inheritDoc} */ @SuppressWarnings("unchecked") @Override protected List<Class<? extends C>> parseValue(Object obj) throws ParameterException { @@ -137,7 +134,7 @@ public class ClassListParameter<C> extends ListParameter<Class<? extends C>> { String[] classes = SPLIT.split((String) obj); // TODO: allow empty lists (and list constraints) to enforce length? if(classes.length == 0) { - throw new UnspecifiedParameterException("Wrong parameter format! Given list of classes for parameter \"" + getName() + "\" is either empty or has the wrong format!"); + throw new WrongParameterValueException("Wrong parameter format! Given list of classes for parameter \"" + getName() + "\" is either empty or has the wrong format!"); } List<Class<? extends C>> cls = new ArrayList<Class<? extends C>>(classes.length); @@ -169,7 +166,6 @@ public class ClassListParameter<C> extends ListParameter<Class<? extends C>> { throw new WrongParameterValueException("Wrong parameter format! Parameter \"" + getName() + "\" requires a list of Class values!"); } - /** {@inheritDoc} */ @Override protected boolean validate(List<Class<? extends C>> obj) throws ParameterException { for(Class<? extends C> cls : obj) { @@ -282,7 +278,7 @@ public class ClassListParameter<C> extends ListParameter<Class<? extends C>> { /** * This class sometimes provides a list of value descriptions. * - * @see de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.Parameter#hasValuesDescription() + * @see de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.AbstractParameter#hasValuesDescription() */ @Override public boolean hasValuesDescription() { @@ -292,7 +288,7 @@ public class ClassListParameter<C> extends ListParameter<Class<? extends C>> { /** * Return a description of known valid classes. * - * @see de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.Parameter#getValuesDescription() + * @see de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.AbstractParameter#getValuesDescription() */ @Override public String getValuesDescription() { diff --git a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameters/ClassParameter.java b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameters/ClassParameter.java index 6b056c03..6d63f9b5 100644 --- a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameters/ClassParameter.java +++ b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameters/ClassParameter.java @@ -49,7 +49,7 @@ import de.lmu.ifi.dbs.elki.utilities.optionhandling.parameterization.Parameteriz */ // TODO: add additional constructors with parameter constraints. // TODO: turn restrictionClass into a constraint? -public class ClassParameter<C> extends Parameter<Class<?>, Class<? extends C>> { +public class ClassParameter<C> extends AbstractParameter<Class<? extends C>> { /** * Class loader. */ @@ -83,7 +83,7 @@ public class ClassParameter<C> extends Parameter<Class<?>, Class<? extends C>> { // * ClassParameter<Foo<Bar>>(optionID, (Class<Foo<Bar>>) Foo.class) is an // invalid cast. this.restrictionClass = (Class<C>) restrictionClass; - if(restrictionClass == null) { + if (restrictionClass == null) { LoggingUtil.warning("Restriction class 'null' for parameter '" + optionID + "'", new Throwable()); } } @@ -106,7 +106,7 @@ public class ClassParameter<C> extends Parameter<Class<?>, Class<? extends C>> { // * ClassParameter<Foo<Bar>>(optionID, (Class<Foo<Bar>>) Foo.class) is an // invalid cast. this.restrictionClass = (Class<C>) restrictionClass; - if(restrictionClass == null) { + if (restrictionClass == null) { LoggingUtil.warning("Restriction class 'null' for parameter '" + optionID + "'", new Throwable()); } } @@ -122,43 +122,38 @@ public class ClassParameter<C> extends Parameter<Class<?>, Class<? extends C>> { this(optionID, restrictionClass, false); } - /** {@inheritDoc} */ @SuppressWarnings("unchecked") @Override protected Class<? extends C> parseValue(Object obj) throws ParameterException { - if(obj == null) { - throw new UnspecifiedParameterException("Parameter Error.\n" + "No value for parameter \"" + getName() + "\" " + "given."); + if (obj == null) { + throw new UnspecifiedParameterException(this); } - if(obj instanceof Class<?>) { + if (obj instanceof Class<?>) { return (Class<? extends C>) obj; } - if(obj instanceof String) { + if (obj instanceof String) { String value = (String) obj; try { // Try exact class factory first. try { return (Class<? extends C>) loader.loadClass(value + FACTORY_POSTFIX); - } - catch(ClassNotFoundException e) { + } catch (ClassNotFoundException e) { // Ignore, retry } try { return (Class<? extends C>) loader.loadClass(value); - } - catch(ClassNotFoundException e) { + } catch (ClassNotFoundException e) { // Ignore, retry } // Try factory for guessed name next try { return (Class<? extends C>) loader.loadClass(restrictionClass.getPackage().getName() + "." + value + FACTORY_POSTFIX); - } - catch(ClassNotFoundException e) { + } catch (ClassNotFoundException e) { // Ignore, retry } // Last try: guessed name prefix only return (Class<? extends C>) loader.loadClass(restrictionClass.getPackage().getName() + "." + value); - } - catch(ClassNotFoundException e) { + } catch (ClassNotFoundException e) { throw new WrongParameterValueException(this, value, "Given class \"" + value + "\" not found.", e); } } @@ -171,13 +166,13 @@ public class ClassParameter<C> extends Parameter<Class<?>, Class<? extends C>> { */ @Override public boolean validate(Class<? extends C> obj) throws ParameterException { - if(obj == null) { - throw new UnspecifiedParameterException("Parameter Error.\n" + "No value for parameter \"" + getName() + "\" " + "given."); + if (obj == null) { + throw new UnspecifiedParameterException(this); } - if(!restrictionClass.isAssignableFrom(obj)) { + if (!restrictionClass.isAssignableFrom(obj)) { throw new WrongParameterValueException(this, obj.getName(), "Given class not a subclass / implementation of " + restrictionClass.getName()); } - if(!super.validate(obj)) { + if (!super.validate(obj)) { return false; } return true; @@ -196,7 +191,7 @@ public class ClassParameter<C> extends Parameter<Class<?>, Class<? extends C>> { /** * This class sometimes provides a list of value descriptions. * - * @see de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.Parameter#hasValuesDescription() + * @see de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.AbstractParameter#hasValuesDescription() */ @Override public boolean hasValuesDescription() { @@ -206,11 +201,11 @@ public class ClassParameter<C> extends Parameter<Class<?>, Class<? extends C>> { /** * Return a description of known valid classes. * - * @see de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.Parameter#getValuesDescription() + * @see de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.AbstractParameter#getValuesDescription() */ @Override public String getValuesDescription() { - if(restrictionClass != null && restrictionClass != Object.class) { + if (restrictionClass != null && restrictionClass != Object.class) { return restrictionString(); } return ""; @@ -234,28 +229,22 @@ public class ClassParameter<C> extends Parameter<Class<?>, Class<? extends C>> { */ public C instantiateClass(Parameterization config) { try { - if(getValue() == null /* && !optionalParameter */) { + if (getValue() == null /* && !optionalParameter */) { throw new UnusedParameterException("Value of parameter " + getName() + " has not been specified."); } C instance; try { config = config.descend(this); instance = ClassGenericsUtil.tryInstantiate(restrictionClass, getValue(), config); - } - catch(InvocationTargetException e) { - // inner exception during instantiation. Log, so we don't lose it! - LoggingUtil.exception(e); + } catch (InvocationTargetException e) { throw new WrongParameterValueException(this, getValue().getCanonicalName(), "Error instantiating class.", e); - } - catch(NoSuchMethodException e) { + } catch (NoSuchMethodException e) { throw new WrongParameterValueException(this, getValue().getCanonicalName(), "Error instantiating class - no usable public constructor."); - } - catch(Exception e) { + } catch (Exception e) { throw new WrongParameterValueException(this, getValue().getCanonicalName(), "Error instantiating class.", e); } return instance; - } - catch(ParameterException e) { + } catch (ParameterException e) { config.reportError(e); return null; } @@ -288,20 +277,19 @@ public class ClassParameter<C> extends Parameter<Class<?>, Class<? extends C>> { */ public String restrictionString() { StringBuilder info = new StringBuilder(); - if(restrictionClass.isInterface()) { + if (restrictionClass.isInterface()) { info.append("Implementing "); - } - else { + } else { info.append("Extending "); } info.append(restrictionClass.getName()); info.append(FormatUtil.NEWLINE); List<Class<?>> known = getKnownImplementations(); - if(!known.isEmpty()) { + if (!known.isEmpty()) { info.append("Known classes (default package " + restrictionClass.getPackage().getName() + "):"); info.append(FormatUtil.NEWLINE); - for(Class<?> c : known) { + for (Class<?> c : known) { info.append("->" + FormatUtil.NONBREAKING_SPACE); info.append(canonicalClassName(c, getRestrictionClass())); info.append(FormatUtil.NEWLINE); @@ -321,13 +309,13 @@ public class ClassParameter<C> extends Parameter<Class<?>, Class<? extends C>> { */ public static String canonicalClassName(Class<?> c, Package pkg, String postfix) { String name = c.getName(); - if(pkg != null) { + if (pkg != null) { String prefix = pkg.getName() + "."; - if(name.startsWith(prefix)) { + if (name.startsWith(prefix)) { name = name.substring(prefix.length()); } } - if(postfix != null && name.endsWith(postfix)) { + if (postfix != null && name.endsWith(postfix)) { name = name.substring(0, name.length() - postfix.length()); } return name; @@ -341,7 +329,7 @@ public class ClassParameter<C> extends Parameter<Class<?>, Class<? extends C>> { * @return Simplified class name. */ public static String canonicalClassName(Class<?> c, Class<?> parent) { - if(parent == null) { + if (parent == null) { return canonicalClassName(c, null, FACTORY_POSTFIX); } return canonicalClassName(c, parent.getPackage(), FACTORY_POSTFIX); @@ -351,4 +339,4 @@ public class ClassParameter<C> extends Parameter<Class<?>, Class<? extends C>> { public String getDefaultValueAsString() { return canonicalClassName(getDefaultValue(), getRestrictionClass()); } -}
\ No newline at end of file +} diff --git a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameters/DistanceParameter.java b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameters/DistanceParameter.java index bb9dc60c..f9943610 100644 --- a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameters/DistanceParameter.java +++ b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameters/DistanceParameter.java @@ -23,13 +23,10 @@ package de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters; along with this program. If not, see <http://www.gnu.org/licenses/>. */ -import java.util.List; - import de.lmu.ifi.dbs.elki.distance.distancefunction.DistanceFunction; import de.lmu.ifi.dbs.elki.distance.distancevalue.Distance; import de.lmu.ifi.dbs.elki.utilities.optionhandling.OptionID; import de.lmu.ifi.dbs.elki.utilities.optionhandling.WrongParameterValueException; -import de.lmu.ifi.dbs.elki.utilities.optionhandling.constraints.ParameterConstraint; /** * Parameter class for a parameter specifying a double value. @@ -39,177 +36,11 @@ import de.lmu.ifi.dbs.elki.utilities.optionhandling.constraints.ParameterConstra * * @param <D> Distance type */ -public class DistanceParameter<D extends Distance<D>> extends Parameter<D, D> { +public class DistanceParameter<D extends Distance<D>> extends AbstractParameter<D> { /** * Distance type */ D dist; - - /** - * Constructs a double parameter with the given optionID, parameter - * constraints, and default value. - * - * @param optionID the unique optionID - * @param dist distance factory - * @param cons a list of parameter constraints for this double parameter - * @param defaultValue the default value for this double parameter - */ - public DistanceParameter(OptionID optionID, D dist, List<ParameterConstraint<D>> cons, D defaultValue) { - super(optionID, cons, defaultValue); - this.dist = dist; - } - - /** - * Constructs a double parameter with the given optionID, parameter - * constraints, and default value. - * - * @param optionID the unique optionID - * @param dist distance factory - * @param cons a list of parameter constraints for this double parameter - * @param defaultValue the default value for this double parameter - */ - public DistanceParameter(OptionID optionID, DistanceFunction<?, D> dist, List<ParameterConstraint<D>> cons, D defaultValue) { - super(optionID, cons, defaultValue); - this.dist = (dist != null) ? dist.getDistanceFactory() : null; - } - - /** - * Constructs a double parameter with the given optionID, parameter - * constraints, and optional flag. - * - * @param optionID the unique optionID - * @param dist distance factory - * @param cons a list of parameter constraints for this double parameter - * @param optional specifies whether this parameter is an optional parameter - */ - public DistanceParameter(OptionID optionID, D dist, List<ParameterConstraint<D>> cons, boolean optional) { - this(optionID, dist, cons); - setOptional(optional); - } - - /** - * Constructs a double parameter with the given optionID, parameter - * constraints, and optional flag. - * - * @param optionID the unique optionID - * @param dist distance factory - * @param cons a list of parameter constraints for this double parameter - * @param optional specifies whether this parameter is an optional parameter - */ - public DistanceParameter(OptionID optionID, DistanceFunction<?, D> dist, List<ParameterConstraint<D>> cons, boolean optional) { - this(optionID, dist, cons); - setOptional(optional); - } - - /** - * Constructs a double parameter with the given optionID, and parameter - * constraints. - * - * @param optionID the unique optionID - * @param dist distance factory - * @param constraints a list of parameter constraints for this double - * parameter - */ - public DistanceParameter(OptionID optionID, D dist, List<ParameterConstraint<D>> constraints) { - super(optionID, constraints); - this.dist = dist; - } - - /** - * Constructs a double parameter with the given optionID, and parameter - * constraints. - * - * @param optionID the unique optionID - * @param dist distance factory - * @param constraints a list of parameter constraints for this double - * parameter - */ - public DistanceParameter(OptionID optionID, DistanceFunction<?, D> dist, List<ParameterConstraint<D>> constraints) { - super(optionID, constraints); - this.dist = (dist != null) ? dist.getDistanceFactory() : null; - } - - /** - * Constructs a double parameter with the given optionID, parameter - * constraint, and default value. - * - * @param optionID the unique id of this parameter - * @param dist distance factory - * @param constraint the constraint of this parameter - * @param defaultValue the default value for this parameter - */ - public DistanceParameter(OptionID optionID, D dist, ParameterConstraint<D> constraint, D defaultValue) { - super(optionID, constraint, defaultValue); - this.dist = dist; - } - - /** - * Constructs a double parameter with the given optionID, parameter - * constraint, and default value. - * - * @param optionID the unique id of this parameter - * @param dist distance factory - * @param constraint the constraint of this parameter - * @param defaultValue the default value for this parameter - */ - public DistanceParameter(OptionID optionID, DistanceFunction<?, D> dist, ParameterConstraint<D> constraint, D defaultValue) { - super(optionID, constraint, defaultValue); - this.dist = (dist != null) ? dist.getDistanceFactory() : null; - } - - /** - * Constructs a double parameter with the given optionID, parameter - * constraint, and optional flag. - * - * @param optionID the unique id of this parameter - * @param dist distance factory - * @param constraint the constraint of this parameter - * @param optional specifies whether this parameter is an optional parameter - */ - public DistanceParameter(OptionID optionID, D dist, ParameterConstraint<D> constraint, boolean optional) { - super(optionID, constraint, optional); - this.dist = dist; - } - - /** - * Constructs a double parameter with the given optionID, parameter - * constraint, and optional flag. - * - * @param optionID the unique id of this parameter - * @param dist distance factory - * @param constraint the constraint of this parameter - * @param optional specifies whether this parameter is an optional parameter - */ - public DistanceParameter(OptionID optionID, DistanceFunction<?, D> dist, ParameterConstraint<D> constraint, boolean optional) { - super(optionID, constraint, optional); - this.dist = (dist != null) ? dist.getDistanceFactory() : null; - } - - /** - * Constructs a double parameter with the given optionID, and parameter - * constraint. - * - * @param optionID the unique id of this parameter - * @param dist distance factory - * @param constraint the constraint of this parameter - */ - public DistanceParameter(OptionID optionID, D dist, ParameterConstraint<D> constraint) { - super(optionID, constraint); - this.dist = dist; - } - - /** - * Constructs a double parameter with the given optionID, and parameter - * constraint. - * - * @param optionID the unique id of this parameter - * @param dist distance factory - * @param constraint the constraint of this parameter - */ - public DistanceParameter(OptionID optionID, DistanceFunction<?, D> dist, ParameterConstraint<D> constraint) { - super(optionID, constraint); - this.dist = (dist != null) ? dist.getDistanceFactory() : null; - } /** * Constructs a double parameter with the given optionID and default value. diff --git a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameters/DoubleListParameter.java b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameters/DoubleListParameter.java index 07b10b83..885a3bfc 100644 --- a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameters/DoubleListParameter.java +++ b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameters/DoubleListParameter.java @@ -30,7 +30,6 @@ import de.lmu.ifi.dbs.elki.utilities.FormatUtil; 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.WrongParameterValueException; -import de.lmu.ifi.dbs.elki.utilities.optionhandling.constraints.ParameterConstraint; /** * Parameter class for a parameter specifying a list of double values. @@ -40,72 +39,6 @@ import de.lmu.ifi.dbs.elki.utilities.optionhandling.constraints.ParameterConstra */ public class DoubleListParameter extends ListParameter<Double> { /** - * Constructs a list parameter with the given optionID. - * - * @param optionID Option ID - * @param constraints Constraints - * @param defaultValue Default value - */ - public DoubleListParameter(OptionID optionID, List<ParameterConstraint<List<Double>>> constraints, List<Double> defaultValue) { - super(optionID, constraints, defaultValue); - } - - /** - * Constructs a list parameter with the given optionID. - * - * @param optionID Option ID - * @param constraints Constraints - * @param optional Optional flag - */ - public DoubleListParameter(OptionID optionID, List<ParameterConstraint<List<Double>>> constraints, boolean optional) { - super(optionID, constraints, optional); - } - - /** - * Constructs a list parameter with the given optionID. - * - * @param optionID Option ID - * @param constraints Constraints - */ - /* - * public DoubleListParameter(OptionID optionID, - * List<ParameterConstraint<List<Double>>> constraints) { super(optionID, - * constraints); } - */ - - /** - * Constructs a list parameter with the given optionID. - * - * @param optionID Option ID - * @param constraint Constraint - * @param defaultValue Default value - */ - public DoubleListParameter(OptionID optionID, ParameterConstraint<List<Double>> constraint, List<Double> defaultValue) { - super(optionID, constraint, defaultValue); - } - - /** - * Constructs a list parameter with the given optionID. - * - * @param optionID Option ID - * @param constraint Constraint - * @param optional Optional flag - */ - public DoubleListParameter(OptionID optionID, ParameterConstraint<List<Double>> constraint, boolean optional) { - super(optionID, constraint, optional); - } - - /** - * Constructs a list parameter with the given optionID. - * - * @param optionID Option ID - * @param constraint Constraint - */ - public DoubleListParameter(OptionID optionID, ParameterConstraint<List<Double>> constraint) { - super(optionID, constraint); - } - - /** * Constructs a list parameter with the given optionID and optional flag. * * @param optionID Option ID @@ -126,7 +59,7 @@ public class DoubleListParameter extends ListParameter<Double> { @Override public String getValueAsString() { - return FormatUtil.format(getValue().toArray(new Double[0]), LIST_SEP, FormatUtil.NF); + return FormatUtil.format(getValue().toArray(new Double[getValue().size()]), LIST_SEP, FormatUtil.NF); } @SuppressWarnings("unchecked") @@ -150,7 +83,7 @@ public class DoubleListParameter extends ListParameter<Double> { String[] values = SPLIT.split((String) obj); ArrayList<Double> doubleValue = new ArrayList<Double>(values.length); for(String val : values) { - doubleValue.add(Double.parseDouble(val)); + doubleValue.add(Double.valueOf(val)); } return doubleValue; } diff --git a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameters/DoubleParameter.java b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameters/DoubleParameter.java index 9ebb61ee..4aee3511 100644 --- a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameters/DoubleParameter.java +++ b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameters/DoubleParameter.java @@ -23,8 +23,6 @@ package de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters; along with this program. If not, see <http://www.gnu.org/licenses/>. */ -import java.util.List; - import de.lmu.ifi.dbs.elki.utilities.optionhandling.OptionID; import de.lmu.ifi.dbs.elki.utilities.optionhandling.WrongParameterValueException; import de.lmu.ifi.dbs.elki.utilities.optionhandling.constraints.ParameterConstraint; @@ -38,63 +36,17 @@ import de.lmu.ifi.dbs.elki.utilities.optionhandling.constraints.ParameterConstra public class DoubleParameter extends NumberParameter<Double> { /** * Constructs a double parameter with the given optionID, parameter - * constraints, and default value. - * - * @param optionID the unique optionID - * @param cons a list of parameter constraints for this double parameter - * @param defaultValue the default value for this double parameter - */ - public DoubleParameter(OptionID optionID, List<ParameterConstraint<Number>> cons, Double defaultValue) { - super(optionID, cons, defaultValue); - } - - /** - * Constructs a double parameter with the given optionID, parameter - * constraints, and optional flag. - * - * @param optionID the unique optionID - * @param cons a list of parameter constraints for this double parameter - * @param optional specifies whether this parameter is an optional parameter - */ - public DoubleParameter(OptionID optionID, List<ParameterConstraint<Number>> cons, boolean optional) { - this(optionID, cons); - setOptional(optional); - } - - /** - * Constructs a double parameter with the given optionID, and parameter - * constraints. - * - * @param optionID the unique optionID - * @param constraints a list of parameter constraints for this double - * parameter - */ - public DoubleParameter(OptionID optionID, List<ParameterConstraint<Number>> constraints) { - super(optionID, constraints); - } - - /** - * Constructs a double parameter with the given optionID, parameter * constraint, and default value. * * @param optionID the unique id of this parameter - * @param constraint the constraint of this parameter * @param defaultValue the default value for this parameter - */ - public DoubleParameter(OptionID optionID, ParameterConstraint<Number> constraint, Double defaultValue) { - super(optionID, constraint, defaultValue); - } - - /** - * Constructs a double parameter with the given optionID, parameter - * constraint, and optional flag. - * - * @param optionID the unique id of this parameter * @param constraint the constraint of this parameter - * @param optional specifies whether this parameter is an optional parameter + * @deprecated Use {@link #addConstraint} instead. */ - public DoubleParameter(OptionID optionID, ParameterConstraint<Number> constraint, boolean optional) { - super(optionID, constraint, optional); + @Deprecated + public DoubleParameter(OptionID optionID, double defaultValue, ParameterConstraint<Number> constraint) { + super(optionID, defaultValue); + addConstraint(constraint); } /** @@ -103,9 +55,12 @@ public class DoubleParameter extends NumberParameter<Double> { * * @param optionID the unique id of this parameter * @param constraint the constraint of this parameter + * @deprecated Use {@link #addConstraint} instead. */ + @Deprecated public DoubleParameter(OptionID optionID, ParameterConstraint<Number> constraint) { - super(optionID, constraint); + super(optionID); + addConstraint(constraint); } /** @@ -114,18 +69,22 @@ public class DoubleParameter extends NumberParameter<Double> { * @param optionID the unique optionID * @param defaultValue the default value for this double parameter */ - public DoubleParameter(OptionID optionID, Double defaultValue) { + public DoubleParameter(OptionID optionID, double defaultValue) { super(optionID, defaultValue); } /** - * Constructs a double parameter with the given optionID and optional flag. + * Constructs a double parameter with the given optionID and default value. * - * @param optionID the unique id of this parameter - * @param optional specifies whether this parameter is an optional parameter + * @param optionID the unique optionID + * @param optional Flag to indicate that the parameter is optional + * + * @deprecated Use {@link #setOptional} instead. */ + @Deprecated public DoubleParameter(OptionID optionID, boolean optional) { - super(optionID, optional); + super(optionID); + setOptional(optional); } /** @@ -137,53 +96,26 @@ public class DoubleParameter extends NumberParameter<Double> { super(optionID); } - /** {@inheritDoc} */ @Override public String getValueAsString() { - return Double.toString(getValue()); + return getValue().toString(); } - /** {@inheritDoc} */ @Override protected Double parseValue(Object obj) throws WrongParameterValueException { - if(obj instanceof Double) { + if (obj instanceof Double) { return (Double) obj; } try { - return Double.parseDouble(obj.toString()); - } - catch(NullPointerException e) { + return Double.valueOf(obj.toString()); + } catch (NullPointerException e) { throw new WrongParameterValueException("Wrong parameter format! Parameter \"" + getName() + "\" requires a double value, read: " + obj + "!\n"); - } - catch(NumberFormatException e) { + } catch (NumberFormatException e) { throw new WrongParameterValueException("Wrong parameter format! Parameter \"" + getName() + "\" requires a double value, read: " + obj + "!\n"); } } /** - * Indicates whether some other object is "equal to" this one. - * - * @param obj the reference object with which to compare. - * @return <code>true</code> if this double parameter has the same value as - * the specified object, <code>false</code> otherwise. - */ - // TODO: comparing the parameters doesn't make sense. REMOVE. - /*@Override - public boolean equals(Object obj) { - if(obj == this) { - return true; - } - if(!(obj instanceof DoubleParameter)) { - return false; - } - DoubleParameter oth = (DoubleParameter) obj; - if(this.getValue() == null) { - return (oth.getValue() == null); - } - return this.getValue().equals(oth.getValue()); - }*/ - - /** * Returns a string representation of the parameter's type. * * @return "<double>" @@ -192,4 +124,13 @@ public class DoubleParameter extends NumberParameter<Double> { public String getSyntax() { return "<double>"; } + + /** + * Get the parameter value as double. + * + * @return double value + */ + public double doubleValue() { + return getValue().doubleValue(); + } } 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 index afab86c8..c2e1c733 100644 --- a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameters/EnumParameter.java +++ b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameters/EnumParameter.java @@ -62,7 +62,7 @@ import de.lmu.ifi.dbs.elki.utilities.optionhandling.WrongParameterValueException *
* @param <E> Enum type
*/
-public class EnumParameter<E extends Enum<E>> extends Parameter<Enum<E>, E> {
+public class EnumParameter<E extends Enum<E>> extends AbstractParameter<E> {
/**
* Reference to the actual enum type, for T.valueOf().
@@ -112,7 +112,7 @@ public class EnumParameter<E extends Enum<E>> extends Parameter<Enum<E>, E> { @Override
protected E parseValue(Object obj) throws ParameterException {
if(obj == null) {
- throw new UnspecifiedParameterException("Parameter \"" + getName() + "\": Null value given!");
+ throw new UnspecifiedParameterException(this);
}
if(obj instanceof String) {
try {
diff --git a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameters/FileListParameter.java b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameters/FileListParameter.java index 1b72df80..203712aa 100644 --- a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameters/FileListParameter.java +++ b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameters/FileListParameter.java @@ -75,10 +75,9 @@ public class FileListParameter extends ListParameter<File> { // TODO: Add remaining constructors. - /** {@inheritDoc} */ @Override public String getValueAsString() { - StringBuffer buf = new StringBuffer(); + StringBuilder buf = new StringBuilder(); List<File> val = getValue(); Iterator<File> veciter = val.iterator(); while(veciter.hasNext()) { @@ -90,7 +89,6 @@ public class FileListParameter extends ListParameter<File> { return buf.toString(); } - /** {@inheritDoc} */ @SuppressWarnings("unchecked") @Override protected List<File> parseValue(Object obj) throws ParameterException { @@ -119,7 +117,6 @@ public class FileListParameter extends ListParameter<File> { throw new WrongParameterValueException("Wrong parameter format! Parameter \"" + getName() + "\" requires a list of file values!"); } - /** {@inheritDoc} */ @Override protected boolean validate(List<File> obj) throws ParameterException { if(!super.validate(obj)) { diff --git a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameters/FileParameter.java b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameters/FileParameter.java index 1e04c7ae..2cc5ab37 100644 --- a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameters/FileParameter.java +++ b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameters/FileParameter.java @@ -38,7 +38,7 @@ import de.lmu.ifi.dbs.elki.utilities.optionhandling.WrongParameterValueException * @author Erich Schubert */ // TODO: turn FileType into a Constraint? -public class FileParameter extends Parameter<File, File> { +public class FileParameter extends AbstractParameter<File> { /** * Available file types: {@link #INPUT_FILE} denotes an input file, * {@link #OUTPUT_FILE} denotes an output file. @@ -86,7 +86,6 @@ public class FileParameter extends Parameter<File, File> { setOptional(optional); } - /** {@inheritDoc} */ @Override public String getValueAsString() { try { @@ -97,11 +96,10 @@ public class FileParameter extends Parameter<File, File> { } } - /** {@inheritDoc} */ @Override protected File parseValue(Object obj) throws ParameterException { if(obj == null) { - throw new UnspecifiedParameterException("Parameter \"" + getName() + "\": No filename given!"); + throw new UnspecifiedParameterException(this); } if(obj instanceof File) { return (File) obj; @@ -109,10 +107,9 @@ public class FileParameter extends Parameter<File, File> { if(obj instanceof String) { return new File((String) obj); } - throw new UnspecifiedParameterException("Parameter \"" + getName() + "\": Unsupported value given!"); + throw new WrongParameterValueException("Parameter \"" + getName() + "\": Unsupported value given!"); } - /** {@inheritDoc} */ @Override protected boolean validate(File obj) throws ParameterException { if(!super.validate(obj)) { diff --git a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameters/Flag.java b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameters/Flag.java index e2ce63c2..90230077 100644 --- a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameters/Flag.java +++ b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameters/Flag.java @@ -37,7 +37,7 @@ import de.lmu.ifi.dbs.elki.utilities.optionhandling.WrongParameterValueException * @author Steffi Wanka * @author Erich Schubert */ -public class Flag extends Parameter<Boolean, Boolean> { +public class Flag extends AbstractParameter<Boolean> { /** * Constant indicating that the flag is set. */ @@ -58,25 +58,25 @@ public class Flag extends Parameter<Boolean, Boolean> { public Flag(OptionID optionID) { super(optionID); setOptional(true); - setDefaultValue(false); + setDefaultValue(Boolean.FALSE); } @Override protected Boolean parseValue(Object obj) throws ParameterException { if(SET.equals(obj)) { - return true; + return Boolean.TRUE; } if(NOT_SET.equals(obj)) { - return false; + return Boolean.FALSE; } if(obj instanceof Boolean) { return (Boolean) obj; } if(obj != null && SET.equals(obj.toString())) { - return true; + return Boolean.TRUE; } if(obj != null && NOT_SET.equals(obj.toString())) { - return false; + return Boolean.FALSE; } throw new WrongParameterValueException("Wrong value for flag \"" + getName() + "\". Allowed values:\n" + SET + " or " + NOT_SET); } @@ -89,15 +89,11 @@ public class Flag extends Parameter<Boolean, Boolean> { return "<|" + SET + "|" + NOT_SET + ">"; } - /** {@inheritDoc} */ @Override public String getValueAsString() { - return getValue() ? SET : NOT_SET; + return getValue().booleanValue() ? SET : NOT_SET; } - /** - * {@inheritDoc} - */ @Override protected boolean validate(Boolean obj) throws ParameterException { if(obj == null) { @@ -114,7 +110,7 @@ public class Flag extends Parameter<Boolean, Boolean> { */ public void setValue(boolean val) { try { - super.setValue(val); + super.setValue(Boolean.valueOf(val)); } catch(ParameterException e) { // We're pretty sure that any Boolean is okay, so this should never be @@ -129,7 +125,7 @@ public class Flag extends Parameter<Boolean, Boolean> { * @return true when defined and true. */ public boolean isTrue() { - return isDefined() && getValue(); + return isDefined() && getValue().booleanValue(); } /** @@ -138,6 +134,6 @@ public class Flag extends Parameter<Boolean, Boolean> { * @return true when defined and true. */ public boolean isFalse() { - return isDefined() && !getValue(); + return isDefined() && !getValue().booleanValue(); } }
\ No newline at end of file diff --git a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameters/IntListParameter.java b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameters/IntListParameter.java index d7b5d570..f61f8842 100644 --- a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameters/IntListParameter.java +++ b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameters/IntListParameter.java @@ -30,7 +30,6 @@ import java.util.List; 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.WrongParameterValueException; -import de.lmu.ifi.dbs.elki.utilities.optionhandling.constraints.ParameterConstraint; /** * Parameter class for a parameter specifying a list of integer values. @@ -43,80 +42,6 @@ public class IntListParameter extends ListParameter<Integer> { * Constructs an integer list parameter * * @param optionID the unique id of this parameter - * @param constraints the constraints of this parameter, may be null - * @param defaultValue the default value - */ - public IntListParameter(OptionID optionID, List<ParameterConstraint<List<Integer>>> constraints, List<Integer> defaultValue) { - super(optionID, constraints, defaultValue); - } - - /** - * Constructs an integer list parameter - * - * @param optionID the unique id of this parameter - * @param constraints the constraints of this parameter, may be null - * @param optional specifies if this parameter is an optional parameter - */ - public IntListParameter(OptionID optionID, List<ParameterConstraint<List<Integer>>> constraints, boolean optional) { - super(optionID, constraints, optional); - } - - /** - * Constructs an integer list parameter - * - * @param optionID the unique id of this parameter - * @param constraints the constraints of this parameter, may be null - */ - /*public IntListParameter(OptionID optionID, List<ParameterConstraint<List<Integer>>> constraints) { - super(optionID, constraints); - } */ - - /** - * Constructs an integer list parameter - * - * @param optionID the unique id of this parameter - * @param constraint the constraint of this parameter, may be null - * @param defaultValue the default value - */ - public IntListParameter(OptionID optionID, ParameterConstraint<List<Integer>> constraint, List<Integer> defaultValue) { - super(optionID, constraint, defaultValue); - } - - /** - * Constructs an integer list parameter - * - * @param optionID the unique id of this parameter - * @param constraint the constraint of this parameter, may be null - * @param optional specifies if this parameter is an optional parameter - */ - public IntListParameter(OptionID optionID, ParameterConstraint<List<Integer>> constraint, boolean optional) { - super(optionID, constraint, optional); - } - - /** - * Constructs an integer list parameter - * - * @param optionID the unique id of this parameter - * @param constraint the constraint of this parameter, may be null - */ - public IntListParameter(OptionID optionID, ParameterConstraint<List<Integer>> constraint) { - super(optionID, constraint); - } - - /** - * Constructs an integer list parameter - * - * @param optionID the unique id of this parameter - * @param defaultValue the default value - */ - /*public IntListParameter(OptionID optionID, List<Integer> defaultValue) { - super(optionID, defaultValue); - }*/ - - /** - * Constructs an integer list parameter - * - * @param optionID the unique id of this parameter * @param optional specifies if this parameter is an optional parameter */ public IntListParameter(OptionID optionID, boolean optional) { @@ -132,14 +57,13 @@ public class IntListParameter extends ListParameter<Integer> { super(optionID); } - /** {@inheritDoc} */ @Override public String getValueAsString() { - StringBuffer buf = new StringBuffer(); + StringBuilder buf = new StringBuilder(); List<Integer> val = getValue(); Iterator<Integer> veciter = val.iterator(); while(veciter.hasNext()) { - buf.append(Integer.toString(veciter.next())); + buf.append(veciter.next().toString()); if (veciter.hasNext()) { buf.append(LIST_SEP); } @@ -147,7 +71,6 @@ public class IntListParameter extends ListParameter<Integer> { return buf.toString(); } - /** {@inheritDoc} */ @SuppressWarnings("unchecked") @Override protected List<Integer> parseValue(Object obj) throws ParameterException { @@ -169,7 +92,7 @@ public class IntListParameter extends ListParameter<Integer> { String[] values = SPLIT.split((String) obj); ArrayList<Integer> intValue = new ArrayList<Integer>(values.length); for(String val : values) { - intValue.add(Integer.parseInt(val)); + intValue.add(Integer.valueOf(val)); } return intValue; } @@ -177,19 +100,6 @@ public class IntListParameter extends ListParameter<Integer> { } /** - * Sets the default value of this parameter. - * - * @param allListDefaultValue default value for all list elements of this - * parameter - */ - // unused? - /*public void setDefaultValue(int allListDefaultValue) { - for(int i = 0; i < defaultValue.size(); i++) { - defaultValue.set(i, allListDefaultValue); - } - }*/ - - /** * Returns a string representation of the parameter's type. * * @return "<int_1,...,int_n>" diff --git a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameters/IntParameter.java b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameters/IntParameter.java index 4e494fda..22823e16 100644 --- a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameters/IntParameter.java +++ b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameters/IntParameter.java @@ -23,8 +23,6 @@ package de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters; along with this program. If not, see <http://www.gnu.org/licenses/>. */ -import java.util.List; - 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.WrongParameterValueException; @@ -42,46 +40,14 @@ public class IntParameter extends NumberParameter<Integer> { * constraint, and default value. * * @param optionID optionID the unique id of the option - * @param constraints the constraint for this integer parameter * @param defaultValue the default value - */ - public IntParameter(OptionID optionID, List<ParameterConstraint<Number>> constraints, Integer defaultValue) { - super(optionID, constraints, defaultValue); - } - - /** - * Constructs an integer parameter with the given optionID, parameter - * constraint, and optional flag. - * - * @param optionID optionID the unique id of the option - * @param constraints the constraint for this integer parameter - * @param optional specifies if this parameter is an optional parameter - */ - public IntParameter(OptionID optionID, List<ParameterConstraint<Number>> constraints, boolean optional) { - super(optionID, constraints, optional); - } - - /** - * Constructs an integer parameter with the given optionID, and parameter - * constraint. - * - * @param optionID optionID the unique id of the option - * @param constraints the constraint for this integer parameter - */ - public IntParameter(OptionID optionID, List<ParameterConstraint<Number>> constraints) { - super(optionID, constraints); - } - - /** - * Constructs an integer parameter with the given optionID, parameter - * constraint, and default value. - * - * @param optionID optionID the unique id of the option * @param constraint the constraint for this integer parameter - * @param defaultValue the default value + * @deprecated Use {@link #addConstraint} instead. */ - public IntParameter(OptionID optionID, ParameterConstraint<Number> constraint, Integer defaultValue) { - super(optionID, constraint, defaultValue); + @Deprecated + public IntParameter(OptionID optionID, int defaultValue, ParameterConstraint<Number> constraint) { + super(optionID, Integer.valueOf(defaultValue)); + addConstraint(constraint); } /** @@ -91,9 +57,12 @@ public class IntParameter extends NumberParameter<Integer> { * @param optionID optionID the unique id of the option * @param constraint the constraint for this integer parameter * @param optional specifies if this parameter is an optional parameter + * @deprecated Use {@link #addConstraint} instead. */ + @Deprecated public IntParameter(OptionID optionID, ParameterConstraint<Number> constraint, boolean optional) { - super(optionID, constraint, optional); + super(optionID, optional); + addConstraint(constraint); } /** @@ -102,9 +71,12 @@ public class IntParameter extends NumberParameter<Integer> { * * @param optionID optionID the unique id of the option * @param constraint the constraint for this integer parameter + * @deprecated Use {@link #addConstraint} instead. */ + @Deprecated public IntParameter(OptionID optionID, ParameterConstraint<Number> constraint) { - super(optionID, constraint); + super(optionID); + addConstraint(constraint); } /** @@ -113,8 +85,8 @@ public class IntParameter extends NumberParameter<Integer> { * @param optionID optionID the unique id of the option * @param defaultValue the default value */ - public IntParameter(OptionID optionID, Integer defaultValue) { - super(optionID, defaultValue); + public IntParameter(OptionID optionID, int defaultValue) { + super(optionID, Integer.valueOf(defaultValue)); } /** @@ -122,7 +94,9 @@ public class IntParameter extends NumberParameter<Integer> { * * @param optionID optionID the unique id of the option * @param optional specifies if this parameter is an optional parameter + * @deprecated Use {@link #setOptional} instead. */ + @Deprecated public IntParameter(OptionID optionID, boolean optional) { super(optionID, optional); } @@ -136,25 +110,21 @@ public class IntParameter extends NumberParameter<Integer> { super(optionID); } - /** {@inheritDoc} */ @Override public String getValueAsString() { - return Integer.toString(getValue()); + return getValue().toString(); } - /** {@inheritDoc} */ @Override protected Integer parseValue(Object obj) throws ParameterException { - if(obj instanceof Integer) { + if (obj instanceof Integer) { return (Integer) obj; } try { - return Integer.parseInt(obj.toString()); - } - catch(NullPointerException e) { + return Integer.valueOf(obj.toString()); + } catch (NullPointerException e) { throw new WrongParameterValueException("Wrong parameter format! Parameter \"" + getName() + "\" requires an integer value, read: " + obj + "!\n"); - } - catch(NumberFormatException e) { + } catch (NumberFormatException e) { throw new WrongParameterValueException("Wrong parameter format! Parameter \"" + getName() + "\" requires an integer value, read: " + obj + "!\n"); } } @@ -168,4 +138,13 @@ public class IntParameter extends NumberParameter<Integer> { public String getSyntax() { return "<int>"; } + + /** + * Get the parameter value as integer + * + * @return Parameter value + */ + public int intValue() { + return getValue().intValue(); + } } diff --git a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameters/ListParameter.java b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameters/ListParameter.java index ad0ded5c..e71a744e 100644 --- a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameters/ListParameter.java +++ b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameters/ListParameter.java @@ -23,20 +23,20 @@ package de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters; along with this program. If not, see <http://www.gnu.org/licenses/>. */ -import de.lmu.ifi.dbs.elki.utilities.optionhandling.OptionID; -import de.lmu.ifi.dbs.elki.utilities.optionhandling.constraints.ParameterConstraint; - import java.util.List; import java.util.regex.Pattern; +import de.lmu.ifi.dbs.elki.utilities.optionhandling.OptionID; + /** * Abstract parameter class defining a parameter for a list of objects. * * @author Steffi Wanka * @author Erich Schubert + * * @param <T> List type */ -public abstract class ListParameter<T> extends Parameter<List<T>, List<T>> { +public abstract class ListParameter<T> extends AbstractParameter<List<T>> { /** * A pattern defining a ",". */ @@ -51,7 +51,7 @@ public abstract class ListParameter<T> extends Parameter<List<T>, List<T>> { * A pattern defining a ":". */ public static final Pattern VECTOR_SPLIT = Pattern.compile(":"); - + /** * Vector separator character - ":" */ @@ -61,79 +61,11 @@ public abstract class ListParameter<T> extends Parameter<List<T>, List<T>> { * Constructs a list parameter with the given optionID. * * @param optionID the unique id of this parameter - * @param constraints the constraints of this parameter, may be null * @param defaultValue the default value of this parameter (may be null) */ - public ListParameter(OptionID optionID, List<ParameterConstraint<List<T>>> constraints, List<T> defaultValue) { - super(optionID, constraints, defaultValue); - } - - /** - * Constructs a list parameter with the given optionID. - * - * @param optionID the unique id of this parameter - * @param constraints the constraints of this parameter, may be null - * @param optional specifies if this parameter is an optional parameter - */ - public ListParameter(OptionID optionID, List<ParameterConstraint<List<T>>> constraints, boolean optional) { - super(optionID, constraints, optional); - } - - /** - * Constructs a list parameter with the given optionID. - * - * @param optionID the unique id of this parameter - * @param constraints the constraint of this parameter - */ - // NOTE: we cannot have this, because it has the same erasure as optionID, defaults! - // Use optional=false! - /*public ListParameter(OptionID optionID, List<ParameterConstraint<List<T>>> constraints) { - super(optionID, constraints); - }*/ - - /** - * Constructs a list parameter with the given optionID. - * - * @param optionID the unique id of this parameter - * @param constraint the constraint of this parameter, may be null - * @param defaultValue the default value of this parameter (may be null) - */ - public ListParameter(OptionID optionID, ParameterConstraint<List<T>> constraint, List<T> defaultValue) { - super(optionID, constraint, defaultValue); - } - - /** - * Constructs a list parameter with the given optionID. - * - * @param optionID the unique id of this parameter - * @param constraint the constraint of this parameter, may be null - * @param optional specifies if this parameter is an optional parameter - */ - public ListParameter(OptionID optionID, ParameterConstraint<List<T>> constraint, boolean optional) { - super(optionID, constraint, optional); - } - - /** - * Constructs a list parameter with the given optionID. - * - * @param optionID the unique id of this parameter - * @param constraint the constraint of this parameter - */ - public ListParameter(OptionID optionID, ParameterConstraint<List<T>> constraint) { - super(optionID, constraint); - } - - /** - * Constructs a list parameter with the given optionID. - * - * @param optionID the unique id of this parameter - * @param defaultValue the default value of this parameter (may be null) - */ - // NOTE: we cannot have this, because it has the same erasure as optionID, defaults! - // Use full constructor, constraints = null! - /*public ListParameter(OptionID optionID, List<T> defaultValue) { + public ListParameter(OptionID optionID, List<T> defaultValue) { super(optionID, defaultValue); - }*/ + } /** * Constructs a list parameter with the given optionID and optional flag. @@ -160,7 +92,7 @@ public abstract class ListParameter<T> extends Parameter<List<T>, List<T>> { * @return the size of this list parameter. */ public int getListSize() { - if(getValue() == null && isOptional()) { + if (getValue() == null && isOptional()) { return 0; } @@ -173,19 +105,19 @@ public abstract class ListParameter<T> extends Parameter<List<T>, List<T>> { */ // TODO: keep? remove? protected String asString() { - if(getValue() == null) { + if (getValue() == null) { return ""; } StringBuilder buffer = new StringBuilder(); - buffer.append("["); + buffer.append('['); - for(int i = 0; i < getValue().size(); i++) { + for (int i = 0; i < getValue().size(); i++) { buffer.append(getValue().get(i).toString()); - if(i != getValue().size() - 1) { - buffer.append(","); + if (i != getValue().size() - 1) { + buffer.append(','); } } - buffer.append("]"); + buffer.append(']'); return buffer.toString(); } } diff --git a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameters/LongParameter.java b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameters/LongParameter.java index 6f1a7beb..ac8bd62c 100644 --- a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameters/LongParameter.java +++ b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameters/LongParameter.java @@ -23,8 +23,6 @@ package de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters; along with this program. If not, see <http://www.gnu.org/licenses/>. */ -import java.util.List; - 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.WrongParameterValueException; @@ -42,69 +40,14 @@ public class LongParameter extends NumberParameter<Long> { * and default value. * * @param optionID the unique OptionID for this parameter - * @param constraints the parameter constraints for this long parameter - * @param defaultValue the default value - */ - public LongParameter(OptionID optionID, List<ParameterConstraint<Number>> constraints, long defaultValue) { - super(optionID, constraints, defaultValue); - } - - /** - * Constructs a long parameter with the given optionID, and parameter - * constraint. - * - * @param optionID the unique OptionID for this parameter - * @param constraints the parameter constraints for this long parameter - * @param optional optional flag - */ - public LongParameter(OptionID optionID, List<ParameterConstraint<Number>> constraints, boolean optional) { - super(optionID, constraints, optional); - } - - /** - * Constructs a long parameter with the given optionID, and parameter - * constraint. - * - * @param optionID the unique OptionID for this parameter - * @param constraints the parameter constraints for this long parameter - */ - public LongParameter(OptionID optionID, List<ParameterConstraint<Number>> constraints) { - super(optionID, constraints); - } - - /** - * Constructs a long parameter with the given optionID, parameter constraint - * and default value. - * - * @param optionID the unique OptionID for this parameter * @param constraint the parameter constraint for this long parameter * @param defaultValue the default value + * @deprecated Use {@link #addConstraint} instead! */ + @Deprecated public LongParameter(OptionID optionID, ParameterConstraint<Number> constraint, long defaultValue) { - super(optionID, constraint, defaultValue); - } - - /** - * Constructs a long parameter with the given optionID, and parameter - * constraint. - * - * @param optionID the unique OptionID for this parameter - * @param constraint the parameter constraint for this long parameter - * @param optional optional flag - */ - public LongParameter(OptionID optionID, ParameterConstraint<Number> constraint, boolean optional) { - super(optionID, constraint, optional); - } - - /** - * Constructs a long parameter with the given optionID, and parameter - * constraint. - * - * @param optionID the unique OptionID for this parameter - * @param constraint the parameter constraint for this long parameter - */ - public LongParameter(OptionID optionID, ParameterConstraint<Number> constraint) { - super(optionID, constraint); + super(optionID, Long.valueOf(defaultValue)); + addConstraint(constraint); } /** @@ -114,17 +57,7 @@ public class LongParameter extends NumberParameter<Long> { * @param defaultValue the default value */ public LongParameter(OptionID optionID, long defaultValue) { - super(optionID, defaultValue); - } - - /** - * Constructs a long parameter with the given optionID. - * - * @param optionID the unique OptionID for this parameter - * @param optional optional flag - */ - public LongParameter(OptionID optionID, boolean optional) { - super(optionID, optional); + super(optionID, Long.valueOf(defaultValue)); } /** @@ -136,29 +69,27 @@ public class LongParameter extends NumberParameter<Long> { super(optionID); } - /** {@inheritDoc} */ @Override public String getValueAsString() { - return Long.toString(getValue()); + return getValue().toString(); } - /** {@inheritDoc} */ @Override protected Long parseValue(Object obj) throws ParameterException { if(obj instanceof Long) { return (Long) obj; } - if(obj instanceof Integer) { - return new Long((Integer) obj); + if(obj instanceof Number) { + return Long.valueOf(((Number) obj).longValue()); } try { - return Long.parseLong(obj.toString()); + return Long.valueOf(obj.toString()); } catch(NullPointerException e) { - throw new WrongParameterValueException("Wrong parameter format! Parameter \"" + getName() + "\" requires a double value, read: " + obj + "!\n"); + throw new WrongParameterValueException("Wrong parameter format! Parameter \"" + getName() + "\" requires a long value, read: " + obj + "!\n"); } catch(NumberFormatException e) { - throw new WrongParameterValueException("Wrong parameter format! Parameter \"" + getName() + "\" requires a double value, read: " + obj + "!\n"); + throw new WrongParameterValueException("Wrong parameter format! Parameter \"" + getName() + "\" requires a long value, read: " + obj + "!\n"); } } diff --git a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameters/NumberParameter.java b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameters/NumberParameter.java index 3a1ab946..8928c292 100644 --- a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameters/NumberParameter.java +++ b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameters/NumberParameter.java @@ -24,87 +24,16 @@ package de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters; */ import de.lmu.ifi.dbs.elki.utilities.optionhandling.OptionID; -import de.lmu.ifi.dbs.elki.utilities.optionhandling.constraints.ParameterConstraint; - -import java.util.List; /** * Abstract class for defining a number parameter. * * @author Steffi Wanka * @author Erich Schubert + * * @param <T> the type of a possible value (i.e., the type of the option) */ -public abstract class NumberParameter<T extends Number> extends Parameter<Number, T> { - /** - * Constructs a number parameter with the given optionID, constraint, and - * optional flag. - * - * @param optionID the unique id of this parameter - * @param constraints the constraints of this parameter - * @param defaultValue the default value for this parameter - */ - public NumberParameter(OptionID optionID, List<ParameterConstraint<Number>> constraints, T defaultValue) { - super(optionID, constraints, defaultValue); - } - - /** - * Constructs a number parameter with the given optionID, constraint, and - * optional flag. - * - * @param optionID the unique id of this parameter - * @param constraints the constraint of this parameter - * @param optional specifies if this parameter is an optional parameter - */ - public NumberParameter(OptionID optionID, List<ParameterConstraint<Number>> constraints, boolean optional) { - super(optionID, constraints, optional); - } - - /** - * Constructs a number parameter with the given optionID, and constraint. - * - * @param optionID the unique id of this parameter - * @param constraints the constraints of this parameter, may be empty if there - * are no constraints - */ - public NumberParameter(OptionID optionID, List<ParameterConstraint<Number>> constraints) { - super(optionID, constraints); - } - - /** - * Constructs a number parameter with the given optionID, constraint, and - * optional flag. - * - * @param optionID the unique id of this parameter - * @param constraint the constraint of this parameter - * @param defaultValue the default value for this parameter - */ - public NumberParameter(OptionID optionID, ParameterConstraint<Number> constraint, T defaultValue) { - super(optionID, constraint, defaultValue); - } - - /** - * Constructs a number parameter with the given optionID, constraint, and - * optional flag. - * - * @param optionID the unique id of this parameter - * @param constraint the constraint of this parameter - * @param optional specifies if this parameter is an optional parameter - */ - public NumberParameter(OptionID optionID, ParameterConstraint<Number> constraint, boolean optional) { - super(optionID, constraint, optional); - } - - /** - * Constructs a number parameter with the given optionID, and constraint. - * - * @param optionID the unique id of this parameter - * @param constraint the constraint of this parameter - */ - public NumberParameter(OptionID optionID, ParameterConstraint<Number> constraint) { - super(optionID, constraint); - } - +public abstract class NumberParameter<T extends Number> extends AbstractParameter<T> { /** * Constructs a number parameter with the given optionID and default Value. * diff --git a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameters/ObjectListParameter.java b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameters/ObjectListParameter.java index cfd74d9a..85a32085 100644 --- a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameters/ObjectListParameter.java +++ b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameters/ObjectListParameter.java @@ -68,18 +68,16 @@ public class ObjectListParameter<C> extends ClassListParameter<C> { super(optionID, restrictionClass); } - /** {@inheritDoc} */ @Override public String getSyntax() { return "<object_1|class_1,...,object_n|class_n>"; } - /** {@inheritDoc} */ @SuppressWarnings("unchecked") @Override protected List<Class<? extends C>> parseValue(Object obj) throws ParameterException { if(obj == null) { - throw new UnspecifiedParameterException("Parameter Error.\n" + "No value for parameter \"" + getName() + "\" " + "given."); + throw new UnspecifiedParameterException(this); } if (List.class.isInstance(obj)) { List<?> l = (List<?>) obj; @@ -116,7 +114,6 @@ public class ObjectListParameter<C> extends ClassListParameter<C> { return super.parseValue(obj); } - /** {@inheritDoc} */ @Override public List<C> instantiateClasses(Parameterization config) { if (instances == null) { diff --git a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameters/ObjectParameter.java b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameters/ObjectParameter.java index 0a1a6b27..eff80954 100644 --- a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameters/ObjectParameter.java +++ b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameters/ObjectParameter.java @@ -99,12 +99,11 @@ public class ObjectParameter<C> extends ClassParameter<C> { super(optionID, restrictionClass); } - /** {@inheritDoc} */ @SuppressWarnings("unchecked") @Override protected Class<? extends C> parseValue(Object obj) throws ParameterException { if(obj == null) { - throw new UnspecifiedParameterException("Parameter Error.\n" + "No value for parameter \"" + getName() + "\" " + "given."); + throw new UnspecifiedParameterException(this); } // does the given objects class fit? if(restrictionClass.isInstance(obj)) { @@ -113,7 +112,6 @@ public class ObjectParameter<C> extends ClassParameter<C> { return super.parseValue(obj); } - /** {@inheritDoc} */ @SuppressWarnings("unchecked") @Override public void setValue(Object obj) throws ParameterException { @@ -154,7 +152,6 @@ public class ObjectParameter<C> extends ClassParameter<C> { return instance = super.instantiateClass(config); } - /** {@inheritDoc} */ @Override public Object getGivenValue() { if(instance != null) { diff --git a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameters/Parameter.java b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameters/Parameter.java index 8502bbec..6dadbf8f 100644 --- a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameters/Parameter.java +++ b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameters/Parameter.java @@ -1,5 +1,10 @@ package de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters; +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.constraints.ParameterConstraint; + /* This file is part of ELKI: Environment for Developing KDD-Applications Supported by Index-Structures @@ -23,20 +28,8 @@ package de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters; along with this program. If not, see <http://www.gnu.org/licenses/>. */ -import java.security.InvalidParameterException; -import java.util.List; -import java.util.Vector; - -import de.lmu.ifi.dbs.elki.logging.LoggingUtil; -import de.lmu.ifi.dbs.elki.utilities.FormatUtil; -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; -import de.lmu.ifi.dbs.elki.utilities.optionhandling.constraints.ParameterConstraint; - /** - * Abstract class for specifying a parameter. + * Interface for the parameter of a class. * * A parameter is defined as an option having a specific value. * @@ -50,202 +43,28 @@ import de.lmu.ifi.dbs.elki.utilities.optionhandling.constraints.ParameterConstra * @apiviz.uses ParameterConstraint * * @param <T> the type of a possible value (i.e., the type of the option) - * @param <S> the supertype for constraints */ -public abstract class Parameter<S, T extends S> { - /** - * The default value of the parameter (may be null). - */ - protected T defaultValue = null; - - /** - * Specifies if the default value of this parameter was taken as parameter - * value. - */ - private boolean defaultValueTaken = false; - - /** - * Specifies if this parameter is an optional parameter. - */ - protected boolean optionalParameter = false; - - /** - * Holds parameter constraints for this parameter. - */ - protected final List<ParameterConstraint<S>> constraints; - - /** - * The option name. - */ - protected final OptionID optionid; - - /** - * The short description of the option. An extended description is provided by - * the method {@link #getFullDescription()} - */ - protected String shortDescription; - - /** - * The value last passed to this option. - */ - protected T givenValue = null; - - /** - * The value of this option. - */ - private T value; - - /** - * Constructs a parameter with the given optionID, constraints, and default - * value. - * - * @param optionID the unique id of this parameter - * @param constraints the constraints of this parameter, may be empty if there - * are no constraints - * @param defaultValue the default value of this parameter (may be null) - */ - public Parameter(OptionID optionID, List<ParameterConstraint<S>> constraints, T defaultValue) { - this.optionid = optionID; - this.shortDescription = optionID.getDescription(); - this.optionalParameter = true; - this.defaultValue = defaultValue; - this.constraints = (constraints != null) ? constraints : new Vector<ParameterConstraint<S>>(); - } - - /** - * Constructs a parameter with the given optionID, constraints, and optional - * flag. - * - * @param optionID the unique id of this parameter - * @param constraints the constraints of this parameter, may be empty if there - * are no constraints - * @param optional specifies if this parameter is an optional parameter - */ - public Parameter(OptionID optionID, List<ParameterConstraint<S>> constraints, boolean optional) { - this.optionid = optionID; - this.shortDescription = optionID.getDescription(); - this.optionalParameter = optional; - this.defaultValue = null; - this.constraints = (constraints != null) ? constraints : new Vector<ParameterConstraint<S>>(); - } - - /** - * Constructs a parameter with the given optionID, and constraints. - * - * @param optionID the unique id of this parameter - * @param constraints the constraints of this parameter, may be empty if there - * are no constraints - */ - public Parameter(OptionID optionID, List<ParameterConstraint<S>> constraints) { - this(optionID, constraints, false); - } - - /** - * Constructs a parameter with the given optionID, constraint, and default - * value. - * - * @param optionID the unique id of this parameter - * @param constraint the constraint of this parameter - * @param defaultValue the default value of this parameter (may be null) - */ - public Parameter(OptionID optionID, ParameterConstraint<S> constraint, T defaultValue) { - this(optionID, makeConstraintsVector(constraint), defaultValue); - } - - /** - * Constructs a parameter with the given optionID, constraint, and optional - * flag. - * - * @param optionID the unique id of this parameter - * @param constraint the constraint of this parameter - * @param optional specifies if this parameter is an optional parameter - */ - public Parameter(OptionID optionID, ParameterConstraint<S> constraint, boolean optional) { - this(optionID, makeConstraintsVector(constraint), optional); - } - - /** - * Constructs a parameter with the given optionID, and constraint. - * - * @param optionID the unique id of this parameter - * @param constraint the constraint of this parameter - */ - public Parameter(OptionID optionID, ParameterConstraint<S> constraint) { - this(optionID, constraint, false); - } - - /** - * Constructs a parameter with the given optionID and default value. - * - * @param optionID the unique id of the option - * @param defaultValue default value. - */ - public Parameter(OptionID optionID, T defaultValue) { - this(optionID, (Vector<ParameterConstraint<S>>) null, defaultValue); - } - - /** - * Constructs a parameter with the given optionID and optional flag. - * - * @param optionID the unique id of the option - * @param optional optional flag - */ - public Parameter(OptionID optionID, boolean optional) { - this(optionID, (Vector<ParameterConstraint<S>>) null, optional); - } - - /** - * Constructs a parameter with the given optionID. - * - * @param optionID the unique id of the option - */ - public Parameter(OptionID optionID) { - this(optionID, (Vector<ParameterConstraint<S>>) null, false); - } - - /** - * Wrap a single constraint into a vector of constraints. - * - * @param <S> Type - * @param constraint Constraint, may be {@code null} - * @return Vector containing the constraint (if not null) - */ - private static <S> List<ParameterConstraint<S>> makeConstraintsVector(ParameterConstraint<S> constraint) { - Vector<ParameterConstraint<S>> constraints = new Vector<ParameterConstraint<S>>((constraint == null) ? 0 : 1); - if(constraint != null) { - constraints.add(constraint); - } - return constraints; - } - +public interface Parameter<T> { /** * Sets the default value of this parameter. * * @param defaultValue default value of this parameter */ - public void setDefaultValue(T defaultValue) { - this.defaultValue = defaultValue; - this.optionalParameter = true; - } + public abstract void setDefaultValue(T defaultValue); /** * Checks if this parameter has a default value. * * @return true, if this parameter has a default value, false otherwise */ - public boolean hasDefaultValue() { - return !(defaultValue == null); - } + public abstract boolean hasDefaultValue(); /** * Sets the default value of this parameter as the actual value of this * parameter. */ // TODO: can we do this more elegantly? - public void useDefaultValue() { - setValueInternal(defaultValue); - defaultValueTaken = true; - } + public abstract void useDefaultValue(); /** * Handle default values for a parameter. @@ -255,38 +74,21 @@ public abstract class Parameter<S, T extends S> { * required parameter! * @throws UnspecifiedParameterException If the parameter requires a value */ - public boolean tryDefaultValue() throws UnspecifiedParameterException { - // Assume default value instead. - if(hasDefaultValue()) { - useDefaultValue(); - return true; - } - else if(isOptional()) { - // Optional is fine, but not successful - return false; - } - else { - throw new UnspecifiedParameterException(this); - } - } + public abstract boolean tryDefaultValue() throws UnspecifiedParameterException; /** * Specifies if this parameter is an optional parameter. * * @param opt true if this parameter is optional, false otherwise */ - public void setOptional(boolean opt) { - this.optionalParameter = opt; - } + public abstract void setOptional(boolean opt); /** * Checks if this parameter is an optional parameter. * * @return true if this parameter is optional, false otherwise */ - public boolean isOptional() { - return this.optionalParameter; - } + public abstract boolean isOptional(); /** * Checks if the default value of this parameter was taken as the actual @@ -295,18 +97,14 @@ public abstract class Parameter<S, T extends S> { * @return true, if the default value was taken as actual parameter value, * false otherwise */ - public boolean tookDefaultValue() { - return defaultValueTaken; - } + public abstract boolean tookDefaultValue(); /** * Returns true if the value of the option is defined, false otherwise. * * @return true if the value of the option is defined, false otherwise. */ - public boolean isDefined() { - return (this.value != null); - } + public abstract boolean isDefined(); /** * Returns the default value of the parameter. @@ -317,27 +115,21 @@ public abstract class Parameter<S, T extends S> { * has no default value. */ // TODO: change this to return a string value? - public T getDefaultValue() { - return defaultValue; - } + public abstract T getDefaultValue(); /** * Whether this class has a list of default values. * * @return whether the class has a description of valid values. */ - public boolean hasValuesDescription() { - return false; - } + public abstract boolean hasValuesDescription(); /** * Return a string explaining valid values. * * @return String explaining valid values (e.g. a class list) */ - public String getValuesDescription() { - return ""; - } + public abstract String getValuesDescription(); /** * Returns the extended description of the option which includes the option's @@ -345,99 +137,35 @@ public abstract class Parameter<S, T extends S> { * * @return the option's description. */ - public String getFullDescription() { - StringBuffer description = new StringBuffer(); - // description.append(getParameterType()).append(" "); - description.append(shortDescription); - description.append(FormatUtil.NEWLINE); - if(hasValuesDescription()) { - final String valuesDescription = getValuesDescription(); - description.append(valuesDescription); - if(!valuesDescription.endsWith(FormatUtil.NEWLINE)) { - description.append(FormatUtil.NEWLINE); - } - } - if(hasDefaultValue()) { - description.append("Default: "); - description.append(getDefaultValueAsString()); - description.append(FormatUtil.NEWLINE); - } - if(!constraints.isEmpty()) { - if(constraints.size() == 1) { - description.append("Constraint: "); - } - else if(constraints.size() > 1) { - description.append("Constraints: "); - } - for(int i = 0; i < constraints.size(); i++) { - ParameterConstraint<S> constraint = constraints.get(i); - if(i > 0) { - description.append(", "); - } - description.append(constraint.getDescription(getName())); - if(i == constraints.size() - 1) { - description.append("."); - } - } - description.append(FormatUtil.NEWLINE); - } - return description.toString(); - } - - /** - * Validate a value after parsing (e.g. do constrain checks!) - * - * @param obj Object to validate - * @return true iff the object is valid for this parameter. - * @throws ParameterException when the object is not valid. - */ - protected boolean validate(T obj) throws ParameterException { - try { - for(ParameterConstraint<S> cons : this.constraints) { - cons.test(obj); - } - } - catch(ParameterException e) { - throw new WrongParameterValueException("Specified parameter value for parameter \"" + getName() + "\" breaches parameter constraint.\n" + e.getMessage()); - } - return true; - } + public abstract String getFullDescription(); /** * Return the OptionID of this option. * * @return Option ID */ - public OptionID getOptionID() { - return optionid; - } + public abstract OptionID getOptionID(); /** * Returns the name of the option. * * @return the option's name. */ - public String getName() { - return optionid.getName(); - } + public abstract String getName(); /** * Returns the short description of the option. * * @return the option's short description. */ - public String getShortDescription() { - return shortDescription; - } + public abstract String getShortDescription(); /** * Sets the short description of the option. * * @param description the short description to be set */ - public void setShortDescription(String description) { - this.shortDescription = description; - } + public abstract void setShortDescription(String description); /** * Sets the value of the option. @@ -446,48 +174,26 @@ public abstract class Parameter<S, T extends S> { * @throws ParameterException if the given value is not a valid value for this * option. */ - public void setValue(Object obj) throws ParameterException { - T val = parseValue(obj); - if(validate(val)) { - setValueInternal(val); - } - else { - throw new InvalidParameterException("Value for option \"" + getName() + "\" did not validate: " + obj.toString()); - } - } - - /** - * Internal setter for the value. - * - * @param val Value - */ - protected final void setValueInternal(T val) { - this.value = this.givenValue = val; - } + public abstract void setValue(Object obj) throws ParameterException; /** * Returns the value of the option. * - * You should use either {@link de.lmu.ifi.dbs.elki.utilities.optionhandling.parameterization.Parameterization#grab} - * or {@link #isDefined} to test if getValue() will return a well-defined value. + * You should use either + * {@link de.lmu.ifi.dbs.elki.utilities.optionhandling.parameterization.Parameterization#grab} + * or {@link #isDefined} to test if getValue() will return a well-defined + * value. * * @return the option's value. */ - public final T getValue() { - if(this.value == null) { - LoggingUtil.warning("Programming error: Parameter#getValue() called for unset parameter \"" + this.optionid.getName() + "\"", new Throwable()); - } - return this.value; - } + public abstract T getValue(); /** * Get the last given value. May return {@code null} * * @return Given value */ - public Object getGivenValue() { - return this.givenValue; - } + public abstract Object getGivenValue(); /** * Checks if the given argument is valid for this option. @@ -497,10 +203,7 @@ public abstract class Parameter<S, T extends S> { * @throws ParameterException if the given value is not a valid value for this * option. */ - public final boolean isValid(Object obj) throws ParameterException { - T val = parseValue(obj); - return validate(val); - } + public abstract boolean isValid(Object obj) throws ParameterException; /** * Returns a string representation of the parameter's type (e.g. an @@ -512,15 +215,6 @@ public abstract class Parameter<S, T extends S> { public abstract String getSyntax(); /** - * Parse a given value into the destination type. - * - * @param obj Object to parse (may be a string representation!) - * @return Parsed object - * @throws ParameterException when the object cannot be parsed. - */ - protected abstract T parseValue(Object obj) throws ParameterException; - - /** * Get the value as string. May return {@code null} * * @return Value as string @@ -532,16 +226,12 @@ public abstract class Parameter<S, T extends S> { * * @return default value */ - public String getDefaultValueAsString() { - return getDefaultValue().toString(); - } + public abstract String getDefaultValueAsString(); /** * Add an additional constraint. * * @param constraint Constraint to add. */ - public void addConstraint(ParameterConstraint<S> constraint) { - constraints.add(constraint); - } -}
\ No newline at end of file + public abstract void addConstraint(ParameterConstraint<? super T> constraint); +} diff --git a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameters/PatternParameter.java b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameters/PatternParameter.java index 1f54c5d2..05c99d2b 100644 --- a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameters/PatternParameter.java +++ b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameters/PatternParameter.java @@ -23,7 +23,6 @@ package de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters; along with this program. If not, see <http://www.gnu.org/licenses/>. */ -import java.util.List; import java.util.regex.Pattern; import java.util.regex.PatternSyntaxException; @@ -31,7 +30,6 @@ 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; -import de.lmu.ifi.dbs.elki.utilities.optionhandling.constraints.ParameterConstraint; /** * Parameter class for a parameter specifying a pattern. @@ -39,101 +37,7 @@ import de.lmu.ifi.dbs.elki.utilities.optionhandling.constraints.ParameterConstra * @author Steffi Wanka * @author Erich Schubert */ -public class PatternParameter extends Parameter<Pattern, Pattern> { - /** - * Constructs a pattern parameter with the given optionID, constraints and - * default value. - * - * @param optionID the unique id of the parameter - * @param constraint parameter constraint - * @param defaultValue the default value of the parameter - */ - public PatternParameter(OptionID optionID, List<ParameterConstraint<Pattern>> constraint, Pattern defaultValue) { - super(optionID, constraint, defaultValue); - } - - /** - * Constructs a pattern parameter with the given optionID, constraints and - * default value. - * - * @param optionID the unique id of the parameter - * @param constraint parameter constraint - * @param defaultValue the default value of the parameter - */ - public PatternParameter(OptionID optionID, List<ParameterConstraint<Pattern>> constraint, String defaultValue) { - super(optionID, constraint, Pattern.compile(defaultValue, Pattern.CASE_INSENSITIVE)); - } - - /** - * Constructs a pattern parameter with the given optionID, constraints and - * default value. - * - * @param optionID the unique id of the parameter - * @param constraints parameter constraint - * @param optional Flag to signal an optional parameter. - */ - public PatternParameter(OptionID optionID, List<ParameterConstraint<Pattern>> constraints, boolean optional) { - super(optionID, constraints, optional); - } - - /** - * Constructs a pattern parameter with the given optionID, constraints and - * default value. - * - * @param optionID the unique id of the parameter - * @param constraints parameter constraint - */ - public PatternParameter(OptionID optionID, List<ParameterConstraint<Pattern>> constraints) { - super(optionID, constraints); - } - - /** - * Constructs a pattern parameter with the given optionID, constraints and - * default value. - * - * @param optionID the unique id of the parameter - * @param constraint parameter constraint - * @param defaultValue the default value of the parameter - */ - public PatternParameter(OptionID optionID, ParameterConstraint<Pattern> constraint, Pattern defaultValue) { - super(optionID, constraint, defaultValue); - } - - /** - * Constructs a pattern parameter with the given optionID, constraints and - * default value. - * - * @param optionID the unique id of the parameter - * @param constraint parameter constraint - * @param defaultValue the default value of the parameter - */ - public PatternParameter(OptionID optionID, ParameterConstraint<Pattern> constraint, String defaultValue) { - super(optionID, constraint, Pattern.compile(defaultValue, Pattern.CASE_INSENSITIVE)); - } - - /** - * Constructs a pattern parameter with the given optionID, constraints and - * default value. - * - * @param optionID the unique id of the parameter - * @param constraint parameter constraint - * @param optional Flag to signal an optional parameter. - */ - public PatternParameter(OptionID optionID, ParameterConstraint<Pattern> constraint, boolean optional) { - super(optionID, constraint, optional); - } - - /** - * Constructs a pattern parameter with the given optionID, constraints and - * default value. - * - * @param optionID the unique id of the parameter - * @param constraint parameter constraint - */ - public PatternParameter(OptionID optionID, ParameterConstraint<Pattern> constraint) { - super(optionID, constraint); - } - +public class PatternParameter extends AbstractParameter<Pattern> { /** * Constructs a pattern parameter with the given optionID, and default value. * @@ -158,16 +62,6 @@ public class PatternParameter extends Parameter<Pattern, Pattern> { * Constructs a pattern parameter with the given optionID. * * @param optionID the unique id of the parameter - * @param optional Flag to signal an optional parameter. - */ - public PatternParameter(OptionID optionID, boolean optional) { - super(optionID, optional); - } - - /** - * Constructs a pattern parameter with the given optionID. - * - * @param optionID the unique id of the parameter */ public PatternParameter(OptionID optionID) { super(optionID); @@ -181,7 +75,7 @@ public class PatternParameter extends Parameter<Pattern, Pattern> { @Override protected Pattern parseValue(Object obj) throws ParameterException { if(obj == null) { - throw new UnspecifiedParameterException("Parameter \"" + getName() + "\": Null value given!"); + throw new UnspecifiedParameterException(this); } if(obj instanceof Pattern) { return (Pattern) obj; diff --git a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameters/RandomParameter.java b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameters/RandomParameter.java new file mode 100644 index 00000000..34d01de5 --- /dev/null +++ b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameters/RandomParameter.java @@ -0,0 +1,147 @@ +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) 2012 + 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 de.lmu.ifi.dbs.elki.utilities.RandomFactory; +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.WrongParameterValueException; + +/** + * Parameter for random generators and/or random seeds. + * + * @author Erich Schubert + */ +public class RandomParameter extends AbstractParameter<RandomFactory> { + /** + * Seed value, if used + */ + Long seed = null; + + /** + * Constructor without default. + * + * @param optionID Option ID + */ + public RandomParameter(OptionID optionID) { + super(optionID, RandomFactory.DEFAULT); + } + + /** + * Constructor with default value. The default value may be {@code null}, + * which means a new random will be generated. + * + * @param optionID Option ID + * @param defaultValue Default value. If {@code null}, a new random object + * will be created. + */ + public RandomParameter(OptionID optionID, RandomFactory defaultValue) { + super(optionID, defaultValue); + } + + /** + * Constructor with default seed value. + * + * @param optionID Option ID + * @param seed Default seed. + */ + public RandomParameter(OptionID optionID, long seed) { + super(optionID, true); + this.seed = Long.valueOf(seed); + } + + @Override + public String getSyntax() { + return "<long|Random>"; + } + + @Override + public void setValue(Object obj) throws ParameterException { + // This is a bit hackish. Set both seed and random (via super.setValue()) + if (obj instanceof RandomFactory) { + seed = null; + } else if (obj instanceof Long) { + seed = (Long) obj; + obj = RandomFactory.get(seed); + } else if (obj instanceof Number) { + seed = Long.valueOf(((Number) obj).longValue()); + obj = RandomFactory.get(seed); + } else if ("global random".equals(obj)) { + obj = RandomFactory.DEFAULT; + } else { + try { + seed = Long.valueOf(obj.toString()); + obj = RandomFactory.get(seed); + } catch (NullPointerException e) { + throw new WrongParameterValueException("Wrong parameter format! Parameter \"" + getName() + "\" requires a long seed value or a random generator factory, read: " + obj + "!\n"); + } catch (NumberFormatException e) { + throw new WrongParameterValueException("Wrong parameter format! Parameter \"" + getName() + "\" requires a long seed value or a random generator factory, read: " + obj + "!\n"); + } + } + super.setValue(obj); + } + + @Override + protected RandomFactory parseValue(Object obj) throws ParameterException { + if (obj instanceof RandomFactory) { + return (RandomFactory) obj; + } + if (obj instanceof Long) { + return RandomFactory.get((Long) obj); + } + if (obj instanceof Number) { + return RandomFactory.get(Long.valueOf(((Number) obj).longValue())); + } + try { + return RandomFactory.get(Long.valueOf(obj.toString())); + } catch (NullPointerException e) { + throw new WrongParameterValueException("Wrong parameter format! Parameter \"" + getName() + "\" requires a long seed value or a random generator factory, read: " + obj + "!\n"); + } catch (NumberFormatException e) { + throw new WrongParameterValueException("Wrong parameter format! Parameter \"" + getName() + "\" requires a long seed value or a random generator factory, read: " + obj + "!\n"); + } + } + + @Override + public Object getGivenValue() { + Object r = super.getGivenValue(); + if (r != null && seed != null) { + super.givenValue = RandomFactory.get(seed); + r = super.givenValue; + } + return r; + } + + @Override + public String getValueAsString() { + return (seed != null) ? seed.toString() : "null"; + } + + @Override + public String getDefaultValueAsString() { + if (defaultValue == RandomFactory.DEFAULT) { + return "global random"; + } + return super.getDefaultValueAsString(); + } +} diff --git a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameters/StringParameter.java b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameters/StringParameter.java index ea1c5a2c..06789dba 100644 --- a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameters/StringParameter.java +++ b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameters/StringParameter.java @@ -23,8 +23,6 @@ package de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters; along with this program. If not, see <http://www.gnu.org/licenses/>. */ -import java.util.List; - 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; @@ -37,7 +35,7 @@ import de.lmu.ifi.dbs.elki.utilities.optionhandling.constraints.ParameterConstra * @author Steffi Wanka * @author Erich Schubert */ -public class StringParameter extends Parameter<String, String> { +public class StringParameter extends AbstractParameter<String> { /** * Constructs a string parameter with the given optionID, constraints and * default value. @@ -45,44 +43,13 @@ public class StringParameter extends Parameter<String, String> { * @param optionID the unique id of the parameter * @param constraint parameter constraint * @param defaultValue the default value of the parameter - */ - public StringParameter(OptionID optionID, List<ParameterConstraint<String>> constraint, String defaultValue) { - super(optionID, constraint, defaultValue); - } - - /** - * Constructs a string parameter with the given optionID, constraints and - * default value. - * - * @param optionID the unique id of the parameter - * @param constraints parameter constraint - * @param optional Flag to signal an optional parameter. - */ - public StringParameter(OptionID optionID, List<ParameterConstraint<String>> constraints, boolean optional) { - super(optionID, constraints, optional); - } - - /** - * Constructs a string parameter with the given optionID, constraints and - * default value. - * - * @param optionID the unique id of the parameter - * @param constraints parameter constraint - */ - public StringParameter(OptionID optionID, List<ParameterConstraint<String>> constraints) { - super(optionID, constraints); - } - - /** - * Constructs a string parameter with the given optionID, constraints and - * default value. * - * @param optionID the unique id of the parameter - * @param constraint parameter constraint - * @param defaultValue the default value of the parameter + * @deprecated Use {@link #addConstraint} instead! */ + @Deprecated public StringParameter(OptionID optionID, ParameterConstraint<String> constraint, String defaultValue) { - super(optionID, constraint, defaultValue); + super(optionID, defaultValue); + addConstraint(constraint); } /** @@ -91,21 +58,13 @@ public class StringParameter extends Parameter<String, String> { * * @param optionID the unique id of the parameter * @param constraint parameter constraint - * @param optional Flag to signal an optional parameter. - */ - public StringParameter(OptionID optionID, ParameterConstraint<String> constraint, boolean optional) { - super(optionID, constraint, optional); - } - - /** - * Constructs a string parameter with the given optionID, constraints and - * default value. * - * @param optionID the unique id of the parameter - * @param constraint parameter constraint + * @deprecated Use {@link #addConstraint} instead! */ + @Deprecated public StringParameter(OptionID optionID, ParameterConstraint<String> constraint) { - super(optionID, constraint); + super(optionID); + addConstraint(constraint); } /** @@ -122,38 +81,26 @@ public class StringParameter extends Parameter<String, String> { * Constructs a string parameter with the given optionID. * * @param optionID the unique id of the parameter - * @param optional Flag to signal an optional parameter. - */ - public StringParameter(OptionID optionID, boolean optional) { - super(optionID, optional); - } - - /** - * Constructs a string parameter with the given optionID. - * - * @param optionID the unique id of the parameter */ public StringParameter(OptionID optionID) { super(optionID); } - - /** {@inheritDoc} */ + @Override public String getValueAsString() { return getValue(); } - /** {@inheritDoc} */ @Override protected String parseValue(Object obj) throws ParameterException { if(obj == null) { - throw new UnspecifiedParameterException("Parameter \"" + getName() + "\": Null value given!"); + throw new UnspecifiedParameterException(this); } - if (obj instanceof String) { + if(obj instanceof String) { return (String) obj; } // TODO: allow anything convertible by toString()? - throw new WrongParameterValueException("String parameter "+getName()+" is not a string."); + throw new WrongParameterValueException("String parameter " + getName() + " is not a string."); } /** diff --git a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameters/VectorListParameter.java b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameters/VectorListParameter.java index 0e6612b6..a9d01f22 100644 --- a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameters/VectorListParameter.java +++ b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameters/VectorListParameter.java @@ -48,7 +48,8 @@ public class VectorListParameter extends ListParameter<List<Double>> { * @param defaultValue Default value */ public VectorListParameter(OptionID optionID, List<ParameterConstraint<List<List<Double>>>> constraints, List<List<Double>> defaultValue) { - super(optionID, constraints, defaultValue); + super(optionID, defaultValue); + addConstraints(constraints); } /** @@ -59,30 +60,20 @@ public class VectorListParameter extends ListParameter<List<Double>> { * @param optional Optional flag */ public VectorListParameter(OptionID optionID, List<ParameterConstraint<List<List<Double>>>> constraints, boolean optional) { - super(optionID, constraints, optional); + super(optionID, optional); + addConstraints(constraints); } /** * Constructs a vector list parameter with the given name and description. * * @param optionID Option ID - * @param constraints Constraints - */ - // Indiscernible from optionID, defaults - /* - * public VectorListParameter(OptionID optionID, List<ParameterConstraint<? - * super List<List<Double>>>> constraints) { super(optionID, constraints); } - */ - - /** - * Constructs a vector list parameter with the given name and description. - * - * @param optionID Option ID * @param constraint Constraint * @param defaultValue Default value */ public VectorListParameter(OptionID optionID, ParameterConstraint<List<List<Double>>> constraint, List<List<Double>> defaultValue) { - super(optionID, constraint, defaultValue); + super(optionID, defaultValue); + addConstraint(constraint); } /** @@ -93,7 +84,8 @@ public class VectorListParameter extends ListParameter<List<Double>> { * @param optional Optional flag */ public VectorListParameter(OptionID optionID, ParameterConstraint<List<List<Double>>> constraint, boolean optional) { - super(optionID, constraint, optional); + super(optionID, optional); + addConstraint(constraint); } /** @@ -103,7 +95,8 @@ public class VectorListParameter extends ListParameter<List<Double>> { * @param constraint Constraint */ public VectorListParameter(OptionID optionID, ParameterConstraint<List<List<Double>>> constraint) { - super(optionID, constraint); + super(optionID); + addConstraint(constraint); } /** @@ -137,17 +130,16 @@ public class VectorListParameter extends ListParameter<List<Double>> { super(optionID); } - /** {@inheritDoc} */ @Override public String getValueAsString() { - StringBuffer buf = new StringBuffer(); + StringBuilder buf = new StringBuilder(); List<List<Double>> val = getValue(); Iterator<List<Double>> valiter = val.iterator(); while(valiter.hasNext()) { List<Double> vec = valiter.next(); Iterator<Double> veciter = vec.iterator(); while(veciter.hasNext()) { - buf.append(Double.toString(veciter.next())); + buf.append(veciter.next().toString()); if (veciter.hasNext()) { buf.append(LIST_SEP); } @@ -160,7 +152,6 @@ public class VectorListParameter extends ListParameter<List<Double>> { return buf.toString(); } - /** {@inheritDoc} */ @SuppressWarnings("unchecked") @Override protected List<List<Double>> parseValue(Object obj) throws ParameterException { @@ -185,7 +176,7 @@ public class VectorListParameter extends ListParameter<List<Double>> { if(obj instanceof String) { String[] vectors = VECTOR_SPLIT.split((String) obj); if(vectors.length == 0) { - throw new UnspecifiedParameterException("Wrong parameter format! Given list of vectors for parameter \"" + getName() + "\" is empty!"); + throw new WrongParameterValueException("Wrong parameter format! Given list of vectors for parameter \"" + getName() + "\" is empty!"); } ArrayList<List<Double>> vecs = new ArrayList<List<Double>>(); @@ -194,8 +185,7 @@ public class VectorListParameter extends ListParameter<List<Double>> { ArrayList<Double> vectorCoord = new ArrayList<Double>(); for(String coordinate : coordinates) { try { - Double.parseDouble(coordinate); - vectorCoord.add(Double.parseDouble(coordinate)); + vectorCoord.add(Double.valueOf(coordinate)); } catch(NumberFormatException e) { throw new WrongParameterValueException("Wrong parameter format! Coordinates of vector \"" + vector + "\" are not valid!"); @@ -209,23 +199,6 @@ public class VectorListParameter extends ListParameter<List<Double>> { } /** - * Returns an array containing the individual vector sizes of this vector list - * parameter. - * - * @return the individual vector sizes - */ - // unused? - /* - * public int[] vectorSizes() { - * - * int[] sizes = new int[getListSize()]; - * - * int i = 0; for(List<?> vecs : value) { sizes[i] = vecs.size(); i++; } - * - * return sizes; } - */ - - /** * Returns a string representation of the parameter's type. * * @return diff --git a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameters/package-info.java b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameters/package-info.java index 5fcaa0c0..b6ff5216 100644 --- a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameters/package-info.java +++ b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameters/package-info.java @@ -3,6 +3,10 @@ * * See the {@link de.lmu.ifi.dbs.elki.utilities.optionhandling} package for documentation! * + * @apiviz.exclude elki.utilities.* + * @apiviz.exclude gui.configurator.* + * @apiviz.exclude AbstractParameterizer + * @apiviz.exclude constraints.ParameterConstraint */ /* This file is part of ELKI: @@ -26,4 +30,4 @@ 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/>. */ -package de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters;
\ No newline at end of file +package de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters; |