Package org.apache.commons.cli
Class TypeHandler
- java.lang.Object
-
- org.apache.commons.cli.TypeHandler
-
public class TypeHandler extends java.lang.Object
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.
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.Map<java.lang.Class<?>,Converter<?,? extends java.lang.Throwable>>
converterMap
Map of Class to Converter.private static TypeHandler
DEFAULT
The default TypeHandler.private static int
HEX_RADIX
Value of hex conversion of strings
-
Constructor Summary
Constructors Constructor Description TypeHandler()
Constructs a default initialized instance.TypeHandler(java.util.Map<java.lang.Class<?>,Converter<?,? extends java.lang.Throwable>> converterMap)
Constructs a default initialized instance.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static java.lang.Class<?>
createClass(java.lang.String className)
Returns the class whose name isclassName
.static java.util.Date
createDate(java.lang.String string)
Returns the date represented bystring
.static java.util.Map<java.lang.Class<?>,Converter<?,? extends java.lang.Throwable>>
createDefaultMap()
Creates a default converter map.static java.io.File
createFile(java.lang.String string)
Returns the File represented bystring
.static java.io.File[]
createFiles(java.lang.String string)
Deprecated.with no replacementstatic java.lang.Number
createNumber(java.lang.String string)
Deprecated.static java.lang.Object
createObject(java.lang.String className)
Deprecated.static java.net.URL
createURL(java.lang.String string)
Creates the URL represented bystring
.static <T> T
createValue(java.lang.String string, java.lang.Class<T> clazz)
Creates the @code Object} of typeclazz
with the value ofstring
.static java.lang.Object
createValue(java.lang.String string, java.lang.Object obj)
Deprecated.private static <T> T
createValueUnchecked(java.lang.String string, java.lang.Class<T> clazz)
Delegates tocreateValue(String, Class)
throwing IllegalArgumentException instead of ParseException.<T> Converter<T,?>
getConverter(java.lang.Class<T> clazz)
Gets the registered converter for the the Class, orConverter.DEFAULT
if absent.static TypeHandler
getDefault()
Gets the default TypeHandler.static java.io.FileInputStream
openFile(java.lang.String string)
Deprecated.private static java.util.Map<java.lang.Class<?>,Converter<?,? extends java.lang.Throwable>>
putDefaultMap(java.util.Map<java.lang.Class<?>,Converter<?,? extends java.lang.Throwable>> map)
-
-
-
Field Detail
-
DEFAULT
private static final TypeHandler DEFAULT
The default TypeHandler.
-
HEX_RADIX
private static final int HEX_RADIX
Value of hex conversion of strings- See Also:
- Constant Field Values
-
converterMap
private final java.util.Map<java.lang.Class<?>,Converter<?,? extends java.lang.Throwable>> converterMap
Map of Class to Converter.For each entry, that Class' type must match the Converter's first type.
-
-
Constructor Detail
-
TypeHandler
public TypeHandler()
Constructs a default initialized instance.
-
TypeHandler
public TypeHandler(java.util.Map<java.lang.Class<?>,Converter<?,? extends java.lang.Throwable>> converterMap)
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 Detail
-
createClass
public static java.lang.Class<?> createClass(java.lang.String className) throws ParseException
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
public static java.util.Date createDate(java.lang.String string)
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
public static java.util.Map<java.lang.Class<?>,Converter<?,? extends java.lang.Throwable>> createDefaultMap()
Creates a default converter map.- Returns:
- a default converter map.
- Since:
- 1.7.0
-
createFile
public static java.io.File createFile(java.lang.String string)
Returns the File represented bystring
.- Parameters:
string
- the File location- Returns:
- The file represented by
string
.
-
createFiles
@Deprecated public static java.io.File[] createFiles(java.lang.String string)
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:
java.lang.UnsupportedOperationException
- always
-
createNumber
@Deprecated public static java.lang.Number createNumber(java.lang.String string) throws ParseException
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 public static java.lang.Object createObject(java.lang.String className) throws ParseException
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
public static java.net.URL createURL(java.lang.String string) throws ParseException
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
public static <T> T createValue(java.lang.String string, java.lang.Class<T> clazz) throws ParseException
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 public static java.lang.Object createValue(java.lang.String string, java.lang.Object obj) throws ParseException
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
-
createValueUnchecked
private static <T> T createValueUnchecked(java.lang.String string, java.lang.Class<T> clazz)
Delegates tocreateValue(String, Class)
throwing IllegalArgumentException instead of ParseException.- 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:
java.lang.IllegalArgumentException
- if the value creation for the given class threw an exception.
-
getDefault
public static TypeHandler getDefault()
Gets the default TypeHandler.- Returns:
- the default TypeHandler.
- Since:
- 1.7.0
-
openFile
@Deprecated public static java.io.FileInputStream openFile(java.lang.String string) throws ParseException
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
-
putDefaultMap
private static java.util.Map<java.lang.Class<?>,Converter<?,? extends java.lang.Throwable>> putDefaultMap(java.util.Map<java.lang.Class<?>,Converter<?,? extends java.lang.Throwable>> map)
-
getConverter
public <T> Converter<T,?> getConverter(java.lang.Class<T> clazz)
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
-
-