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 type
  `T` - 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

## Field Summary

### Fields inherited from class uk.ac.starlink.task.Parameter

`BY_NAME`

## Constructor Summary

Constructors
Modifier
Constructor
Description
`protected`
`AbstractChoiceParameter(String name, Class<T> paramClazz, Class<C> optClazz, C[] options)`
Constructor.

## Method Summary

Modifier and Type
Method
Description
`void`
`addOption(C option)`
Adds an option value to this parameter.
`void`
`addOption(C option, String name)`
Adds an option value to this parameter with a given name.
`protected String`
`choiceToString(C objVal)`
Provides a string representation of a given typed value for this parameter.
`void`
`clearOptions()`
Clears the list of known options.
`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(String name)`
Returns the option value associated with a given string by this parameter.
`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.
`Collection<C>`
`getOptionValueList()`
Returns a collection of the option objects which may form the values of this parameter.
`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(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`

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

## Constructor Details

### AbstractChoiceParameter

protected AbstractChoiceParameter(String name, Class<T> paramClazz, Class<C> optClazz, C[] options)
Constructor.

Parameters:
  `name` - parameter name
  `paramClazz` - class of result type for this parameter
  `optClazz` - class of typed option
  `options` - initial list of typed options available

## Method Details

### addOption

public void addOption(C option, 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 object
  `name` - 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's `stringifyOption(C)` method.

Parameters:
  `option` - option object

### clearOptions

public void clearOptions()
Clears the list of known options.

### getOptionNames

public 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 Collection<C> getOptionValueList()
Returns a collection of the option objects which may form the values of this parameter.

Returns:
  permitted options

### getName

public 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 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 is `String.valueOf(option)`, but this may be overrridden.

Parameters:
  `option` - option value
Returns:
  string representation of option

### getOption

public C getOption(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(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 String choiceToString(C objVal)
Provides a string representation of a given typed value for this parameter.

Parameters:
  `objVal` - typed option
Returns:
  string representation