Package org.apache.commons.cli

# Class TypeHandler

java.lang.Object
org.apache.commons.cli.TypeHandler

public class TypeHandler extends 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.

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `TypeHandler()`
  Constructs a default initialized instance.
  `TypeHandler(Map<Class<?>,Converter<?,? extends Throwable>> converterMap)`
  Constructs a default initialized instance.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `static Class<?>`
  `createClass(String className)`
  Returns the class whose name is `className`.
  `static Date`
  `createDate(String string)`
  Returns the date represented by `string`.
  `static Map<Class<?>,Converter<?,? extends Throwable>>`
  `createDefaultMap()`
  Creates a default converter map.
  `static File`
  `createFile(String string)`
  Returns the File represented by `string`.
  `static File[]`
  `createFiles(String string)`
  Deprecated.
  with no replacement
  `static Number`
  `createNumber(String string)`
  Deprecated.
  `static Object`
  `createObject(String className)`
  Deprecated.
  use `createValue(String, Class)`
  `static URL`
  `createURL(String string)`
  Creates the URL represented by `string`.
  `static <T> T`
  `createValue(String string, Class<T> clazz)`
  Creates the @code Object} of type `clazz` with the value of `string`.
  `static Object`
  `createValue(String string, Object obj)`
  Deprecated.
  use `createValue(String, Class)`
  `<T> Converter<T,?>`
  `getConverter(Class<T> clazz)`
  Gets the registered converter for the the Class, or `Converter.DEFAULT` if absent.
  `static TypeHandler`
  `getDefault()`
  Gets the default TypeHandler.
  `static FileInputStream`
  `openFile(String string)`
  Deprecated.
  use `createValue(String, Class)`

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

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

- ## Constructor Details

  - ### TypeHandler

    public TypeHandler()
    Constructs a default initialized instance.

  - ### TypeHandler

    public TypeHandler(Map<Class<?>,Converter<?,? extends 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 Details

  - ### createClass

    public static Class<?> createClass(String className) throws ParseException
    Returns the class whose name is `className`.
    Parameters:
      `className` - the class name
    Returns:
      The class if it is found
    Throws:
      `ParseException` - if the class could not be found

  - ### createDate

    public static Date createDate(String string)
    Returns the date represented by `string`.

    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 Map<Class<?>,Converter<?,? extends Throwable>> createDefaultMap()
    Creates a default converter map.
    Returns:
      a default converter map.
    Since:
      1.7.0

  - ### createFile

    public static File createFile(String string)
    Returns the File represented by `string`.
    Parameters:
      `string` - the File location
    Returns:
      The file represented by `string`.

  - ### createFiles

    @Deprecated public static File[] createFiles(String string)
    Deprecated.
    with no replacement
    Creates the File[] represented by `string`.

    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 public static Number createNumber(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` - if `string` is not a number

  - ### createObject

    @Deprecated public static Object createObject(String className) throws ParseException
    Deprecated.
    use `createValue(String, Class)`
    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 URL createURL(String string) throws ParseException
    Creates the URL represented by `string`.
    Parameters:
      `string` - the URL string
    Returns:
      The URL in `string` is well-formed
    Throws:
      `ParseException` - if the URL in `string` is not well-formed

  - ### createValue

    public static <T> T createValue(String string, Class<T> clazz) throws ParseException
    Creates the @code Object} of type `clazz` with the value of `string`.
    Type Parameters:
      `T` - type of argument
    Parameters:
      `string` - the command line value
      `clazz` - the class representing the type of argument
    Returns:
      The instance of `clazz` initialized with the value of `string`.
    Throws:
      `ParseException` - if the value creation for the given class threw an exception.

  - ### createValue

    @Deprecated public static Object createValue(String string, Object obj) throws ParseException
    Deprecated.
    use `createValue(String, Class)`
    Creates the `Object` of type `obj` with the value of `string`.
    Parameters:
      `string` - the command line value
      `obj` - the type of argument
    Returns:
      The instance of `obj` initialized with the value of `string`.
    Throws:
      `ParseException` - if the value creation for the given object type failed

  - ### getDefault

    public static TypeHandler getDefault()
    Gets the default TypeHandler.
    Returns:
      the default TypeHandler.
    Since:
      1.7.0

  - ### openFile

    @Deprecated public static FileInputStream openFile(String string) throws ParseException
    Deprecated.
    use `createValue(String, Class)`
    Returns the opened FileInputStream represented by `string`.
    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

    public <T> Converter<T,?> getConverter(Class<T> clazz)
    Gets the registered converter for the the Class, or `Converter.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