Class 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 is className.
      static java.util.Date createDate​(java.lang.String string)
      Returns the date represented by string.
      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 by string.
      static java.io.File[] createFiles​(java.lang.String string)
      Deprecated.
      with no replacement
      static java.lang.Number createNumber​(java.lang.String string)
      Deprecated.
      static java.lang.Object createObject​(java.lang.String className)
      static java.net.URL createURL​(java.lang.String string)
      Creates the URL represented by string.
      static <T> T createValue​(java.lang.String string, java.lang.Class<T> clazz)
      Creates the @code Object} of type clazz with the value of string.
      static java.lang.Object createValue​(java.lang.String string, java.lang.Object obj)
      private static <T> T createValueUnchecked​(java.lang.String string, java.lang.Class<T> clazz)
      Delegates to createValue(String, Class) throwing IllegalArgumentException instead of ParseException.
      <T> Converter<T,​?> getConverter​(java.lang.Class<T> clazz)
      Gets the registered converter for the the Class, or Converter.DEFAULT if absent.
      static TypeHandler getDefault()
      Gets the default TypeHandler.
      static java.io.FileInputStream openFile​(java.lang.String string)
      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)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • 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 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 java.util.Date createDate​(java.lang.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 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 by string.
        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 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:
        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 - if string is not a number
      • createObject

        @Deprecated
        public static java.lang.Object createObject​(java.lang.String className)
                                             throws ParseException
        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 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​(java.lang.String string,
                                        java.lang.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 java.lang.Object createValue​(java.lang.String string,
                                                   java.lang.Object obj)
                                            throws ParseException
        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
      • createValueUnchecked

        private static <T> T createValueUnchecked​(java.lang.String string,
                                                  java.lang.Class<T> clazz)
        Delegates to createValue(String, Class) throwing IllegalArgumentException instead of ParseException.
        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:
        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
        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
      • 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, 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