Class TypeConverter


  • public final class TypeConverter
    extends java.lang.Object
    A utility class that can convert a String value as a typed object.
    Version:
    $Revision: $
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static java.util.Map<java.lang.Class<?>,​java.lang.Class<?>> PRIMITIVES
      A map of primitive to objects.
      private static java.lang.String VALUE_OF_METHOD  
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private TypeConverter()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.Boolean getBooleanValue​(java.lang.String source)
      Returns a Boolean value from a String.
      static <T> T getType​(java.lang.Class<T> targetType, java.lang.String source)
      A generic method that returns the String as the specified Java type.
      private static <T> T getTypeViaStringConstructor​(java.lang.String source, java.lang.Class<T> targetType)  
      static <T> T getTypeViaValueOfMethod​(java.lang.String source, java.lang.Class<T> targetType)  
      static boolean isConvertable​(java.lang.Class<?> targetType)
      Tests if the class can safely be converted from a String to the specified type.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • PRIMITIVES

        private static final java.util.Map<java.lang.Class<?>,​java.lang.Class<?>> PRIMITIVES
        A map of primitive to objects.
    • Constructor Detail

      • TypeConverter

        private TypeConverter()
    • Method Detail

      • getType

        public static <T> T getType​(java.lang.Class<T> targetType,
                                    java.lang.String source)
        A generic method that returns the String as the specified Java type.
        Type Parameters:
        T - the type to return
        Parameters:
        source - the string value to convert
        targetType -
        Returns:
        the object instance
      • isConvertable

        public static boolean isConvertable​(java.lang.Class<?> targetType)
        Tests if the class can safely be converted from a String to the specified type.
        Parameters:
        targetType - the type to convert to
        Returns:
        true if the class possesses either a "valueOf()" method or a constructor with a String parameter.
      • getBooleanValue

        public static java.lang.Boolean getBooleanValue​(java.lang.String source)

        Returns a Boolean value from a String. Unlike {@link Boolean.#valueOf(String)}, this method takes more String options. The following String values will return true:

        • Yes
        • Y
        • T
        • 1

        While the following values will return false:

        • No
        • N
        • F
        • 0
        Parameters:
        source -
        Returns:
      • getTypeViaValueOfMethod

        public static <T> T getTypeViaValueOfMethod​(java.lang.String source,
                                                    java.lang.Class<T> targetType)
                                             throws java.lang.NoSuchMethodException
        Type Parameters:
        T -
        Parameters:
        source -
        targetType -
        Returns:
        Throws:
        java.lang.NoSuchMethodException
      • getTypeViaStringConstructor

        private static <T> T getTypeViaStringConstructor​(java.lang.String source,
                                                         java.lang.Class<T> targetType)
        Type Parameters:
        T -
        Parameters:
        source -
        targetType -
        Returns:
        Throws:
        java.lang.IllegalArgumentException
        java.lang.InstantiationException
        java.lang.IllegalAccessException
        java.lang.reflect.InvocationTargetException