public abstract class AbstractParameter<T> extends Object implements Parameter<T>
To implement a concrete Parameter class by subclassing this class, the following should be done:
convertValue(String)
method.validateValue(Object)
is not adequate (see the javadoc
for this method), override it, perhaps calling super.validateValue() to also
invoke the functionality of this class.setOptionLabel()
from the
constructors to set a reasonable option label for the new type of parameter.
A simple Parameter class that accepts only strings of a specified length might look as follows:
public class FixedLenParam extends AbstractParameter<String> { private int length; public FixedLenParam(String tag, String desc, int length) { setTag(tag); setDesc(desc); this.length = length; setOptionLabel("<s>"); } public String convertValue(String strVal) { return strVal; } public void validateValue(String val) throws CmdLineException { super.validateValue(val); // check acceptable values, etc.. if (val.length() != length) { throw new CmdLineException(getTag() + " must be a string of " + length + " characters in length"); } } }
Modifier and Type | Field and Description |
---|---|
protected List<T> |
acceptableValues
a set of restricted values the Parameter may take
|
protected String |
desc
a description of the parameter to be displayed in the usage
|
protected boolean |
hidden
indicates that the parameter is hidden and will not be displayed in the
normal usage - default is
false |
protected boolean |
ignoreRequired
During parse, ignore missing required Parameters if this Parameter is
set.
|
protected boolean |
multiValued
Indicates whether the parameter can have multiple values.
|
protected boolean |
optional
Indicates whether or not the parameter is optional.
|
protected String |
optionLabel
The label that should be used for a Parameter option's value in the usage
|
protected boolean |
set
indicates that the value of the parameter has been set
|
protected String |
tag
the tag which uniquely identifies the parameter, and will be used to
identify the parameter on the command line if the parameter is used as an
option
|
protected ArrayList<T> |
values
the value(s) of the entity
|
HIDDEN, MULTI_VALUED, OPTIONAL, PUBLIC, REQUIRED, SINGLE_VALUED
Constructor and Description |
---|
AbstractParameter() |
Modifier and Type | Method and Description |
---|---|
void |
addStringValue(String value)
Add a value to this Parameter.
|
void |
addValue(T value)
Adds the specified Object as a value for this entity - the Object will be
validated with respect to the constraints of the Parameter.
|
abstract T |
convertValue(String strVal)
Converts a String value to the type associated with the Parameter.
|
List<T> |
getAcceptableValues()
Gets the values that are acceptable for this parameter, if a restricted
set exists.
|
String |
getDesc()
gets the value of the parameter's description
|
boolean |
getIgnoreRequired()
Gets the flag indicating that during parse, missing required Parameters
are ignored if this Parameter is set.
|
String |
getOptionLabel()
gets the value of optionLabel
|
String |
getTag()
gets the value of tag
|
T |
getValue()
The value of the parameter, in the case where the parameter is not
multi-valued.
|
List<T> |
getValues()
gets the values associated with this Parameter - the List will be in the
order the values were listed on the command line.
|
boolean |
isHidden()
gets the value of the hidden indicator
|
boolean |
isMultiValued()
gets the value of multiValued indicator
|
boolean |
isOptional()
returns the value of the optional indicator
|
boolean |
isSet()
gets an indicator that the parameter's value has been set
|
void |
setAcceptableValues(Collection<T> vals)
Sets the values that are acceptable for this parameter, if a restricted
set exists.
|
void |
setAcceptableValues(T[] vals)
Sets the values that are acceptable for this parameter, if a restricted
set exists.
|
void |
setDesc(String desc)
sets the value of this parameter's description
|
void |
setHidden(boolean hidden)
sets the value of the hidden indicator
|
void |
setIgnoreRequired(boolean ignoreRequired)
Sets a flag such that during parse, missing required Parameters are
ignored if this Parameter is set.
|
void |
setMultiValued(boolean multiValued)
sets the value of the multiValued indicator
|
void |
setOptional(boolean optional)
indicates whether or not the parameter is optional
|
void |
setOptionLabel(String optionLabel)
Sets the value of optionLabel.
|
void |
setTag(String tag)
sets the value of tag
|
void |
setValue(T value)
Sets the value of the parameter to the specified string.
|
void |
setValues(List<T> values)
Sets the values of the parameter to those specified.
|
void |
setValues(T[] values)
Sets the values of the parameter to those specified.
|
void |
validateValue(T value)
This implementation compares the value to the acceptable values if any
have been defined.
|
protected List<T> acceptableValues
setAcceptableValues()
,
getAcceptableValues()
protected String desc
protected boolean hidden
false
protected boolean ignoreRequired
setIgnoreRequired()
,
getIgnoreRequired()
protected boolean multiValued
protected boolean optional
true
, indicating that the parameter is optional.protected String optionLabel
setOptionLabel()
,
getOptionLabel()
protected boolean set
protected String tag
public void addStringValue(String value) throws CmdLineException
convertValue(String)
to convert the String, then
addValue(Object)
.addStringValue
in interface Parameter<T>
value
- the value to be addedCmdLineException
- if the value of the entity has already been set and
multiValued
is not true
, or if the
value is not valid.Parameter.addStringValue(java.lang.String)
public abstract T convertValue(String strVal) throws CmdLineException
strVal
- the String value of the ParameterCmdLineException
- if the conversion cannot be madepublic void addValue(T value) throws CmdLineException
Parameter
addValue
in interface Parameter<T>
value
- the value to be addedCmdLineException
- if the value of the entity has already been set and
multiValued
is not true
, or if the
validation provided by the implementing class fails.Parameter.addValue(Object)
public List<T> getAcceptableValues()
Parameter
getAcceptableValues
in interface Parameter<T>
Parameter.getAcceptableValues()
public String getDesc()
Parameter
getDesc
in interface Parameter<T>
Parameter.getDesc()
public boolean getIgnoreRequired()
Parameter
getIgnoreRequired
in interface Parameter<T>
true
if missing required Parameters will be ignored
when this Parameter is set.Parameter.getIgnoreRequired()
public String getOptionLabel()
Parameter
getOptionLabel
in interface Parameter<T>
Parameter.getOptionLabel()
public String getTag()
Parameter
getTag
in interface Parameter<T>
Parameter.getTag()
public T getValue()
Parameter
getValue
in interface Parameter<T>
Parameter.getValue()
public List<T> getValues()
Parameter
getValues
in interface Parameter<T>
Parameter.getValues()
public boolean isHidden()
Parameter
isHidden
in interface Parameter<T>
Parameter.HIDDEN
) if the parameter is a hidden parameterParameter.isHidden()
public boolean isMultiValued()
Parameter
isMultiValued
in interface Parameter<T>
Parameter.isMultiValued()
public boolean isOptional()
Parameter
isOptional
in interface Parameter<T>
Parameter.isOptional()
public boolean isSet()
Parameter
isSet
in interface Parameter<T>
Parameter.isSet()
public void setAcceptableValues(Collection<T> vals)
Parameter
vals
value, or an empty vals
Collection, will result in any previously set acceptable values being
cleared.
The toString()
values of the Objects in vals
will be used for the acceptable values.
setAcceptableValues
in interface Parameter<T>
vals
- the new acceptable valuesParameter.setAcceptableValues(java.util.Collection)
public void setAcceptableValues(T[] vals)
Parameter
vals
value, or an empty vals
array, will result in any previously set acceptable values being cleared.setAcceptableValues
in interface Parameter<T>
vals
- the new acceptable valuesParameter.setAcceptableValues(Collection)
public void setDesc(String desc) throws IllegalArgumentException
Parameter
setDesc
in interface Parameter<T>
desc
- a description of the parameter, suitable for display in the
command's usageIllegalArgumentException
- if desc
is fewer than 5 charaters.Parameter.setDesc(java.lang.String)
public void setHidden(boolean hidden)
Parameter
setHidden
in interface Parameter<T>
hidden
- true (Parameter.HIDDEN
) if the parameter is a hidden parameterParameter.setHidden(boolean)
public void setIgnoreRequired(boolean ignoreRequired)
Parameter
setIgnoreRequired
in interface Parameter<T>
ignoreRequired
- set to true
to ignore missing required Parameters
if this Parameter is setParameter.setIgnoreRequired(boolean)
public void setMultiValued(boolean multiValued)
Parameter
setMultiValued
in interface Parameter<T>
multiValued
- true if the parameter can have multiple valuesParameter.setMultiValued(boolean)
public void setOptional(boolean optional)
Parameter
setOptional
in interface Parameter<T>
optional
- true if the parameter is optionalParameter.setOptional(boolean)
public void setOptionLabel(String optionLabel)
Parameter
st_date <mm/dd/yy> the start date of the reportThe default is the empty string.
setOptionLabel
in interface Parameter<T>
optionLabel
- The string used as a label for the parameter's value. If null,
an empty string is used.Parameter.setOptionLabel(java.lang.String)
public void setTag(String tag) throws IllegalArgumentException
Parameter
setTag
in interface Parameter<T>
tag
- a unique identifier for this parameter. If the parameter is
used as an option, it will be used to identify the option on
the command line. In the case where the parameter is used as
an argument, it will only be used to identify the argument in
the usage statement. Tags must be made up of any character but
'='.IllegalArgumentException
- if the length of tag
is less than 1, or
tag
contains an invalid character.Parameter.setTag(java.lang.String)
public void setValue(T value) throws CmdLineException
Parameter
setValue
in interface Parameter<T>
value
- the new value of the parameterCmdLineException
- if the validation provided by the implementing class fails.Parameter.setValue(java.lang.Object)
public void setValues(List<T> values) throws CmdLineException
Parameter
setValues
in interface Parameter<T>
values
- A List of objects to be used as the parameter's values.CmdLineException
- if more than one value is specified and
multiValued
is not true
, or if the
validation provided by the implementing class fails.Parameter.setValues(List)
public void setValues(T[] values) throws CmdLineException
Parameter
setValues
in interface Parameter<T>
values
- The objects to be used as the parameter's values.CmdLineException
- if more than one value is specified and
multiValued
is not true
, or if the
validation provided by the implementing class fails.Parameter.setValues(Object[])
public void validateValue(T value) throws CmdLineException
value
- the value to be validatedCmdLineException
- if there are acceptable values defined and the value is not
one of them.Copyright © 2017. All rights reserved.