diff options
Diffstat (limited to 'src/de/lmu/ifi/dbs/elki/utilities/optionhandling')
58 files changed, 1348 insertions, 1753 deletions
diff --git a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/AbstractParameterizer.java b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/AbstractParameterizer.java index 32543ace..0a094ced 100644 --- a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/AbstractParameterizer.java +++ b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/AbstractParameterizer.java @@ -37,22 +37,22 @@ import de.lmu.ifi.dbs.elki.utilities.optionhandling.parameterization.Parameteriz */ public abstract class AbstractParameterizer implements Parameterizer { /** - * Constant for "fresh" state + * Constant for "fresh" state. */ private static final int STATE_FRESH = 0; /** - * Constant for "initializing" state + * Constant for "initializing" state. */ private static final int STATE_INIT = 1; /** - * Constant for "complete" state + * Constant for "complete" state. */ private static final int STATE_COMPLETE = 2; /** - * Constant for "errors" state + * Constant for "errors" state. */ private static final int STATE_ERRORS = -1; @@ -84,7 +84,7 @@ public abstract class AbstractParameterizer implements Parameterizer { * * @return instance */ - abstract protected Object makeInstance(); + protected abstract Object makeInstance(); /** * Method to configure a class, then instantiate when the configuration step diff --git a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/InternalParameterizationErrors.java b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/InternalParameterizationErrors.java index 4bec9193..c8797c73 100644 --- a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/InternalParameterizationErrors.java +++ b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/InternalParameterizationErrors.java @@ -23,6 +23,7 @@ package de.lmu.ifi.dbs.elki.utilities.optionhandling; along with this program. If not, see <http://www.gnu.org/licenses/>. */ +import java.util.ArrayList; import java.util.Collection; /** @@ -62,7 +63,7 @@ public class InternalParameterizationErrors extends ParameterException { */ public InternalParameterizationErrors(String message, Exception internalError) { super(message); - final java.util.Vector<Exception> errors = new java.util.Vector<Exception>(1); + final ArrayList<Exception> errors = new ArrayList<Exception>(1); errors.add(internalError); this.internalErrors = errors; } diff --git a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/NoParameterValueException.java b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/NoParameterValueException.java index 2965f207..ce422de7 100644 --- a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/NoParameterValueException.java +++ b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/NoParameterValueException.java @@ -23,7 +23,6 @@ package de.lmu.ifi.dbs.elki.utilities.optionhandling; along with this program. If not, see <http://www.gnu.org/licenses/>. */ - /** * Thrown by OptionHandler in case of incorrect parameter-array. */ diff --git a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/OptionID.java b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/OptionID.java index bea6bb4f..2c9814e3 100644 --- a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/OptionID.java +++ b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/OptionID.java @@ -23,8 +23,6 @@ package de.lmu.ifi.dbs.elki.utilities.optionhandling; along with this program. If not, see <http://www.gnu.org/licenses/>. */ -import de.lmu.ifi.dbs.elki.utilities.ConstantObject; - /** * An OptionID is used by option handlers as a unique identifier for specific * options. There is no option possible without a specific OptionID defined @@ -32,7 +30,7 @@ import de.lmu.ifi.dbs.elki.utilities.ConstantObject; * * @author Elke Achtert */ -public final class OptionID extends ConstantObject<OptionID> { +public final class OptionID { /** * Flag to obtain help-message. * <p> @@ -113,6 +111,11 @@ public final class OptionID extends ConstantObject<OptionID> { public static final OptionID TIME_FLAG = new OptionID("time", "Enable logging of runtime data. Do not combine with more verbose logging, since verbose logging can significantly impact performance."); /** + * Option name + */ + private String name; + + /** * The description of the OptionID. */ private String description; @@ -126,8 +129,9 @@ public final class OptionID extends ConstantObject<OptionID> { * @param name the name of the option * @param description the description of the option */ - private OptionID(final String name, final String description) { - super(name); + public OptionID(final String name, final String description) { + super(); + this.name = name; this.description = description; } @@ -141,15 +145,6 @@ public final class OptionID extends ConstantObject<OptionID> { } /** - * Sets the description of this OptionID. - * - * @param description the description to be set - */ - public void setDescription(String description) { - this.description = description; - } - - /** * Gets or creates the OptionID for the given class and given name. The * OptionID usually is named as the classes name (lowercase) as name-prefix * and the given name as suffix of the complete name, separated by a dot. For @@ -163,24 +158,7 @@ public final class OptionID extends ConstantObject<OptionID> { * @return the OptionID for the given name */ public static OptionID getOrCreateOptionID(final String name, final String description) { - OptionID optionID = getOptionID(name); - if(optionID == null) { - optionID = new OptionID(name, description); - } - else { - optionID.setDescription(description); - } - return optionID; - } - - /** - * Returns the OptionID for the given name if it exists, null otherwise. - * - * @param name name of the desired OptionID - * @return the OptionID for the given name - */ - public static OptionID getOptionID(final String name) { - return OptionID.lookup(OptionID.class, name); + return new OptionID(name, description); } /** @@ -194,4 +172,12 @@ public final class OptionID extends ConstantObject<OptionID> { return getName(); } + /** + * Get the option name. + * + * @return Option name + */ + public String getName() { + return name; + } }
\ No newline at end of file diff --git a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/OptionUtil.java b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/OptionUtil.java index 2aa68611..76ae9feb 100644 --- a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/OptionUtil.java +++ b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/OptionUtil.java @@ -43,6 +43,13 @@ import de.lmu.ifi.dbs.elki.utilities.pairs.Pair; */ public final class OptionUtil { /** + * Fake constructor. Use static method. + */ + private OptionUtil() { + // Do not instantiate. + } + + /** * Returns a string representation of the specified list of options containing * the names of the options. * @@ -50,16 +57,16 @@ public final class OptionUtil { * @param options the list of options * @return the names of the options */ - public static <O extends Parameter<?, ?>> String optionsNamesToString(List<O> options) { - StringBuffer buffer = new StringBuffer(); - buffer.append("["); + public static <O extends Parameter<?>> String optionsNamesToString(List<O> options) { + StringBuilder buffer = new StringBuilder(); + buffer.append('['); for(int i = 0; i < options.size(); i++) { buffer.append(options.get(i).getName()); if(i != options.size() - 1) { - buffer.append(","); + buffer.append(','); } } - buffer.append("]"); + buffer.append(']'); return buffer.toString(); } @@ -71,16 +78,16 @@ public final class OptionUtil { * @param options the list of options * @return the names of the options */ - public static <O extends Parameter<?, ?>> String optionsNamesToString(O[] options) { - StringBuffer buffer = new StringBuffer(); - buffer.append("["); + public static <O extends Parameter<?>> String optionsNamesToString(O[] options) { + StringBuilder buffer = new StringBuilder(); + buffer.append('['); for(int i = 0; i < options.length; i++) { buffer.append(options[i].getName()); if(i != options.length - 1) { - buffer.append(","); + buffer.append(','); } } - buffer.append("]"); + buffer.append(']'); return buffer.toString(); } @@ -92,18 +99,18 @@ public final class OptionUtil { * @param parameters the list of number parameters * @return the names and the values of the parameters */ - public static <N extends Parameter<?, ?>> String parameterNamesAndValuesToString(List<N> parameters) { - StringBuffer buffer = new StringBuffer(); - buffer.append("["); + public static <N extends Parameter<?>> String parameterNamesAndValuesToString(List<N> parameters) { + StringBuilder buffer = new StringBuilder(); + buffer.append('['); for(int i = 0; i < parameters.size(); i++) { buffer.append(parameters.get(i).getName()); - buffer.append(":"); + buffer.append(':'); buffer.append(parameters.get(i).getValueAsString()); if(i != parameters.size() - 1) { buffer.append(", "); } } - buffer.append("]"); + buffer.append(']'); return buffer.toString(); } @@ -116,15 +123,15 @@ public final class OptionUtil { * @param indent Indentation string * @param options List of options */ - public static void formatForConsole(StringBuffer buf, int width, String indent, Collection<Pair<Object, Parameter<?, ?>>> options) { - for(Pair<Object, Parameter<?, ?>> pair : options) { + public static void formatForConsole(StringBuilder buf, int width, String indent, Collection<Pair<Object, Parameter<?>>> options) { + for(Pair<Object, Parameter<?>> pair : options) { String currentOption = pair.getSecond().getName(); String syntax = pair.getSecond().getSyntax(); String longDescription = pair.getSecond().getFullDescription(); buf.append(SerializedParameterization.OPTION_PREFIX); buf.append(currentOption); - buf.append(" "); + buf.append(' '); buf.append(syntax); buf.append(FormatUtil.NEWLINE); println(buf, width, longDescription, indent); @@ -139,7 +146,7 @@ public final class OptionUtil { * @param data Data to write. * @param indent Indentation */ - public static void println(StringBuffer buf, int width, String data, String indent) { + public static void println(StringBuilder buf, int width, String data, String indent) { for(String line : FormatUtil.splitAtLastBlank(data, width - indent.length())) { buf.append(indent); buf.append(line); @@ -158,7 +165,7 @@ public final class OptionUtil { * @param indent Text indent * @return Formatted description */ - public static StringBuffer describeParameterizable(StringBuffer buf, Class<?> pcls, int width, String indent) { + public static StringBuilder describeParameterizable(StringBuilder buf, Class<?> pcls, int width, String indent) { try { println(buf, width, "Description for class " + pcls.getName(), ""); @@ -189,7 +196,7 @@ public final class OptionUtil { TrackParameters track = new TrackParameters(config); @SuppressWarnings("unused") Object p = ClassGenericsUtil.tryInstantiate(Object.class, pcls, track); - Collection<Pair<Object, Parameter<?, ?>>> options = track.getAllParameters(); + Collection<Pair<Object, Parameter<?>>> options = track.getAllParameters(); if(options.size() > 0) { OptionUtil.formatForConsole(buf, width, indent, options); } @@ -198,7 +205,7 @@ public final class OptionUtil { } catch(Exception e) { LoggingUtil.exception("Error instantiating class to describe.", e.getCause()); - buf.append("No description available: " + e); + buf.append("No description available: ").append(e); return buf; } } diff --git a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/Parameterizable.java b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/Parameterizable.java index 4149658a..13f202dd 100644 --- a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/Parameterizable.java +++ b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/Parameterizable.java @@ -47,7 +47,7 @@ import de.lmu.ifi.dbs.elki.utilities.InspectionUtilFrequentlyScanned; * <em>must have a constructor that either is</em> <blockquote> * * <pre> - * @code + * {@code * public Class(Parameterizable config) { ... } * } * </pre> @@ -55,7 +55,7 @@ import de.lmu.ifi.dbs.elki.utilities.InspectionUtilFrequentlyScanned; * </blockquote> or <blockquote> * * <pre> - * @code + * {@code * public Class() { ... } * } * </pre> @@ -91,11 +91,12 @@ import de.lmu.ifi.dbs.elki.utilities.InspectionUtilFrequentlyScanned; * * @apiviz.exclude * @apiviz.excludeSubtypes - * @apiviz.has de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.Parameter oneway - n + * @apiviz.has de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.Parameter + * oneway - n * @apiviz.uses de.lmu.ifi.dbs.elki.utilities.optionhandling.parameterization * .Parameterization oneway */ public interface Parameterizable extends InspectionUtilFrequentlyScanned { // Empty marker interface - the \@Description / \@Title / \@Reference and // constructor requirements cannot be specified in Java! -}
\ No newline at end of file +} diff --git a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/UnspecifiedParameterException.java b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/UnspecifiedParameterException.java index 8e294ddb..ab138704 100644 --- a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/UnspecifiedParameterException.java +++ b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/UnspecifiedParameterException.java @@ -27,6 +27,7 @@ import de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.Parameter; /** * Exception when a required parameter was not given. + * * @author Erich Schubert */ public class UnspecifiedParameterException extends WrongParameterValueException { @@ -36,27 +37,26 @@ public class UnspecifiedParameterException extends WrongParameterValueException private static final long serialVersionUID = -7142809547201980898L; /** - * Constructor with missing Parameter - * @param parameter Missing parameter + * Parameter that was missing. */ - public UnspecifiedParameterException(Parameter<?, ?> parameter) { - super("No value given for parameter \"" + parameter.getName() + "\":\n" + "Expected: " + parameter.getFullDescription()); - } + private String parameter; /** - * Constructor with missing Parameter and cause + * Constructor with missing Parameter + * * @param parameter Missing parameter - * @param cause Cause */ - public UnspecifiedParameterException(Parameter<?, ?> parameter, Throwable cause) { - super("No value given for parameter \"" + parameter.getName() + "\":\n" + "Expected: " + parameter.getFullDescription(), cause); + public UnspecifiedParameterException(Parameter<?> parameter) { + super("No value given for parameter \"" + parameter.getName() + "\":\n" + "Expected: " + parameter.getFullDescription()); + this.parameter = parameter.getName(); } /** - * Constructor with error message. - * @param message Message + * Get the parameter name that was missing. + * + * @return Parameter name */ - public UnspecifiedParameterException(String message) { - super(message); + public String getParameterName() { + return parameter; } -}
\ No newline at end of file +} diff --git a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/WrongParameterValueException.java b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/WrongParameterValueException.java index 219b32cd..c51eae0e 100644 --- a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/WrongParameterValueException.java +++ b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/WrongParameterValueException.java @@ -27,6 +27,8 @@ import de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.Parameter; /** * Thrown by a Parameterizable object in case of wrong parameter format. + * + * @author Steffi Wanka */ public class WrongParameterValueException extends ParameterException { /** @@ -40,7 +42,7 @@ public class WrongParameterValueException extends ParameterException { * @param parameter the parameter that has a wrong value * @param read the value of the parameter read by the option handler */ - public WrongParameterValueException(Parameter<?,?> parameter, String read) { + public WrongParameterValueException(Parameter<?> parameter, String read) { this("Wrong value of parameter \"" + parameter.getName() + "\".\n" + "Read: " + read + ".\n" + "Expected: " + parameter.getFullDescription()); } @@ -51,8 +53,8 @@ public class WrongParameterValueException extends ParameterException { * @param read the value of the parameter read by the option handler * @param cause the cause */ - public WrongParameterValueException(Parameter<?, ?> parameter, String read, Throwable cause) { - this("Wrong value of parameter \"" + parameter.getName() + "\".\n" + "Read: " + read + ".\n" + "Expected: " + parameter.getFullDescription(), cause); + public WrongParameterValueException(Parameter<?> parameter, String read, Throwable cause) { + this("Wrong value of parameter \"" + parameter.getName() + "\".\n" + "Read: " + read + ".\n" + "Expected: " + parameter.getFullDescription() + "\n" + cause.getMessage(), cause); } /** @@ -63,8 +65,8 @@ public class WrongParameterValueException extends ParameterException { * @param reason detailed error description * @param cause the cause */ - public WrongParameterValueException(Parameter<?, ?> parameter, String read, String reason, Throwable cause) { - this("Wrong value of parameter " + parameter.getName() + ".\n" + "Read: " + read + ".\n" + "Expected: " + parameter.getFullDescription()+"\n"+reason, cause); + public WrongParameterValueException(Parameter<?> parameter, String read, String reason, Throwable cause) { + this("Wrong value of parameter " + parameter.getName() + ".\n" + "Read: " + read + ".\n" + "Expected: " + parameter.getFullDescription() + "\n" + reason + "\n" + cause.getMessage(), cause); } /** @@ -74,8 +76,8 @@ public class WrongParameterValueException extends ParameterException { * @param read the value of the parameter read by the option handler * @param reason detailed error description */ - public WrongParameterValueException(Parameter<?, ?> parameter, String read, String reason) { - this("Wrong value of parameter " + parameter.getName() + ".\n" + "Read: " + read + ".\n" + "Expected: " + parameter.getFullDescription()+"\n"+reason); + public WrongParameterValueException(Parameter<?> parameter, String read, String reason) { + this("Wrong value of parameter " + parameter.getName() + ".\n" + "Read: " + read + ".\n" + "Expected: " + parameter.getFullDescription() + "\n" + reason); } /** @@ -86,7 +88,7 @@ public class WrongParameterValueException extends ParameterException { public WrongParameterValueException(String message) { super(message); } - + /** * Thrown by a Parameterizable object in case of wrong parameter format. * @@ -94,6 +96,6 @@ public class WrongParameterValueException extends ParameterException { * @param e cause */ public WrongParameterValueException(String message, Throwable e) { - super(message,e ); + super(message, e); } -}
\ No newline at end of file +} diff --git a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/constraints/AllOrNoneMustBeSetGlobalConstraint.java b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/constraints/AllOrNoneMustBeSetGlobalConstraint.java index 42209d87..c2448ba5 100644 --- a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/constraints/AllOrNoneMustBeSetGlobalConstraint.java +++ b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/constraints/AllOrNoneMustBeSetGlobalConstraint.java @@ -23,8 +23,8 @@ package de.lmu.ifi.dbs.elki.utilities.optionhandling.constraints; along with this program. If not, see <http://www.gnu.org/licenses/>. */ +import java.util.ArrayList; import java.util.List; -import java.util.Vector; import de.lmu.ifi.dbs.elki.utilities.optionhandling.OptionUtil; import de.lmu.ifi.dbs.elki.utilities.optionhandling.ParameterException; @@ -33,9 +33,7 @@ import de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.Parameter; /** * Global parameter constraint specifying that either all elements of a list of - * parameters ( - * {@link de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.Parameter}) - * must be set, or none of them. + * parameters ({@link Parameter}) must be set, or none of them. * * @author Steffi Wanka */ @@ -43,7 +41,7 @@ public class AllOrNoneMustBeSetGlobalConstraint implements GlobalParameterConstr /** * List of parameters to be checked */ - private List<Parameter<?, ?>> parameterList; + private List<Parameter<?>> parameterList; /** * Constructs a global parameter constraint for testing if either all elements @@ -51,7 +49,7 @@ public class AllOrNoneMustBeSetGlobalConstraint implements GlobalParameterConstr * * @param parameters list of parameters to be checked */ - public AllOrNoneMustBeSetGlobalConstraint(List<Parameter<?, ?>> parameters) { + public AllOrNoneMustBeSetGlobalConstraint(List<Parameter<?>> parameters) { this.parameterList = parameters; } @@ -62,10 +60,10 @@ public class AllOrNoneMustBeSetGlobalConstraint implements GlobalParameterConstr @Override public void test() throws ParameterException { - Vector<String> set = new Vector<String>(); - Vector<String> notSet = new Vector<String>(); + ArrayList<String> set = new ArrayList<String>(); + ArrayList<String> notSet = new ArrayList<String>(); - for(Parameter<?, ?> p : parameterList) { + for(Parameter<?> p : parameterList) { if(p.isDefined()) { set.add(p.getName()); } diff --git a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/constraints/EqualStringConstraint.java b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/constraints/EqualStringConstraint.java index ad567fa1..4c4a8de3 100644 --- a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/constraints/EqualStringConstraint.java +++ b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/constraints/EqualStringConstraint.java @@ -57,16 +57,16 @@ public class EqualStringConstraint implements ParameterConstraint<String> { } private String constraintStrings() { - StringBuffer buffer = new StringBuffer(); - buffer.append("["); + StringBuilder buffer = new StringBuilder(); + buffer.append('['); for(int i = 0; i < testStrings.length; i++) { buffer.append(testStrings[i]); if(i != testStrings.length - 1) { - buffer.append(","); + buffer.append(','); } } - buffer.append("]"); + buffer.append(']'); return buffer.toString(); } diff --git a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/constraints/GlobalListSizeConstraint.java b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/constraints/GlobalListSizeConstraint.java index b30a45d9..1bad7d00 100644 --- a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/constraints/GlobalListSizeConstraint.java +++ b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/constraints/GlobalListSizeConstraint.java @@ -71,7 +71,7 @@ public class GlobalListSizeConstraint implements GlobalParameterConstraint { return; } - if(list.getListSize() != length.getValue()) { + if(list.getListSize() != length.getValue().intValue()) { throw new WrongParameterValueException("Global Parameter Constraint Error." + "\nThe size of the list parameter \"" + list.getName() + "\" must be " + length.getValue() + ", current size is " + list.getListSize() + ". The value is defined by the integer parameter " + length.getName() + ".\n"); } } diff --git a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/constraints/GlobalVectorListElementSizeConstraint.java b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/constraints/GlobalVectorListElementSizeConstraint.java index 740d9545..6bc9fd2d 100644 --- a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/constraints/GlobalVectorListElementSizeConstraint.java +++ b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/constraints/GlobalVectorListElementSizeConstraint.java @@ -75,7 +75,7 @@ public class GlobalVectorListElementSizeConstraint implements GlobalParameterCon } for(List<Double> vec : vector.getValue()) { - if(vec.size() != size.getValue()) { + if(vec.size() != size.getValue().intValue()) { throw new WrongParameterValueException("Global Parameter Constraint Error.\n" + "The vectors of vector list parameter " + vector.getName() + " have not the required dimension of " + size.getValue() + " given by integer parameter " + size.getName() + "."); } } diff --git a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/constraints/GreaterConstraint.java b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/constraints/GreaterConstraint.java index 22cc50f9..17a9a54d 100644 --- a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/constraints/GreaterConstraint.java +++ b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/constraints/GreaterConstraint.java @@ -28,42 +28,63 @@ import de.lmu.ifi.dbs.elki.utilities.optionhandling.WrongParameterValueException import de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.NumberParameter; /** - * Represents a parameter constraint for testing if the value of the - * number parameter ({@link NumberParameter}) - * tested is greater than the specified constraint value. - * + * Represents a parameter constraint for testing if the value of the number + * parameter ({@link NumberParameter}) tested is greater than the specified + * constraint value. + * * @author Steffi Wanka */ public class GreaterConstraint extends AbstractNumberConstraint<Number> { - /** - * Creates a Greater-Than-Number parameter constraint. - * <p/> - * That is, the value of the number - * parameter has to be greater than the given constraint value. - * - * @param constraintValue the constraint value - */ - public GreaterConstraint(Number constraintValue) { - super(constraintValue); - } + /** + * Creates a Greater-Than-Number parameter constraint. + * <p/> + * That is, the value of the number parameter has to be greater than the given + * constraint value. + * + * @param constraintValue the constraint value + */ + public GreaterConstraint(Number constraintValue) { + super(constraintValue); + } - /** - * Checks if the number value given by the number parameter is greater than - * the constraint value. If not, a parameter exception is thrown. - * - */ - @Override - public void test(Number t) throws ParameterException { - if (t.doubleValue() <= constraintValue.doubleValue()) { - throw new WrongParameterValueException("Parameter Constraint Error:\n" - + "The parameter value specified has to be greater than " - + constraintValue.toString() + - ". (current value: " + t.doubleValue() + ")\n"); - } - } + /** + * Creates a Greater-Than-Number parameter constraint. + * <p/> + * That is, the value of the number parameter has to be greater than the given + * constraint value. + * + * @param constraintValue the constraint value + */ + public GreaterConstraint(int constraintValue) { + super(Integer.valueOf(constraintValue)); + } - @Override - public String getDescription(String parameterName) { - return parameterName + " > " + constraintValue; + /** + * Creates a Greater-Than-Number parameter constraint. + * <p/> + * That is, the value of the number parameter has to be greater than the given + * constraint value. + * + * @param constraintValue the constraint value + */ + public GreaterConstraint(double constraintValue) { + super(Double.valueOf(constraintValue)); + } + + /** + * Checks if the number value given by the number parameter is greater than + * the constraint value. If not, a parameter exception is thrown. + * + */ + @Override + public void test(Number t) throws ParameterException { + if (t.doubleValue() <= constraintValue.doubleValue()) { + throw new WrongParameterValueException("Parameter Constraint Error:\n" + "The parameter value specified has to be greater than " + constraintValue.toString() + ". (current value: " + t.doubleValue() + ")\n"); } + } + + @Override + public String getDescription(String parameterName) { + return parameterName + " > " + constraintValue; + } } diff --git a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/constraints/GreaterEqualConstraint.java b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/constraints/GreaterEqualConstraint.java index 26145620..7eb0de83 100644 --- a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/constraints/GreaterEqualConstraint.java +++ b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/constraints/GreaterEqualConstraint.java @@ -28,45 +28,64 @@ import de.lmu.ifi.dbs.elki.utilities.optionhandling.WrongParameterValueException import de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.NumberParameter; /** - * Represents a Greater-Equal-Than-Number parameter constraint. The - * value of the number parameter ({@link NumberParameter}) - * tested has to be greater equal than the specified - * constraint value. - * + * Represents a Greater-Equal-Than-Number parameter constraint. The value of the + * number parameter ({@link NumberParameter}) tested has to be greater equal + * than the specified constraint value. + * * @author Steffi Wanka */ public class GreaterEqualConstraint extends AbstractNumberConstraint<Number> { - /** - * Creates a Greater-Equal parameter constraint. - * <p/> - * That is, the value of the number - * parameter given has to be greater equal than the constraint value given. - * - * @param constraintValue the constraint value - */ - public GreaterEqualConstraint(Number constraintValue) { - super(constraintValue); - } + /** + * Creates a Greater-Equal parameter constraint. + * <p/> + * That is, the value of the number parameter given has to be greater equal + * than the constraint value given. + * + * @param constraintValue the constraint value + */ + public GreaterEqualConstraint(Number constraintValue) { + super(constraintValue); + } - /** - * Checks if the number value given by the number parameter is - * greater equal than the constraint - * value. If not, a parameter exception is thrown. - * - */ - @Override - public void test(Number t) throws ParameterException { - if (t.doubleValue() < constraintValue.doubleValue()) { - throw new WrongParameterValueException("Parameter Constraint Error: \n" - + "The parameter value specified has to be greater equal than " - + constraintValue.toString() + - ". (current value: " + t.doubleValue() + ")\n"); - } - } + /** + * Creates a Greater-Equal parameter constraint. + * <p/> + * That is, the value of the number parameter given has to be greater equal + * than the constraint value given. + * + * @param constraintValue the constraint value + */ + public GreaterEqualConstraint(int constraintValue) { + super(Integer.valueOf(constraintValue)); + } - @Override - public String getDescription(String parameterName) { - return parameterName + " >= " + constraintValue; + /** + * Creates a Greater-Equal parameter constraint. + * <p/> + * That is, the value of the number parameter given has to be greater equal + * than the constraint value given. + * + * @param constraintValue the constraint value + */ + public GreaterEqualConstraint(double constraintValue) { + super(Double.valueOf(constraintValue)); + } + + /** + * Checks if the number value given by the number parameter is greater equal + * than the constraint value. If not, a parameter exception is thrown. + * + */ + @Override + public void test(Number t) throws ParameterException { + if (t.doubleValue() < constraintValue.doubleValue()) { + throw new WrongParameterValueException("Parameter Constraint Error: \n" + "The parameter value specified has to be greater equal than " + constraintValue.toString() + ". (current value: " + t.doubleValue() + ")\n"); } + } + + @Override + public String getDescription(String parameterName) { + return parameterName + " >= " + constraintValue; + } } diff --git a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/constraints/IntervalConstraint.java b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/constraints/IntervalConstraint.java index f9e93a5b..db6a1ed8 100644 --- a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/constraints/IntervalConstraint.java +++ b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/constraints/IntervalConstraint.java @@ -37,7 +37,10 @@ import de.lmu.ifi.dbs.elki.utilities.optionhandling.WrongParameterValueException * @author Steffi Wanka * * @apiviz.uses de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.NumberParameter + * + * @deprecated Use two constraints instead. */ +@Deprecated public class IntervalConstraint implements ParameterConstraint<Number> { /** * Available interval boundary types types: @@ -110,6 +113,56 @@ public class IntervalConstraint implements ParameterConstraint<Number> { } /** + * Creates an IntervalConstraint parameter constraint. + * <p/> + * That is, the value of the number parameter given has to be greater than (or + * equal to, if specified) than the specified low constraint value and less + * than (or equal to, if specified) than the specified high constraint value. + * + * @param lowConstraintValue the low constraint value (left interval boundary) + * @param lowBoundary the interval boundary for the low constraint value (see {@link IntervalBoundary}) + * @param highConstraintValue the high constraint value (right interval + * boundary) + * @param highBoundary the interval boundary for the high constraint value + * (see {@link IntervalBoundary}) + */ + public IntervalConstraint(int lowConstraintValue, IntervalBoundary lowBoundary, int highConstraintValue, IntervalBoundary highBoundary) { + if(lowConstraintValue >= highConstraintValue) { + throw new IllegalArgumentException("Left interval boundary is greater than " + "or equal to right interval boundary!"); + } + + this.lowConstraintValue = Integer.valueOf(lowConstraintValue); + this.lowBoundary = lowBoundary; + this.highConstraintValue = Integer.valueOf(highConstraintValue); + this.highBoundary = highBoundary; + } + + /** + * Creates an IntervalConstraint parameter constraint. + * <p/> + * That is, the value of the number parameter given has to be greater than (or + * equal to, if specified) than the specified low constraint value and less + * than (or equal to, if specified) than the specified high constraint value. + * + * @param lowConstraintValue the low constraint value (left interval boundary) + * @param lowBoundary the interval boundary for the low constraint value (see {@link IntervalBoundary}) + * @param highConstraintValue the high constraint value (right interval + * boundary) + * @param highBoundary the interval boundary for the high constraint value + * (see {@link IntervalBoundary}) + */ + public IntervalConstraint(double lowConstraintValue, IntervalBoundary lowBoundary, double highConstraintValue, IntervalBoundary highBoundary) { + if(lowConstraintValue >= highConstraintValue) { + throw new IllegalArgumentException("Left interval boundary is greater than " + "or equal to right interval boundary!"); + } + + this.lowConstraintValue = Double.valueOf(lowConstraintValue); + this.lowBoundary = lowBoundary; + this.highConstraintValue = Double.valueOf(highConstraintValue); + this.highBoundary = highBoundary; + } + + /** * Checks if the number value given by the number parameter is greater equal * than the constraint value. If not, a parameter exception is thrown. * diff --git a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/constraints/LessConstraint.java b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/constraints/LessConstraint.java index 0994425e..a8369304 100644 --- a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/constraints/LessConstraint.java +++ b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/constraints/LessConstraint.java @@ -28,41 +28,64 @@ import de.lmu.ifi.dbs.elki.utilities.optionhandling.WrongParameterValueException import de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.NumberParameter; /** - * Represents a Less-Than-Number parameter constraint. The value of the - * number parameter ({@link NumberParameter}) tested has to be less than the specified constraint value. - * + * Represents a Less-Than-Number parameter constraint. The value of the number + * parameter ({@link NumberParameter}) tested has to be less than the specified + * constraint value. + * * @author Steffi Wanka */ public class LessConstraint extends AbstractNumberConstraint<Number> { - /** - * Creates a Less-Than-Number parameter constraint. - * <p/> - * That is, the value of the number - * parameter tested has to be less than the constraint value given. - * - * @param constraintValue the constraint value - */ - public LessConstraint(Number constraintValue) { - super(constraintValue); - } + /** + * Creates a Less-Than-Number parameter constraint. + * <p/> + * That is, the value of the number parameter tested has to be less than the + * constraint value given. + * + * @param constraintValue the constraint value + */ + public LessConstraint(Number constraintValue) { + super(constraintValue); + } - /** - * Checks if the number value given by the number parameter is less than the constraint value. - * If not, a parameter exception is thrown. - * - */ - @Override - public void test(Number t) throws ParameterException { - if (t.doubleValue() >= constraintValue.doubleValue()) { - throw new WrongParameterValueException("Parameter Constraint Error: \n" - + "The parameter value specified has to be less than " + constraintValue.toString() - + ". (current value: " + t.doubleValue() + ")\n"); - } - } + /** + * Creates a Less-Than-Number parameter constraint. + * <p/> + * That is, the value of the number parameter tested has to be less than the + * constraint value given. + * + * @param constraintValue the constraint value + */ + public LessConstraint(int constraintValue) { + super(Integer.valueOf(constraintValue)); + } - @Override - public String getDescription(String parameterName) { - return parameterName + " < " + constraintValue; + /** + * Creates a Less-Than-Number parameter constraint. + * <p/> + * That is, the value of the number parameter tested has to be less than the + * constraint value given. + * + * @param constraintValue the constraint value + */ + public LessConstraint(double constraintValue) { + super(Double.valueOf(constraintValue)); + } + + /** + * Checks if the number value given by the number parameter is less than the + * constraint value. If not, a parameter exception is thrown. + * + */ + @Override + public void test(Number t) throws ParameterException { + if (t.doubleValue() >= constraintValue.doubleValue()) { + throw new WrongParameterValueException("Parameter Constraint Error: \n" + "The parameter value specified has to be less than " + constraintValue.toString() + ". (current value: " + t.doubleValue() + ")\n"); } + } + + @Override + public String getDescription(String parameterName) { + return parameterName + " < " + constraintValue; + } } diff --git a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/constraints/LessEqualConstraint.java b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/constraints/LessEqualConstraint.java index 712630e8..e35381b3 100644 --- a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/constraints/LessEqualConstraint.java +++ b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/constraints/LessEqualConstraint.java @@ -29,40 +29,61 @@ import de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.NumberParameter; /** * Represents a Less-Equal-Than-Number parameter constraint. The value of the - * number parameter ({@link NumberParameter}) tested has to be less equal than the specified constraint - * value. - * + * number parameter ({@link NumberParameter}) tested has to be less equal than + * the specified constraint value. + * * @author Steffi Wanka */ public class LessEqualConstraint extends AbstractNumberConstraint<Number> { - /** - * Creates a Less-Equal-Than-Number parameter constraint. - * <p/> - * That is, the value of - * the appropriate number parameter has to be less equal than the given constraint value. - * - * @param constraintValue the constraint value - */ - public LessEqualConstraint(Number constraintValue) { - super(constraintValue); - } + /** + * Creates a Less-Equal-Than-Number parameter constraint. + * <p/> + * That is, the value of the appropriate number parameter has to be less equal + * than the given constraint value. + * + * @param constraintValue the constraint value + */ + public LessEqualConstraint(Number constraintValue) { + super(constraintValue); + } - /** - * Checks if the number value given by the number parameter is less equal than - * the constraint value. If not, a parameter exception is thrown. - * - */ - @Override - public void test(Number t) throws ParameterException { - if (t.doubleValue() > constraintValue.doubleValue()) { - throw new WrongParameterValueException("Parameter Constraint Error: \n" - + "The parameter value specified has to be less equal than " - + constraintValue.toString() + ". (current value: " + t.doubleValue() + ")\n"); - } - } + /** + * Creates a Less-Equal-Than-Number parameter constraint. + * <p/> + * That is, the value of the appropriate number parameter has to be less equal + * than the given constraint value. + * + * @param constraintValue the constraint value + */ + public LessEqualConstraint(double constraintValue) { + super(Double.valueOf(constraintValue)); + } - @Override - public String getDescription(String parameterName) { - return parameterName + " <= " + constraintValue; + /** + * Creates a Less-Equal-Than-Number parameter constraint. + * <p/> + * That is, the value of the appropriate number parameter has to be less equal + * than the given constraint value. + * + * @param constraintValue the constraint value + */ + public LessEqualConstraint(int constraintValue) { + super(Integer.valueOf(constraintValue)); + } + + /** + * Checks if the number value given by the number parameter is less equal than + * the constraint value. If not, a parameter exception is thrown. + */ + @Override + public void test(Number t) throws ParameterException { + if (t.doubleValue() > constraintValue.doubleValue()) { + throw new WrongParameterValueException("Parameter Constraint Error: \n" + "The parameter value specified has to be less equal than " + constraintValue.toString() + ". (current value: " + t.doubleValue() + ")\n"); } + } + + @Override + public String getDescription(String parameterName) { + return parameterName + " <= " + constraintValue; + } } diff --git a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/constraints/ListEachConstraint.java b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/constraints/ListEachConstraint.java new file mode 100644 index 00000000..7dab4006 --- /dev/null +++ b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/constraints/ListEachConstraint.java @@ -0,0 +1,90 @@ +package de.lmu.ifi.dbs.elki.utilities.optionhandling.constraints; + +/* + 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.util.ArrayList; +import java.util.List; + +import de.lmu.ifi.dbs.elki.utilities.optionhandling.ParameterException; + +/** + * Applies constraints to all elements of a list. + * + * @author Erich Schubert + * + * @apiviz.composedOf ParameterConstraint oneway 1 n + */ +public class ListEachConstraint<T> implements ParameterConstraint<List<T>> { + /** + * Constraints + */ + private List<ParameterConstraint<? super T>> constraints; + + /** + * Constructor. + */ + public ListEachConstraint() { + super(); + this.constraints = new ArrayList<ParameterConstraint<? super T>>(); + } + + /** + * Constructor. + * + * @param constraint Constraint to apply to all elements + */ + public ListEachConstraint(ParameterConstraint<? super T> constraint) { + super(); + this.constraints = new ArrayList<ParameterConstraint<? super T>>(1); + this.constraints.add(constraint); + } + + /** + * Add a constraint to this operator. + * + * @param constraint Constraint + */ + public void addConstraint(ParameterConstraint<? super T> constraint) { + this.constraints.add(constraint); + } + + @Override + public void test(List<T> t) throws ParameterException { + for (T e : t) { + for (ParameterConstraint<? super T> c : constraints) { + c.test(e); + } + } + } + + @Override + public String getDescription(String parameterName) { + final String all = "all elements of " + parameterName; + StringBuilder b = new StringBuilder(); + for (ParameterConstraint<? super T> c : constraints) { + b.append(c.getDescription(all)); + } + return b.toString(); + } +} diff --git a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/constraints/ListGreaterEqualConstraint.java b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/constraints/ListGreaterEqualConstraint.java deleted file mode 100644 index 0f33b9b2..00000000 --- a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/constraints/ListGreaterEqualConstraint.java +++ /dev/null @@ -1,74 +0,0 @@ -package de.lmu.ifi.dbs.elki.utilities.optionhandling.constraints; - -/* - 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.optionhandling.ParameterException; -import de.lmu.ifi.dbs.elki.utilities.optionhandling.WrongParameterValueException; - -import java.util.List; - -/** - * Represents a Greater-Equal-Than-Number parameter constraint for a list of number values. - * All values of the list parameter ({@link de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.ListParameter}) - * tested have to be greater than or equal to the specified constraint value. - * - * @author Elke Achtert - * @param <N> Number type - */ -public class ListGreaterEqualConstraint<N extends Number> extends AbstractNumberConstraint<List<N>> { - /** - * Creates a Greater-Equal-Than-Number parameter constraint. - * <p/> - * That is, all values of the list parameter - * tested have to be greater than or equal to the specified constraint value. - * - * @param constraintValue parameter constraint value - */ - public ListGreaterEqualConstraint(N constraintValue) { - super(constraintValue); - } - - /** - * Checks if all number values of the specified list parameter - * are greater than or equal to the constraint value. - * If not, a parameter exception is thrown. - * - */ - @Override - public void test(List<N> t) throws ParameterException { - for (Number n : t) { - if (n.doubleValue() < constraintValue.doubleValue()) { - throw new WrongParameterValueException("Parameter Constraint Error: \n" - + "The parameter values specified have to be greater than or equal to " + constraintValue.toString() - + ". (current value: " + t + ")\n"); - } - } - } - - @Override - public String getDescription(String parameterName) { - return "all elements of " + parameterName + " < " + constraintValue; - } - -} diff --git a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/constraints/ListParameterNoDuplicateValueConstraint.java b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/constraints/ListParameterNoDuplicateValueConstraint.java new file mode 100644 index 00000000..6545cc9d --- /dev/null +++ b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/constraints/ListParameterNoDuplicateValueConstraint.java @@ -0,0 +1,78 @@ +package de.lmu.ifi.dbs.elki.utilities.optionhandling.constraints; + +/* + 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.util.Arrays; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import de.lmu.ifi.dbs.elki.utilities.optionhandling.ParameterException; +import de.lmu.ifi.dbs.elki.utilities.optionhandling.WrongParameterValueException; + +/** + * Parameter constraint specifying that a parameter list + * is not allowed to have duplicate values. + * + * @author Arthur Zimek + */ +public class ListParameterNoDuplicateValueConstraint<T extends Object> implements ParameterConstraint<List<T>> { + + /** + * Constructs a Not-Equal-Value parameter constraint. That is, the + * elements of a list of parameter values are not allowed to have equal + * values. + * + */ + public ListParameterNoDuplicateValueConstraint() { + } + + + + /** + * Checks if the elements of the list of parameter values do have different + * values. If not, a parameter exception is thrown. + * + */ + @Override + public void test(List<T> list) throws ParameterException { + Set<T> values = new HashSet<T>(); + + for(T pv : list) { + if(!values.add(pv)) { + Object[] parametervaluesarr = list.toArray(); + throw new WrongParameterValueException("Global Parameter Constraint Error:\n" + "Parameter values must have different values. Current values: " + Arrays.deepToString(parametervaluesarr) + ".\n"); + } + } + } + + @Override + public String getDescription(String parameterName) { + return "Values for parameter "+parameterName+" must have different values."; + } + + + + +}
\ No newline at end of file diff --git a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/constraints/ListSizeConstraint.java b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/constraints/ListSizeConstraint.java index f2ef2069..82b3ae7e 100644 --- a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/constraints/ListSizeConstraint.java +++ b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/constraints/ListSizeConstraint.java @@ -37,10 +37,8 @@ import de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.ListParameter; * @author Steffi Wanka * * @apiviz.uses de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.ListParameter - * - * @param <T> Parameter type */ -public class ListSizeConstraint<T> implements ParameterConstraint<List<T>> { +public class ListSizeConstraint implements ParameterConstraint<List<?>> { /** * The list size constraint. */ @@ -63,7 +61,7 @@ public class ListSizeConstraint<T> implements ParameterConstraint<List<T>> { * equal to the list size constraint specified. */ @Override - public void test(List<T> t) throws ParameterException { + public void test(List<?> t) throws ParameterException { if(t.size() != sizeConstraint) { throw new WrongParameterValueException("Parameter Constraint Error.\n" + "List parameter has not the required size. (Requested size: " + +sizeConstraint + ", current size: " + t.size() + ").\n"); } diff --git a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/constraints/NoDuplicateValueGlobalConstraint.java b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/constraints/NoDuplicateValueGlobalConstraint.java index 04472fa5..39ab2680 100644 --- a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/constraints/NoDuplicateValueGlobalConstraint.java +++ b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/constraints/NoDuplicateValueGlobalConstraint.java @@ -32,6 +32,7 @@ import de.lmu.ifi.dbs.elki.utilities.optionhandling.OptionUtil; 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.parameters.NumberParameter; +import de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.AbstractParameter; import de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.Parameter; /** @@ -44,7 +45,7 @@ public class NoDuplicateValueGlobalConstraint implements GlobalParameterConstrai /** * List of number parameters to be checked. */ - private List<? extends Parameter<?, ?>> parameters; + private List<? extends AbstractParameter<?>> parameters; /** * Constructs a Not-Equal-Value global parameter constraint. That is, the @@ -53,7 +54,7 @@ public class NoDuplicateValueGlobalConstraint implements GlobalParameterConstrai * * @param parameters list of number parameters to be tested */ - public NoDuplicateValueGlobalConstraint(List<? extends Parameter<?, ?>> parameters) { + public NoDuplicateValueGlobalConstraint(List<? extends AbstractParameter<?>> parameters) { this.parameters = parameters; } @@ -64,7 +65,7 @@ public class NoDuplicateValueGlobalConstraint implements GlobalParameterConstrai * * @param parameters list of number parameters to be tested */ - public NoDuplicateValueGlobalConstraint(Parameter<?, ?>... parameters) { + public NoDuplicateValueGlobalConstraint(AbstractParameter<?>... parameters) { this.parameters = Arrays.asList(parameters); } @@ -77,7 +78,7 @@ public class NoDuplicateValueGlobalConstraint implements GlobalParameterConstrai public void test() throws ParameterException { Set<Object> numbers = new HashSet<Object>(); - for(Parameter<?, ?> param : parameters) { + for(Parameter<?> param : parameters) { if(param.isDefined()) { if(!numbers.add(param.getValue())) { throw new WrongParameterValueException("Global Parameter Constraint Error:\n" + "Parameters " + OptionUtil.optionsNamesToString(parameters) + " must have different values. Current values: " + OptionUtil.parameterNamesAndValuesToString(parameters) + ".\n"); diff --git a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/constraints/OneMustBeSetGlobalConstraint.java b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/constraints/OneMustBeSetGlobalConstraint.java index 1c8c6b94..c0109ac5 100644 --- a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/constraints/OneMustBeSetGlobalConstraint.java +++ b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/constraints/OneMustBeSetGlobalConstraint.java @@ -41,7 +41,7 @@ public class OneMustBeSetGlobalConstraint implements GlobalParameterConstraint { /** * List of parameters to be checked. */ - private List<Parameter<?,?>> parameters; + private List<Parameter<?>> parameters; /** * Creates a One-Must-Be-Set global parameter constraint. That is, at least @@ -49,7 +49,7 @@ public class OneMustBeSetGlobalConstraint implements GlobalParameterConstraint { * * @param params list of parameters */ - public OneMustBeSetGlobalConstraint(List<Parameter<?,?>> params) { + public OneMustBeSetGlobalConstraint(List<Parameter<?>> params) { parameters = params; } @@ -60,8 +60,8 @@ public class OneMustBeSetGlobalConstraint implements GlobalParameterConstraint { */ @Override public void test() throws ParameterException { - for(Parameter<?,?> p : parameters) { - if(p.isDefined()) { + for (Parameter<?> p : parameters) { + if (p.isDefined()) { return; } } @@ -72,4 +72,4 @@ public class OneMustBeSetGlobalConstraint implements GlobalParameterConstraint { public String getDescription() { return "At least one of the parameters " + OptionUtil.optionsNamesToString(parameters) + " has to be set."; } -}
\ No newline at end of file +} diff --git a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/constraints/OnlyOneIsAllowedToBeSetGlobalConstraint.java b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/constraints/OnlyOneIsAllowedToBeSetGlobalConstraint.java index 74a1955b..22db8511 100644 --- a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/constraints/OnlyOneIsAllowedToBeSetGlobalConstraint.java +++ b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/constraints/OnlyOneIsAllowedToBeSetGlobalConstraint.java @@ -23,8 +23,8 @@ package de.lmu.ifi.dbs.elki.utilities.optionhandling.constraints; along with this program. If not, see <http://www.gnu.org/licenses/>. */ +import java.util.ArrayList; import java.util.List; -import java.util.Vector; import de.lmu.ifi.dbs.elki.utilities.optionhandling.OptionUtil; import de.lmu.ifi.dbs.elki.utilities.optionhandling.ParameterException; @@ -42,7 +42,7 @@ public class OnlyOneIsAllowedToBeSetGlobalConstraint implements GlobalParameterC /** * List of parameters to be checked. */ - private List<Parameter<?, ?>> parameters; + private List<Parameter<?>> parameters; /** * Constructs a global parameter constraint for testing if only one parameter @@ -50,23 +50,22 @@ public class OnlyOneIsAllowedToBeSetGlobalConstraint implements GlobalParameterC * * @param params list of parameters to be checked */ - public OnlyOneIsAllowedToBeSetGlobalConstraint(List<Parameter<?, ?>> params) { + public OnlyOneIsAllowedToBeSetGlobalConstraint(List<Parameter<?>> params) { parameters = params; } /** * Checks if only one parameter of a list of parameters is set. If not, a * parameter exception is thrown. - * */ @Override public void test() throws ParameterException { - Vector<String> set = new Vector<String>(); - for(Parameter<?, ?> p : parameters) { + ArrayList<String> set = new ArrayList<String>(); + for(Parameter<?> p : parameters) { if(p.isDefined()) { // FIXME: Retire the use of this constraint for Flags! if(p instanceof Flag) { - if (((Flag)p).getValue()) { + if (((Flag)p).getValue().booleanValue()) { set.add(p.getName()); } } else { diff --git a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/constraints/ParameterFlagGlobalConstraint.java b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/constraints/ParameterFlagGlobalConstraint.java index befc0788..ff2e6675 100644 --- a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/constraints/ParameterFlagGlobalConstraint.java +++ b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/constraints/ParameterFlagGlobalConstraint.java @@ -28,26 +28,28 @@ import java.util.List; import de.lmu.ifi.dbs.elki.utilities.optionhandling.ParameterException; import de.lmu.ifi.dbs.elki.utilities.optionhandling.UnusedParameterException; import de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.Flag; +import de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.AbstractParameter; import de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.Parameter; /** * Global parameter constraint describing the dependency of a parameter ( - * {@link Parameter}) on a given flag ({@link Flag}). Depending on the status of - * the flag the parameter is tested for keeping its constraints or not. + * {@link AbstractParameter}) on a given flag ({@link Flag}). Depending on the + * status of the flag the parameter is tested for keeping its constraints or + * not. * * @author Steffi Wanka * * @apiviz.uses de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.Flag - * @apiviz.uses de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.Parameter + * @apiviz.uses + * de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.Parameter * - * @param <C> Constraint type * @param <S> Parameter type */ -public class ParameterFlagGlobalConstraint<S, C extends S> implements GlobalParameterConstraint { +public class ParameterFlagGlobalConstraint<S> implements GlobalParameterConstraint { /** * Parameter possibly to be checked. */ - private Parameter<S,C> param; + private Parameter<S> param; /** * Flag the checking of the parameter constraints is dependent on. @@ -62,7 +64,7 @@ public class ParameterFlagGlobalConstraint<S, C extends S> implements GlobalPara /** * List of parameter constraints. */ - private List<ParameterConstraint<S>> cons; + private List<ParameterConstraint<? super S>> cons; /** * Constructs a global parameter constraint specifying that the testing of the @@ -70,12 +72,13 @@ public class ParameterFlagGlobalConstraint<S, C extends S> implements GlobalPara * the status of the flag given. * * @param p parameter possibly to be checked - * @param c a list of parameter constraints, if the value is null, the parameter is just tested if it is set. + * @param c a list of parameter constraints, if the value is null, the + * parameter is just tested if it is set. * @param f flag controlling the checking of the parameter constraints * @param flagConstraint indicates at which status of the flag the parameter * is to be checked */ - public ParameterFlagGlobalConstraint(Parameter<S, C> p, List<ParameterConstraint<S>> c, Flag f, boolean flagConstraint) { + public ParameterFlagGlobalConstraint(Parameter<S> p, List<ParameterConstraint<? super S>> c, Flag f, boolean flagConstraint) { param = p; flag = f; this.flagConstraint = flagConstraint; @@ -91,9 +94,9 @@ public class ParameterFlagGlobalConstraint<S, C extends S> implements GlobalPara @Override public void test() throws ParameterException { // only check constraints of param if flag is set - if(flagConstraint == flag.getValue()) { + if(flagConstraint == flag.getValue().booleanValue()) { if(cons != null) { - for(ParameterConstraint<? super C> c : cons) { + for(ParameterConstraint<? super S> c : cons) { c.test(param.getValue()); } } @@ -107,14 +110,14 @@ public class ParameterFlagGlobalConstraint<S, C extends S> implements GlobalPara @Override public String getDescription() { - StringBuffer description = new StringBuffer(); + StringBuilder description = new StringBuilder(); if(flagConstraint) { description.append("If ").append(flag.getName()); description.append(" is set, the following constraints for parameter "); description.append(param.getName()).append(" have to be fullfilled: "); if(cons != null) { for(int i = 0; i < cons.size(); i++) { - ParameterConstraint<? super C> c = cons.get(i); + ParameterConstraint<? super S> c = cons.get(i); if(i > 0) { description.append(", "); } @@ -122,7 +125,7 @@ public class ParameterFlagGlobalConstraint<S, C extends S> implements GlobalPara } } else { - description.append(param.getName()+" must be set."); + description.append(param.getName()).append(" must be set."); } } return description.toString(); diff --git a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/package-info.java b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/package-info.java index 2756327e..593c11d2 100644 --- a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/package-info.java +++ b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/package-info.java @@ -19,7 +19,7 @@ * </li> * * <li><b>Parameter Object</b>: To obtain a value, you <em>must</em> use a - * {@link de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.Parameter Parameter} + * {@link de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.AbstractParameter Parameter} * object. <br /> * Parameter objects handle <em>parsing</em> of values into the desired type, and various * subclasses for common types are provided. It is not desirable to subclass these types diff --git a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameterization/AbstractParameterization.java b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameterization/AbstractParameterization.java index 156dca1d..7cc0055e 100644 --- a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameterization/AbstractParameterization.java +++ b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameterization/AbstractParameterization.java @@ -23,10 +23,13 @@ package de.lmu.ifi.dbs.elki.utilities.optionhandling.parameterization; along with this program. If not, see <http://www.gnu.org/licenses/>. */ +import java.util.ArrayList; import java.util.Collection; +import java.util.List; import de.lmu.ifi.dbs.elki.logging.Logging; import de.lmu.ifi.dbs.elki.utilities.ClassGenericsUtil; +import de.lmu.ifi.dbs.elki.utilities.exceptions.AbortException; import de.lmu.ifi.dbs.elki.utilities.optionhandling.InternalParameterizationErrors; import de.lmu.ifi.dbs.elki.utilities.optionhandling.ParameterException; import de.lmu.ifi.dbs.elki.utilities.optionhandling.constraints.GlobalParameterConstraint; @@ -43,12 +46,12 @@ public abstract class AbstractParameterization implements Parameterization { /** * Errors */ - java.util.Vector<ParameterException> errors = new java.util.Vector<ParameterException>(); + List<ParameterException> errors = new ArrayList<ParameterException>(); /** * The logger of the class. */ - private final static Logging logger = Logging.getLogger(AbstractParameterization.class); + private static final Logging LOG = Logging.getLogger(AbstractParameterization.class); @Override public Collection<ParameterException> getErrors() { @@ -70,11 +73,11 @@ public abstract class AbstractParameterization implements Parameterization { */ public synchronized void logAndClearReportedErrors() { for(ParameterException e : getErrors()) { - if(logger.isDebugging()) { - logger.warning(e.getMessage(), e); + if(LOG.isDebugging()) { + LOG.warning(e.getMessage(), e); } else { - logger.warning(e.getMessage()); + LOG.warning(e.getMessage()); } } clearErrors(); @@ -86,7 +89,7 @@ public abstract class AbstractParameterization implements Parameterization { public synchronized void clearErrors() { // Do NOT use errors.clear(), since we might have an error report // referencing the collection! - errors = new java.util.Vector<ParameterException>(); + errors = new ArrayList<ParameterException>(); } /** @@ -95,11 +98,11 @@ public abstract class AbstractParameterization implements Parameterization { * @throws RuntimeException if any error has occurred. */ // TODO: make a multi-exception class? - public void failOnErrors() throws RuntimeException { + public void failOnErrors() throws AbortException { final int numerror = getErrors().size(); if(numerror > 0) { logAndClearReportedErrors(); - throw new RuntimeException(numerror + " errors occurred during parameterization."); + throw new AbortException(numerror + " errors occurred during parameterization."); } } @@ -117,9 +120,9 @@ public abstract class AbstractParameterization implements Parameterization { } @Override - public final boolean grab(Parameter<?, ?> opt) { + public final boolean grab(Parameter<?> opt) { if(opt.isDefined()) { - logger.warning("Option " + opt.getName() + " is already set!"); + LOG.warning("Option " + opt.getName() + " is already set!"); } try { if(setValueForOption(opt)) { @@ -146,12 +149,15 @@ public abstract class AbstractParameterization implements Parameterization { * @throws ParameterException on assignment errors. */ @Override - public abstract boolean setValueForOption(Parameter<?, ?> opt) throws ParameterException; + public abstract boolean setValueForOption(Parameter<?> opt) throws ParameterException; - /** Upon destruction, report any errors that weren't handled yet. */ + /** Upon destruction, report any errors that weren't handled yet. + * + * @throws Throwable Errors */ @Override - public void finalize() { + public void finalize() throws Throwable { failOnErrors(); + super.finalize(); } @Override @@ -172,7 +178,7 @@ public abstract class AbstractParameterization implements Parameterization { return ClassGenericsUtil.tryInstantiate(r, c, this); } catch(Exception e) { - logger.exception(e); + LOG.exception(e); reportError(new InternalParameterizationErrors("Error instantiating internal class: "+c.getName(), e)); return null; } @@ -184,7 +190,7 @@ public abstract class AbstractParameterization implements Parameterization { return ClassGenericsUtil.tryInstantiate(c, c, this); } catch(Exception e) { - logger.exception(e); + LOG.exception(e); reportError(new InternalParameterizationErrors("Error instantiating internal class: "+c.getName(), e)); return null; } diff --git a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameterization/ChainedParameterization.java b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameterization/ChainedParameterization.java index f62b8128..a2fd168e 100644 --- a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameterization/ChainedParameterization.java +++ b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameterization/ChainedParameterization.java @@ -23,7 +23,8 @@ package de.lmu.ifi.dbs.elki.utilities.optionhandling.parameterization; along with this program. If not, see <http://www.gnu.org/licenses/>. */ -import java.util.Vector; +import java.util.ArrayList; +import java.util.List; import de.lmu.ifi.dbs.elki.utilities.optionhandling.ParameterException; import de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.Parameter; @@ -43,7 +44,7 @@ public class ChainedParameterization extends AbstractParameterization { /** * Keep the list of parameterizations. */ - private Vector<Parameterization> chain = new Vector<Parameterization>(); + private List<Parameterization> chain = new ArrayList<Parameterization>(); /** * Error target @@ -73,7 +74,7 @@ public class ChainedParameterization extends AbstractParameterization { } @Override - public boolean setValueForOption(Parameter<?,?> opt) throws ParameterException { + public boolean setValueForOption(Parameter<?> opt) throws ParameterException { for(Parameterization p : chain) { if(p.setValueForOption(opt)) { return true; @@ -102,10 +103,9 @@ public class ChainedParameterization extends AbstractParameterization { this.errorTarget = config; } - /** {@inheritDoc} */ @Override public void reportError(ParameterException e) { - if (this.errorTarget == this) { + if (this.equals(this.errorTarget)) { super.reportError(e); } else { this.errorTarget.reportError(e); diff --git a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameterization/EmptyParameterization.java b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameterization/EmptyParameterization.java index 1fbd26b6..3391ff4a 100644 --- a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameterization/EmptyParameterization.java +++ b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameterization/EmptyParameterization.java @@ -23,7 +23,6 @@ package de.lmu.ifi.dbs.elki.utilities.optionhandling.parameterization; along with this program. If not, see <http://www.gnu.org/licenses/>. */ -import de.lmu.ifi.dbs.elki.utilities.optionhandling.ParameterException; import de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.Parameter; /** @@ -38,7 +37,7 @@ public class EmptyParameterization extends AbstractParameterization { } @Override - public boolean setValueForOption(Parameter<?,?> opt) throws ParameterException { + public boolean setValueForOption(Parameter<?> opt) { // Always return false, we don't have extra parameters, // This will cause {@link AbstractParameterization} to use the default values return false; diff --git a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameterization/ListParameterization.java b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameterization/ListParameterization.java index 937bb966..7d728280 100644 --- a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameterization/ListParameterization.java +++ b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameterization/ListParameterization.java @@ -90,7 +90,7 @@ public class ListParameterization extends AbstractParameterization { * @param flag Flag to add, if set */ public void forwardOption(Flag flag) { - if (flag.isDefined() && flag.getValue()) { + if (flag.isDefined() && flag.getValue().booleanValue()) { addFlag(flag.getOptionID()); } } @@ -100,14 +100,14 @@ public class ListParameterization extends AbstractParameterization { * * @param param Parameter to add */ - public void forwardOption(Parameter<?,?> param) { + public void forwardOption(Parameter<?> param) { if (param.isDefined()) { addParameter(param.getOptionID(), param.getValue()); } } @Override - public boolean setValueForOption(Parameter<?,?> opt) throws ParameterException { + public boolean setValueForOption(Parameter<?> opt) throws ParameterException { Iterator<Pair<OptionID, Object>> iter = parameters.iterator(); while(iter.hasNext()) { Pair<OptionID, Object> pair = iter.next(); @@ -144,10 +144,10 @@ public class ListParameterization extends AbstractParameterization { @Override public String toString() { - StringBuffer buf = new StringBuffer(); + StringBuilder buf = new StringBuilder(); for (Pair<OptionID, Object> pair : parameters) { - buf.append("-").append(pair.getFirst().toString()).append(" "); - buf.append(pair.getSecond().toString()).append(" "); + buf.append('-').append(pair.getFirst().toString()).append(' '); + buf.append(pair.getSecond().toString()).append(' '); } return buf.toString(); } diff --git a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameterization/MergedParameterization.java b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameterization/MergedParameterization.java index 8bba1c2a..580a3371 100644 --- a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameterization/MergedParameterization.java +++ b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameterization/MergedParameterization.java @@ -24,7 +24,8 @@ package de.lmu.ifi.dbs.elki.utilities.optionhandling.parameterization; */ import java.util.Collection; -import java.util.Vector; +import java.util.ArrayList; +import java.util.List; import de.lmu.ifi.dbs.elki.utilities.ClassGenericsUtil; import de.lmu.ifi.dbs.elki.utilities.optionhandling.InternalParameterizationErrors; @@ -57,7 +58,7 @@ public class MergedParameterization implements Parameterization { /** * Parameters to rewind. */ - final private java.util.Vector<Pair<OptionID, Object>> used; + final private List<Pair<OptionID, Object>> used; /** * Constructor. @@ -68,7 +69,7 @@ public class MergedParameterization implements Parameterization { super(); this.inner = child; this.current = new ListParameterization(); - this.used = new java.util.Vector<Pair<OptionID, Object>>(); + this.used = new ArrayList<Pair<OptionID, Object>>(); } /** @@ -78,7 +79,7 @@ public class MergedParameterization implements Parameterization { * @param current Current parameterization to re-used * @param used Used parameters list. */ - private MergedParameterization(Parameterization inner, ListParameterization current, Vector<Pair<OptionID, Object>> used) { + private MergedParameterization(Parameterization inner, ListParameterization current, List<Pair<OptionID, Object>> used) { super(); this.inner = inner; this.current = current; @@ -93,12 +94,12 @@ public class MergedParameterization implements Parameterization { for(Pair<OptionID, Object> pair : used) { current.addParameter(pair.first, pair.second); } - used.removeAllElements(); + used.clear(); } } @Override - public boolean setValueForOption(Parameter<?, ?> opt) throws ParameterException { + public boolean setValueForOption(Parameter<?> opt) throws ParameterException { try { if(current.setValueForOption(opt)) { used.add(new Pair<OptionID, Object>(opt.getOptionID(), opt.getValue())); @@ -139,7 +140,7 @@ public class MergedParameterization implements Parameterization { } @Override - public boolean grab(Parameter<?, ?> opt) { + public boolean grab(Parameter<?> opt) { try { if (setValueForOption(opt)) { return true; diff --git a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameterization/Parameterization.java b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameterization/Parameterization.java index f03463f8..5cdff013 100644 --- a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameterization/Parameterization.java +++ b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameterization/Parameterization.java @@ -58,7 +58,7 @@ public interface Parameterization { * @param opt Option to add * @return if the value is available (= readable) */ - public boolean grab(Parameter<?,?> opt); + public boolean grab(Parameter<?> opt); /** * Assign a value for an option, but not using default values and throwing @@ -68,7 +68,7 @@ public interface Parameterization { * @return Success code * @throws ParameterException on assignment errors. */ - public boolean setValueForOption(Parameter<?,?> opt) throws ParameterException; + public boolean setValueForOption(Parameter<?> opt) throws ParameterException; /** * Get the configuration errors thrown in {@link #grab} diff --git a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameterization/SerializedParameterization.java b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameterization/SerializedParameterization.java index 73abbca0..b88b7dc4 100644 --- a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameterization/SerializedParameterization.java +++ b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameterization/SerializedParameterization.java @@ -108,7 +108,7 @@ public class SerializedParameterization extends AbstractParameterization { } @Override - public boolean setValueForOption(Parameter<?,?> opt) throws ParameterException { + public boolean setValueForOption(Parameter<?> opt) throws ParameterException { Iterator<String> piter = parameters.iterator(); while(piter.hasNext()) { String cur = piter.next(); diff --git a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameterization/TrackParameters.java b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameterization/TrackParameters.java index 157efa32..424f54ee 100644 --- a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameterization/TrackParameters.java +++ b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameterization/TrackParameters.java @@ -23,11 +23,11 @@ package de.lmu.ifi.dbs.elki.utilities.optionhandling.parameterization; along with this program. If not, see <http://www.gnu.org/licenses/>. */ +import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.Vector; import de.lmu.ifi.dbs.elki.logging.LoggingUtil; import de.lmu.ifi.dbs.elki.utilities.ClassGenericsUtil; @@ -54,7 +54,7 @@ public class TrackParameters implements Parameterization { /** * Tracking storage */ - java.util.Vector<Pair<Object, Parameter<?, ?>>> options = new java.util.Vector<Pair<Object, Parameter<?, ?>>>(); + List<Pair<Object, Parameter<?>>> options = new ArrayList<Pair<Object, Parameter<?>>>(); /** * Tree information: parent links @@ -85,13 +85,13 @@ public class TrackParameters implements Parameterization { /** * Internal constructor, for nested tracking. * - * @param inner - * @param option - * @param options - * @param parents - * @param children + * @param inner Inner parameterization + * @param option Option + * @param options List of options + * @param parents Parent map + * @param children Child map */ - private TrackParameters(Parameterization inner, Object option, Vector<Pair<Object, Parameter<?, ?>>> options, Map<Object, Object> parents, Map<Object, List<Object>> children) { + private TrackParameters(Parameterization inner, Object option, List<Pair<Object, Parameter<?>>> options, Map<Object, Object> parents, Map<Object, List<Object>> children) { super(); this.inner = inner.descend(option); this.cur = option; @@ -111,9 +111,9 @@ public class TrackParameters implements Parameterization { } @Override - public boolean grab(Parameter<?, ?> opt) { + public boolean grab(Parameter<?> opt) { registerChild(opt); - options.add(new Pair<Object, Parameter<?, ?>>(cur, opt)); + options.add(new Pair<Object, Parameter<?>>(cur, opt)); return inner.grab(opt); } @@ -128,9 +128,9 @@ public class TrackParameters implements Parameterization { } @Override - public boolean setValueForOption(Parameter<?, ?> opt) throws ParameterException { + public boolean setValueForOption(Parameter<?> opt) throws ParameterException { registerChild(opt); - options.add(new Pair<Object, Parameter<?, ?>>(cur, opt)); + options.add(new Pair<Object, Parameter<?>>(cur, opt)); return inner.setValueForOption(opt); } @@ -139,7 +139,7 @@ public class TrackParameters implements Parameterization { * * @return Parameters seen */ - public Collection<Pair<Object, Parameter<?, ?>>> getAllParameters() { + public Collection<Pair<Object, Parameter<?>>> getAllParameters() { return options; } @@ -149,8 +149,8 @@ public class TrackParameters implements Parameterization { * @return Parameters given */ public Collection<Pair<OptionID, Object>> getGivenParameters() { - java.util.Vector<Pair<OptionID, Object>> ret = new java.util.Vector<Pair<OptionID, Object>>(); - for(Pair<Object, Parameter<?, ?>> pair : options) { + ArrayList<Pair<OptionID, Object>> ret = new ArrayList<Pair<OptionID, Object>>(); + for(Pair<Object, Parameter<?>> pair : options) { if(pair.second.isDefined() && pair.second.getGivenValue() != null) { ret.add(new Pair<OptionID, Object>(pair.second.getOptionID(), pair.second.getGivenValue())); } @@ -158,7 +158,6 @@ public class TrackParameters implements Parameterization { return ret; } - /** {@inheritDoc} */ @Override public boolean checkConstraint(GlobalParameterConstraint constraint) { return inner.checkConstraint(constraint); @@ -181,7 +180,7 @@ public class TrackParameters implements Parameterization { parents.put(opt, cur); List<Object> c = children.get(cur); if(c == null) { - c = new java.util.Vector<Object>(); + c = new ArrayList<Object>(); children.put(cur, c); } if(!c.contains(opt)) { diff --git a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameterization/UnParameterization.java b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameterization/UnParameterization.java index 3a25ddd4..a04b3217 100644 --- a/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameterization/UnParameterization.java +++ b/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameterization/UnParameterization.java @@ -23,7 +23,9 @@ package de.lmu.ifi.dbs.elki.utilities.optionhandling.parameterization; along with this program. If not, see <http://www.gnu.org/licenses/>. */ +import java.util.ArrayList; import java.util.Collection; +import java.util.List; import de.lmu.ifi.dbs.elki.utilities.ClassGenericsUtil; import de.lmu.ifi.dbs.elki.utilities.optionhandling.InternalParameterizationErrors; @@ -43,7 +45,7 @@ public class UnParameterization implements Parameterization { /** * Errors */ - java.util.Vector<ParameterException> errors = new java.util.Vector<ParameterException>(); + List<ParameterException> errors = new ArrayList<ParameterException>(); @Override public boolean hasUnusedParameters() { @@ -66,7 +68,7 @@ public class UnParameterization implements Parameterization { } @Override - public boolean grab(Parameter<?, ?> opt) { + public boolean grab(Parameter<?> opt) { return false; } @@ -76,7 +78,7 @@ public class UnParameterization implements Parameterization { } @Override - public boolean setValueForOption(Parameter<?, ?> opt) throws ParameterException { + public boolean setValueForOption(Parameter<?> opt) { return false; } 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; |