Class TypeConverter


  • public class TypeConverter
    extends Object
    Type converter class for converting types to others.
    Author:
    Matthew Tropiano
    • Constructor Detail

      • TypeConverter

        public TypeConverter​(TypeProfileFactory profileFactory)
        Creates a type profiler.
        Parameters:
        profileFactory - the profile factory to use for caching reflection info.
    • Method Detail

      • createForType

        public <T> T createForType​(Object object,
                                   Class<T> targetType)
        Reflect.creates a new instance of an object for placement in a POJO or elsewhere.
        Type Parameters:
        T - the return object type.
        Parameters:
        object - the object to convert to another object
        targetType - the target class type to convert to, if the types differ.
        Returns:
        a suitable object of type targetType.
        Throws:
        ClassCastException - if the incoming type cannot be converted.
      • createForType

        public <T> T createForType​(String memberName,
                                   Object object,
                                   Class<T> targetType)
        Reflect.creates a new instance of an object for placement in a POJO or elsewhere.
        Type Parameters:
        T - the return object type.
        Parameters:
        memberName - the name of the member that is being converted (for reporting).
        object - the object to convert to another object
        targetType - the target class type to convert to, if the types differ.
        Returns:
        a suitable object of type targetType.
        Throws:
        ClassCastException - if the incoming type cannot be converted.
      • applyMemberToObject

        public <T> void applyMemberToObject​(String name,
                                            Object value,
                                            T targetObject)
        Applies an object value to a target object via a "field" name (setter/field).
        Type Parameters:
        T - the target object type.
        Parameters:
        name - the field/setter name - this can be a member name or an alias.
        value - the value to apply/convert.
        targetObject - the target object to set stuff on.
      • convertBoolean

        protected final <T> T convertBoolean​(String memberName,
                                             Boolean b,
                                             Class<T> targetType)
        Converts a boolean to another type.
        Type Parameters:
        T - the target value type.
        Parameters:
        memberName - the name of the member being converted (for logging).
        b - the value to convert.
        targetType - the target type.
        Returns:
        the resultant type.
      • convertNumber

        protected final <T> T convertNumber​(String memberName,
                                            Number n,
                                            Class<T> targetType)
        Converts a numeric value to a target type.
        Type Parameters:
        T - the target value type.
        Parameters:
        memberName - the name of the member being converted (for logging).
        n - the value to convert.
        targetType - the target type.
        Returns:
        the resultant type.
      • convertCharacter

        protected final <T> T convertCharacter​(String memberName,
                                               Character c,
                                               Class<T> targetType)
        Converts a character value to a target type.
        Type Parameters:
        T - the target value type.
        Parameters:
        memberName - the name of the member being converted (for logging).
        c - the value to convert.
        targetType - the target type.
        Returns:
        the resultant type.
      • convertDate

        protected final <T> T convertDate​(String memberName,
                                          Date d,
                                          Class<T> targetType)
        Converts a date value to a target type.
        Type Parameters:
        T - the target value type.
        Parameters:
        memberName - the name of the member being converted (for logging).
        d - the value to convert.
        targetType - the target type.
        Returns:
        the resultant type.
      • convertEnum

        protected final <T> T convertEnum​(String memberName,
                                          Enum<?> e,
                                          Class<T> targetType)
        Converts an enum value to a target type.
        Type Parameters:
        T - the target value type.
        Parameters:
        memberName - the name of the member being converted (for logging).
        e - the value to convert.
        targetType - the target type.
        Returns:
        the resultant type.
      • convertString

        protected final <T> T convertString​(String memberName,
                                            String s,
                                            Class<T> targetType)
        Converts a string value to a target type.
        Type Parameters:
        T - the target value type.
        Parameters:
        memberName - the name of the member being converted (for logging).
        s - the value to convert.
        targetType - the target type.
        Returns:
        the resultant type.
      • convertArray

        protected final <T> T convertArray​(String memberName,
                                           Object array,
                                           Class<T> targetType)
        Converts an array value to a target type.
        Type Parameters:
        T - the target value type.
        Parameters:
        memberName - the name of the member being converted (for logging).
        array - the value to convert.
        targetType - the target type.
        Returns:
        the resultant type.
      • convertCharArray

        protected final <T> T convertCharArray​(String memberName,
                                               char[] charArray,
                                               Class<T> targetType)
        Converts a char array value to a target type.
        Type Parameters:
        T - the target value type.
        Parameters:
        memberName - the name of the member being converted (for logging).
        charArray - the value to convert.
        targetType - the target type.
        Returns:
        the resultant type.
      • convertByteArray

        protected final <T> T convertByteArray​(String memberName,
                                               byte[] byteArray,
                                               Class<T> targetType)
        Converts a byte array value to a target type.
        Type Parameters:
        T - the target value type.
        Parameters:
        memberName - the name of the member being converted (for logging).
        byteArray - the value to convert.
        targetType - the target type.
        Returns:
        the resultant type.
      • convertOtherArray

        protected final <T> T convertOtherArray​(String memberName,
                                                Object array,
                                                Class<T> targetType)
        Converts a totally different array type.
        Type Parameters:
        T - the target value type.
        Parameters:
        memberName - the name of the member being converted (for logging).
        array - the value to convert.
        targetType - the target type.
        Returns:
        the resultant type.
      • convertIterable

        protected final <T> T convertIterable​(String memberName,
                                              Iterable<?> iter,
                                              Class<T> targetType)
        Converts an iterable to another type (like an array).
        Type Parameters:
        T - the target value type.
        Parameters:
        memberName - the name of the member being converted (for logging).
        iter - the value to convert.
        targetType - the target type.
        Returns:
        the resultant type.