Class TypeConversion


  • public class TypeConversion
    extends Object
    This class can perform various type conversions.
    • Constructor Detail

      • TypeConversion

        public TypeConversion()
    • Method Detail

      • getString

        public static String getString​(Object obj)
        Converts the specified object to a string. If the object is null, this method returns null. Otherwise it will call toString().
        Parameters:
        obj - the object or null
        Returns:
        the string or null
      • getInt

        public static Integer getInt​(Object obj)
                              throws ParseException
        Converts the specified object to an integer. If the object is null, this method returns null. If it's a Number, it will return the integer value. Otherwise it will call toString() and try to parse the string value.
        Parameters:
        obj - the object or null
        Returns:
        the integer or null
        Throws:
        ParseException - if a string value can't be parsed as an integer
      • getLong

        public static Long getLong​(Object obj)
                            throws ParseException
        Converts the specified object to a long. If the object is null, this method returns null. If it's a Number, it will return the long value. Otherwise it will call toString() and try to parse the string value.
        Parameters:
        obj - the object or null
        Returns:
        the long or null
        Throws:
        ParseException - if a string value can't be parsed as a long
      • getDouble

        public static Double getDouble​(Object obj)
                                throws ParseException
        Converts the specified object to a double. If the object is null, this method returns null. If it's a Number, it will return the double value. Otherwise it will call toString() and try to parse the string value.
        Parameters:
        obj - the object or null
        Returns:
        the double or null
        Throws:
        ParseException - if a string value can't be parsed as a double
      • getBoolean

        public static Boolean getBoolean​(Object obj)
                                  throws ParseException
        Converts the specified object to a boolean. If the object is null, this method returns null. If it's a Boolean, it will return the boolean value. Otherwise it will call toString() and try to parse the string value.
        Parameters:
        obj - the object or null
        Returns:
        the boolean or null
        Throws:
        ParseException - if a string value can't be parsed as a boolean
      • getEnum

        public static <T extends Enum<?>> T getEnum​(Object obj,
                                                    Class<T> enumClass)
                                             throws ParseException
        Converts the specified object to an enum. If the object is null, this method returns null. If it's an instance of the specified enum class, it just returns the object. Otherwise it gets the string value and does a case-insensitive comparison with the possible enum values.
        Type Parameters:
        T - the type of enum to return
        Parameters:
        obj - the object or null
        enumClass - the enum class
        Returns:
        the enum or null
        Throws:
        ParseException - if the enum value is invalid
      • getJson

        public static <T> T getJson​(Object obj,
                                    Class<T> clazz)
                             throws ParseException
        Converts an object to the specified type using the Jackson ObjectMapper. For example a Map could be converted to an object. This method does not parse JSON strings. If the object is null, this method returns null.
        Type Parameters:
        T - the return type
        Parameters:
        obj - the object or null
        clazz - the return type
        Returns:
        the converted object or null
        Throws:
        ParseException - if the object can't be converted to the specified type
      • getJson

        public static <T> T getJson​(Object obj,
                                    com.fasterxml.jackson.core.type.TypeReference<T> typeRef)
                             throws ParseException
        Converts an object to the specified type using the Jackson ObjectMapper. For example a Map could be converted to an object. This method does not parse JSON strings. If the object is null, this method returns null.

        If you want to convert to MyObject, you can specify:
        new TypeReference<MyObject>() {}

        Type Parameters:
        T - the return type
        Parameters:
        obj - the object or null
        typeRef - the return type
        Returns:
        the converted object or null
        Throws:
        ParseException - if the object can't be converted to the specified type