Class AbstractParameter<P extends AbstractParameter<P,T>,T>
- java.lang.Object
-
- elki.utilities.optionhandling.parameters.AbstractParameter<P,T>
-
- Type Parameters:
P
- type self-referenceT
- the type of a possible value (i.e., the type of the option)
- All Implemented Interfaces:
Parameter<T>
- Direct Known Subclasses:
ClassParameter
,EnumParameter
,FileParameter
,Flag
,IntGeneratorParameter
,ListParameter
,NumberParameter
,PatternParameter
,RandomParameter
,StringParameter
public abstract class AbstractParameter<P extends AbstractParameter<P,T>,T> extends java.lang.Object implements Parameter<T>
Abstract class for specifying a parameter.A parameter is defined as an option having a specific value.
See the
elki.utilities.optionhandling
package for documentation!- Since:
- 0.1
- Author:
- Steffi Wanka, Erich Schubert
-
-
Field Summary
Fields Modifier and Type Field Description protected java.util.List<ParameterConstraint<? super T>>
constraints
Holds parameter constraints for this parameter.protected T
defaultValue
The default value of the parameter (may be null).private boolean
defaultValueTaken
Whether the default value of this parameter was used.protected T
givenValue
The value last passed to this option.protected boolean
optionalParameter
Specifies if this parameter is an optional parameter.protected OptionID
optionid
The option name.private T
value
The value of this option.
-
Constructor Summary
Constructors Constructor Description AbstractParameter(OptionID optionID)
Constructs a parameter with the given optionID, and constraints.AbstractParameter(OptionID optionID, T defaultValue)
Constructs a parameter with the given optionID, constraints, and default value.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description P
addConstraint(ParameterConstraint<? super T> constraint)
Add an additional constraint.java.lang.StringBuilder
describeValues(java.lang.StringBuilder description)
Describe the valid values.java.util.List<ParameterConstraint<? super T>>
getConstraints()
Get the parameter constraints.T
getDefaultValue()
Returns the default value of the parameter.java.lang.String
getDefaultValueAsString()
Get the default value as string.OptionID
getOptionID()
Return the OptionID of this option.T
getValue()
Returns the value of the option.boolean
hasDefaultValue()
Checks if this parameter has a default value.boolean
isDefined()
Returns true if the value of the option is defined, false otherwise.boolean
isOptional()
Checks if this parameter is an optional parameter.boolean
isValid(java.lang.Object obj)
Checks if the given argument is valid for this option.protected abstract T
parseValue(java.lang.Object obj)
Parse a given value into the destination type.P
setDefaultValue(T defaultValue)
Sets the default value of this parameter.P
setOptional(boolean opt)
Specifies if this parameter is an optional parameter.void
setValue(java.lang.Object obj)
Sets the value of the option.protected void
setValueInternal(T val)
Internal setter for the value.boolean
tookDefaultValue()
Checks if the default value of this parameter was taken as the actual parameter value.boolean
tryDefaultValue()
Handle default values for a parameter.protected boolean
validate(T obj)
Validate a value after parsing (e.g., do constraint checks!)-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface elki.utilities.optionhandling.parameters.Parameter
getSyntax, getValueAsString
-
-
-
-
Field Detail
-
optionid
protected final OptionID optionid
The option name.
-
defaultValue
protected T defaultValue
The default value of the parameter (may be null).
-
defaultValueTaken
private boolean defaultValueTaken
Whether the default value of this parameter was used.
-
optionalParameter
protected boolean optionalParameter
Specifies if this parameter is an optional parameter.
-
constraints
protected java.util.List<ParameterConstraint<? super T>> constraints
Holds parameter constraints for this parameter.
-
givenValue
protected T givenValue
The value last passed to this option.
-
value
private T value
The value of this option.
-
-
Constructor Detail
-
AbstractParameter
public AbstractParameter(OptionID optionID, T defaultValue)
Constructs a parameter with the given optionID, constraints, and default value.- Parameters:
optionID
- the unique id of this parameterdefaultValue
- the default value of this parameter (may be null)
-
AbstractParameter
public AbstractParameter(OptionID optionID)
Constructs a parameter with the given optionID, and constraints.- Parameters:
optionID
- the unique id of this parameter
-
-
Method Detail
-
setDefaultValue
public P setDefaultValue(T defaultValue)
Description copied from interface:Parameter
Sets the default value of this parameter.- Specified by:
setDefaultValue
in interfaceParameter<P extends AbstractParameter<P,T>>
- Parameters:
defaultValue
- default value of this parameter- Returns:
- the parameter itself, for chaining
-
hasDefaultValue
public boolean hasDefaultValue()
Description copied from interface:Parameter
Checks if this parameter has a default value.- Specified by:
hasDefaultValue
in interfaceParameter<P extends AbstractParameter<P,T>>
- Returns:
- true, if this parameter has a default value, false otherwise
-
tryDefaultValue
public boolean tryDefaultValue() throws UnspecifiedParameterException
Description copied from interface:Parameter
Handle default values for a parameter.- Specified by:
tryDefaultValue
in interfaceParameter<P extends AbstractParameter<P,T>>
- Returns:
- Return code:
true
if it has a default value,false
if it is optional without a default value. Exception if it is a required parameter! - Throws:
UnspecifiedParameterException
- If the parameter requires a value
-
setOptional
public P setOptional(boolean opt)
Description copied from interface:Parameter
Specifies if this parameter is an optional parameter.- Specified by:
setOptional
in interfaceParameter<P extends AbstractParameter<P,T>>
- Parameters:
opt
- true if this parameter is optional, false otherwise- Returns:
- the parameter itself, for chaining
-
isOptional
public boolean isOptional()
Description copied from interface:Parameter
Checks if this parameter is an optional parameter.- Specified by:
isOptional
in interfaceParameter<P extends AbstractParameter<P,T>>
- Returns:
- true if this parameter is optional, false otherwise
-
tookDefaultValue
public boolean tookDefaultValue()
Description copied from interface:Parameter
Checks if the default value of this parameter was taken as the actual parameter value.- Specified by:
tookDefaultValue
in interfaceParameter<P extends AbstractParameter<P,T>>
- Returns:
- true, if the default value was taken as actual parameter value, false otherwise
-
isDefined
public boolean isDefined()
Description copied from interface:Parameter
Returns true if the value of the option is defined, false otherwise.
-
getDefaultValue
public T getDefaultValue()
Description copied from interface:Parameter
Returns the default value of the parameter.If the parameter has no default value, the method returns null.
- Specified by:
getDefaultValue
in interfaceParameter<P extends AbstractParameter<P,T>>
- Returns:
- the default value of the parameter, null if the parameter has no default value.
-
describeValues
public java.lang.StringBuilder describeValues(java.lang.StringBuilder description)
Description copied from interface:Parameter
Describe the valid values.- Specified by:
describeValues
in interfaceParameter<P extends AbstractParameter<P,T>>
- Parameters:
description
- Buffer to append to- Returns:
- Buffer
-
validate
protected boolean validate(T obj) throws ParameterException
Validate a value after parsing (e.g., do constraint checks!)- Parameters:
obj
- Object to validate- Returns:
- true iff the object is valid for this parameter.
- Throws:
ParameterException
- when the object is not valid.
-
getOptionID
public OptionID getOptionID()
Description copied from interface:Parameter
Return the OptionID of this option.- Specified by:
getOptionID
in interfaceParameter<P extends AbstractParameter<P,T>>
- Returns:
- Option ID
-
setValue
public void setValue(java.lang.Object obj) throws ParameterException
Description copied from interface:Parameter
Sets the value of the option.- Specified by:
setValue
in interfaceParameter<P extends AbstractParameter<P,T>>
- Parameters:
obj
- the option's value to be set- Throws:
ParameterException
- if the given value is not a valid value for this option.
-
setValueInternal
protected void setValueInternal(T val)
Internal setter for the value.- Parameters:
val
- Value
-
getValue
public final T getValue()
Description copied from interface:Parameter
Returns the value of the option.You should use either
Parameterization.grab(elki.utilities.optionhandling.parameters.Parameter<?>)
orParameter.isDefined()
to test if getValue() will return a well-defined value.
-
isValid
public final boolean isValid(java.lang.Object obj) throws ParameterException
Description copied from interface:Parameter
Checks if the given argument is valid for this option.- Specified by:
isValid
in interfaceParameter<P extends AbstractParameter<P,T>>
- Parameters:
obj
- option value to be checked- Returns:
- true, if the given value is valid for this option
- Throws:
ParameterException
- if the given value is not a valid value for this option.
-
parseValue
protected abstract T parseValue(java.lang.Object obj) throws ParameterException
Parse a given value into the destination type.- Parameters:
obj
- Object to parse (may be a string representation!)- Returns:
- Parsed object
- Throws:
ParameterException
- when the object cannot be parsed.
-
getDefaultValueAsString
public java.lang.String getDefaultValueAsString()
Description copied from interface:Parameter
Get the default value as string.- Specified by:
getDefaultValueAsString
in interfaceParameter<P extends AbstractParameter<P,T>>
- Returns:
- default value
-
addConstraint
public P addConstraint(ParameterConstraint<? super T> constraint)
Description copied from interface:Parameter
Add an additional constraint.- Specified by:
addConstraint
in interfaceParameter<P extends AbstractParameter<P,T>>
- Parameters:
constraint
- Constraint to add.- Returns:
- the parameter itself, for chaining
-
getConstraints
public java.util.List<ParameterConstraint<? super T>> getConstraints()
Description copied from interface:Parameter
Get the parameter constraints.- Specified by:
getConstraints
in interfaceParameter<P extends AbstractParameter<P,T>>
- Returns:
- Parameter constraints
-
-