Package uk.ac.starlink.task
Class AbstractChoiceParameter<T,C>
- java.lang.Object
-
- uk.ac.starlink.task.Parameter<T>
-
- uk.ac.starlink.task.AbstractChoiceParameter<T,C>
-
- Type Parameters:
C
- option typeT
- parameter result type
- Direct Known Subclasses:
ChoiceParameter
,MultiChoiceParameter
public abstract class AbstractChoiceParameter<T,C> extends Parameter<T>
Abstract superclass for parameters that permit choices from a given list of typed options.- Since:
- 17 Dec 2021
- Author:
- Mark Taylor
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
AbstractChoiceParameter(java.lang.String name, java.lang.Class<T> paramClazz, java.lang.Class<C> optClazz, C[] options)
Constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addOption(C option)
Adds an option value to this parameter.void
addOption(C option, java.lang.String name)
Adds an option value to this parameter with a given name.protected java.lang.String
choiceToString(C objVal)
Provides a string representation of a given typed value for this parameter.void
clearOptions()
Clears the list of known options.java.lang.String
getName(C option)
Converts an option value object to a string which is used to identify it as a string value of this parameter.C
getOption(java.lang.String name)
Returns the option value associated with a given string by this parameter.java.lang.String[]
getOptionNames()
Returns an array of the string values of options accepted by this parameter.C[]
getOptions()
Returns an array of the option objects which may form the values of this parameter.java.util.Collection<C>
getOptionValueList()
Returns a collection of the option objects which may form the values of this parameter.java.lang.String
stringifyOption(C option)
Determines how an option will be represented as a string value of this parameter if no name has explicitly been supplied.protected C
stringToChoice(java.lang.String sval)
Converts a supplied string to an option value for this parameter, or throws a ParameterValueException if it is not suitable.-
Methods inherited from class uk.ac.starlink.task.Parameter
clearValue, getDescription, getName, getPosition, getPreferExplicit, getPrompt, getStringDefault, getUsage, getValueClass, isNullPermitted, objectToString, objectValue, setDescription, setDescription, setName, setNullPermitted, setPosition, setPreferExplicit, setPrompt, setStringDefault, setUsage, setValue, setValueFromObject, setValueFromString, stringToObject, stringValue, toArray, toString
-
-
-
-
Constructor Detail
-
AbstractChoiceParameter
protected AbstractChoiceParameter(java.lang.String name, java.lang.Class<T> paramClazz, java.lang.Class<C> optClazz, C[] options)
Constructor.- Parameters:
name
- parameter nameparamClazz
- class of result type for this parameteroptClazz
- class of typed optionoptions
- initial list of typed options available
-
-
Method Detail
-
addOption
public void addOption(C option, java.lang.String name)
Adds an option value to this parameter with a given name. The name is the parameter value string used to identify this option.- Parameters:
option
- option objectname
- label for option
-
addOption
public void addOption(C option)
Adds an option value to this parameter. The option's name will be determined by this object'sstringifyOption(C)
method.- Parameters:
option
- option object
-
clearOptions
public void clearOptions()
Clears the list of known options.
-
getOptionNames
public java.lang.String[] getOptionNames()
Returns an array of the string values of options accepted by this parameter.- Returns:
- permitted options, stringified
-
getOptions
public C[] getOptions()
Returns an array of the option objects which may form the values of this parameter.- Returns:
- permitted options
-
getOptionValueList
public java.util.Collection<C> getOptionValueList()
Returns a collection of the option objects which may form the values of this parameter.- Returns:
- permitted options
-
getName
public java.lang.String getName(C option)
Converts an option value object to a string which is used to identify it as a string value of this parameter.- Parameters:
option
- option value- Returns:
- string representation
-
stringifyOption
public java.lang.String stringifyOption(C option)
Determines how an option will be represented as a string value of this parameter if no name has explicitly been supplied. The default implementation isString.valueOf(option)
, but this may be overrridden.- Parameters:
option
- option value- Returns:
- string representation of option
-
getOption
public C getOption(java.lang.String name)
Returns the option value associated with a given string by this parameter. Null is returned if none of the options added so far has a name as supplied. Name matching is case-insensitive.- Parameters:
name
- name of option which has been added- Returns:
- corresponding option object
-
stringToChoice
protected C stringToChoice(java.lang.String sval) throws ParameterValueException
Converts a supplied string to an option value for this parameter, or throws a ParameterValueException if it is not suitable.- Parameters:
sval
- option name- Returns:
- option value, not null
- Throws:
ParameterValueException
- if no such option
-
choiceToString
protected java.lang.String choiceToString(C objVal)
Provides a string representation of a given typed value for this parameter.- Parameters:
objVal
- typed option- Returns:
- string representation
-
-