public abstract class Parameter<T> extends Object
This class must be subclassed according to the type of values it obtains.
Such concrete subclasses must implement the stringToObject(uk.ac.starlink.task.Environment, java.lang.String)
method to perform mapping from an environment-supplied string value
to a typed parameter value, including validation.
Task
,
Environment
Modifier and Type | Field and Description |
---|---|
static Comparator<Parameter<?>> |
BY_NAME
Compares parameters alphabetically by parameter name.
|
Constructor and Description |
---|
Parameter(String name,
Class<T> clazz,
boolean allowClassnameValue)
Constructs a parameter with a given name.
|
Modifier and Type | Method and Description |
---|---|
void |
clearValue(Environment env)
Clears the value of this parameter.
|
String |
getDescription()
Returns the textual description for this parameter.
|
String |
getName()
Returns the name of this parameter.
|
int |
getPosition()
Gets the position of this parameter in a parameter list;
the first parameter is 1.
|
boolean |
getPreferExplicit()
Determine whether an explict value is generally preferred to the
default value for this parameter.
|
String |
getPrompt()
Gets the prompt string for this parameter, as displayed to the
user when the value of the parameter is requested.
|
String |
getStringDefault()
Gets the default string value for this parameter.
|
String |
getUsage()
Returns the usage string for this parameter.
|
Class<T> |
getValueClass()
Returns the class of the typed values this parameter takes.
|
boolean |
isNullPermitted()
Determine whether it is legal for this parameter's value to be blank.
|
String |
objectToString(Environment env,
T objectVal)
Takes a typed value of this parameter and formats it as a string
which may be used for presentation to the user.
|
T |
objectValue(Environment env)
Gets the value of this parameter as a typed object.
|
void |
setDescription(String description)
Sets the textual description for this parameter.
|
void |
setDescription(String[] descLines)
Convenience method to set the description for this parameter by
the result of joining an array of lines together.
|
void |
setName(String name)
Sets the name of this parameter.
|
void |
setNullPermitted(boolean permitted)
Set whether it is legal for this parameter's value to be blank.
|
void |
setPosition(int pos)
Sets the position of this parameter in a parameter list;
the first parameter is 1.
|
void |
setPreferExplicit(boolean prefer)
Set whether an explicit value is generally to be solicited from
the user rather than taking the default.
|
void |
setPrompt(String prompt)
Sets the prompt string for this parameter, as displayed to the
user when the value of the parameter is requested.
|
void |
setStringDefault(String stringDflt)
Sets the default string value for this parameter.
|
void |
setUsage(String usage)
Sets a usage string for this parameter.
|
protected void |
setValue(String stringValue,
T objectValue)
Sets the value of this parameter without any additional validation.
|
void |
setValueFromObject(Environment env,
T objectValue)
Sets the value of this parameter directly from a typed object.
|
void |
setValueFromString(Environment env,
String stringval)
Sets the value of this parameter from a String.
|
abstract T |
stringToObject(Environment env,
String stringVal)
Takes a non-blank string, as supplied by the execution environment,
and turns it into a typed value for this parameter.
|
String |
stringValue(Environment env)
Gets the value of this parameter as a String.
|
T[] |
toArray(Collection<T> collection)
Utility function to convert a list to an array, where the elements
are of the value class of this parameter.
|
String |
toString()
Returns the name of this parameter.
|
public static final Comparator<Parameter<?>> BY_NAME
public Parameter(String name, Class<T> clazz, boolean allowClassnameValue)
If the allowClassnameValue
flag is set,
then in addition to the options provided by
stringToObject
, any supplied string value
equal to the classname of an available class matching
this parameter's value type which has a no-arg constructor
will cause a newly constructed instance of that class to be
used as a value.
name
- the identifying name of this parameterclazz
- the class of this parameter's typed valuesallowClassnameValue
- whether suitable classnames may be
supplied as string valuespublic abstract T stringToObject(Environment env, String stringVal) throws TaskException
It is an error to supply a null or empty string value.
If this method fails (throws a ParameterValueException)
and if allowClassnameValue
is set, then a subsequent
attempt will be made to interpret the stringVal
as the classname of a suitable class with a no-arg constructor.
env
- execution environment; in most cases this is not required
but for some purposes environment-specific characteristics
may influence the resultstringVal
- non-null, non-empty string valueTaskException
public String objectToString(Environment env, T objectVal) throws TaskException
stringToObject
should be possible,
but that is not in general required/guaranteed.
The default implementation uses the value's toString method, but subclasses can override this for smarter behaviour.
env
- execution environmentobjectVal
- typed parameter valueTaskException
public void setValueFromString(Environment env, String stringval) throws TaskException
A null or empty string is intercepted by this method and translated to a null object value or triggers a ParameterValueException according to the value of isNullPermitted().
env
- execution environment; in most cases this is not required
but for some purposes environment-specific characteristics
may influence the resultstringval
- string representation of valueTaskException
public void setValueFromObject(Environment env, T objectValue) throws TaskException
objectToString
.env
- execution environment; in most cases this is not required
but for some purposes environment-specific characteristics
may influence the resultobjectValue
- typed valueTaskException
public final String stringValue(Environment env) throws TaskException
The returned value may be null
only if the isNullPermitted()
method returns true.
env
- execution environment from which value is obtainednull
AbortException
- if during the course of trying to obtain
a value the Environment determines that the task should
not continue.TaskException
public final T objectValue(Environment env) throws TaskException
The returned value will generally be null if the string value
that generated it is null or empty. That is only possible
if the isNullPermitted()
method returns true.
env
- execution environment from which value is obtainednull
AbortException
- if during the course of trying to obtain
a value the Environment determines that the task should
not continue.TaskException
public void clearValue(Environment env)
env
- execution environment within which value will be clearedprotected void setValue(String stringValue, T objectValue)
stringValue
- string representation of valueobjectValue
- typed valuepublic Class<T> getValueClass()
public String getName()
public void setName(String name)
name
- identifying name of this parameterpublic String getPrompt()
public void setPrompt(String prompt)
prompt
- the prompt stringpublic String getDescription()
public void setDescription(String description)
description
- descriptionpublic void setDescription(String[] descLines)
descLines
- lines of textual descriptionsetDescription(java.lang.String)
public void setUsage(String usage)
Parameter
class uses the string "<value>"
as the default usage string.usage
- usage stringpublic String getUsage()
setUsage(java.lang.String)
public void setNullPermitted(boolean permitted)
permitted
- whether null values are to be permitted for this
parameterpublic boolean isNullPermitted()
public boolean getPreferExplicit()
public void setPreferExplicit(boolean prefer)
prefer
- true if you would like to encourage an explicit
value to be set for this parameterpublic String getStringDefault()
public void setStringDefault(String stringDflt)
stringDflt
- the default string valuepublic int getPosition()
public void setPosition(int pos)
pos
- parameter positionpublic T[] toArray(Collection<T> collection)
collection
- typed collectionCopyright © 2024 Central Laboratory of the Research Councils. All Rights Reserved.