Package org.apache.commons.cli
Class TypeHandler
java.lang.Object
org.apache.commons.cli.TypeHandler
TypeHandler will handle the pluggable conversion and verification of Option types. It handles the mapping of classes to bot converters and verifiers. It
provides the default conversion and verification methods when converters and verifiers are not explicitly set.
If Options are serialized and deserialized their converters and verifiers will revert to the defaults defined in this class. To correctly de-serialize Options with custom converters and/or verifiers, using the default serialization methods, this class should be properly configured with the custom converters and verifiers for the specific class.
-
Constructor Summary
ConstructorDescriptionConstructs a default initialized instance.TypeHandler
(Map<Class<?>, Converter<?, ? extends Throwable>> converterMap) Constructs a default initialized instance. -
Method Summary
Modifier and TypeMethodDescriptionstatic Class<?>
createClass
(String className) Returns the class whose name isclassName
.static Date
createDate
(String string) Returns the date represented bystring
.Creates a default converter map.static File
createFile
(String string) Returns the File represented bystring
.static File[]
createFiles
(String string) Deprecated.with no replacementstatic Number
createNumber
(String string) Deprecated.static Object
createObject
(String className) Deprecated.static URL
Creates the URL represented bystring
.static <T> T
createValue
(String string, Class<T> clazz) Creates the @code Object} of typeclazz
with the value ofstring
.static Object
createValue
(String string, Object obj) Deprecated.<T> Converter<T,
?> getConverter
(Class<T> clazz) Gets the registered converter for the the Class, orConverter.DEFAULT
if absent.static TypeHandler
Gets the default TypeHandler.static FileInputStream
Deprecated.
-
Constructor Details
-
TypeHandler
public TypeHandler()Constructs a default initialized instance. -
TypeHandler
Constructs a default initialized instance.For each entry, that Class' type must match the Converter's first type.
- Parameters:
converterMap
- The converter map, not null.- Since:
- 1.7.0
-
-
Method Details
-
createClass
Returns the class whose name isclassName
.- Parameters:
className
- the class name- Returns:
- The class if it is found
- Throws:
ParseException
- if the class could not be found
-
createDate
Returns the date represented bystring
.This method is not yet implemented and always throws an
UnsupportedOperationException
.- Parameters:
string
- the date string- Returns:
- The date if
string
is a valid date string, otherwise return null.
-
createDefaultMap
Creates a default converter map.- Returns:
- a default converter map.
- Since:
- 1.7.0
-
createFile
Returns the File represented bystring
.- Parameters:
string
- the File location- Returns:
- The file represented by
string
.
-
createFiles
Deprecated.with no replacementCreates the File[] represented bystring
.This method is not yet implemented and always throws an
UnsupportedOperationException
.- Parameters:
string
- the paths to the files- Returns:
- The File[] represented by
string
. - Throws:
UnsupportedOperationException
- always
-
createNumber
Deprecated.Creates a number from a String. If a '.' is present, it creates a Double, otherwise a Long.- Parameters:
string
- the value- Returns:
- the number represented by
string
- Throws:
ParseException
- ifstring
is not a number
-
createObject
Deprecated.Creates an Object from the class name and empty constructor.- Parameters:
className
- the argument value- Returns:
- the initialized object
- Throws:
ParseException
- if the class could not be found or the object could not be created
-
createURL
Creates the URL represented bystring
.- Parameters:
string
- the URL string- Returns:
- The URL in
string
is well-formed - Throws:
ParseException
- if the URL instring
is not well-formed
-
createValue
Creates the @code Object} of typeclazz
with the value ofstring
.- Type Parameters:
T
- type of argument- Parameters:
string
- the command line valueclazz
- the class representing the type of argument- Returns:
- The instance of
clazz
initialized with the value ofstring
. - Throws:
ParseException
- if the value creation for the given class threw an exception.
-
createValue
Deprecated.Creates theObject
of typeobj
with the value ofstring
.- Parameters:
string
- the command line valueobj
- the type of argument- Returns:
- The instance of
obj
initialized with the value ofstring
. - Throws:
ParseException
- if the value creation for the given object type failed
-
getDefault
Gets the default TypeHandler.- Returns:
- the default TypeHandler.
- Since:
- 1.7.0
-
openFile
Deprecated.Returns the opened FileInputStream represented bystring
.- Parameters:
string
- the file location- Returns:
- The file input stream represented by
string
. - Throws:
ParseException
- if the file is not exist or not readable
-
getConverter
Gets the registered converter for the the Class, orConverter.DEFAULT
if absent.- Type Parameters:
T
- The Class parameter type.- Parameters:
clazz
- The Class to get the Converter for.- Returns:
- the registered converter if any,
Converter.DEFAULT
otherwise. - Since:
- 1.7.0
-