public final class OptionType<T> extends Object
Constructor and Description |
---|
OptionType(String name,
Function<String,T> stringConverter)
Constructs a new option type with name and function that allows to convert a string to the
option type.
|
OptionType(String name,
Function<String,T> stringConverter,
Consumer<T> validator)
Constructs a new option type with name and function that allows to convert a string to the
option type and validator of the option values.
|
Modifier and Type | Method and Description |
---|---|
T |
convert(Object previousValue,
String nameSuffix,
String value)
Converts a string value, validates it, and converts it to an object of this type.
|
T |
convert(String value)
Converts a string value, validates it, and converts it to an object of this type.
|
static <T> OptionType<T> |
defaultType(Class<T> clazz)
Returns the default option type for a class.
|
static <T> OptionType<T> |
defaultType(T value)
Returns the default option type for a given value.
|
String |
getName()
Returns the name of this type.
|
String |
toString() |
void |
validate(T value)
Validates an option value and throws an
IllegalArgumentException if the value is
invalid. |
public OptionType(String name, Function<String,T> stringConverter, Consumer<T> validator)
name
- the name of the type.stringConverter
- a function that converts a string value to the option value. Can throw
IllegalArgumentException
to indicate an invalid string.validator
- used for validating the option value. Throws
IllegalArgumentException
if the value is invalid.public OptionType(String name, Function<String,T> stringConverter)
name
- the name of the type.stringConverter
- a function that converts a string value to the option value. Can throw
IllegalArgumentException
to indicate an invalid string.public String getName()
public T convert(String value)
IllegalArgumentException
- if the value is invalid or cannot be converted.public T convert(Object previousValue, String nameSuffix, String value)
nameSuffix
- the key for prefix options.previousValue
- the previous value holded by option.IllegalArgumentException
- if the value is invalid or cannot be converted.public void validate(T value)
IllegalArgumentException
if the value is
invalid.IllegalArgumentException
- if the value is invalid or cannot be converted.public static <T> OptionType<T> defaultType(T value)
null
if no default
option type is available for the Java type of this value.public static <T> OptionType<T> defaultType(Class<T> clazz)
null
if no default option
type is available for this Java type.