Class TypeConverter


  • public class TypeConverter
    extends java.lang.Object
    This class provides a whole bunch of helper functions for performing type conversions on values produced by various expressions. All values are passed around as Object references, and this means they may need to be converted into specific value types at some point. This class contains all of the conversion logic so it's in one place.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static java.util.HashMap<java.lang.Class,​SQLDataType> sqlTypeMapping
      A mapping from the various Java types used for expression results, to their corresponding SQL data types.
    • Constructor Summary

      Constructors 
      Constructor Description
      TypeConverter()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static Pair coerceArithmetic​(java.lang.Object obj1, java.lang.Object obj2)
      This function takes two arguments and coerces them to be the same numeric type, for use with arithmetic operations.
      static Pair coerceComparison​(java.lang.Object obj1, java.lang.Object obj2)
      This function takes two arguments and coerces them to be the same numeric type, for use with comparison operations.
      private static Pair coerceTemporalAmountArithmetic​(java.time.temporal.TemporalAmount obj1, java.lang.Object obj2)  
      private static Pair coerceTemporalArithmetic​(java.time.temporal.Temporal obj1, java.lang.Object obj2)  
      static java.lang.Object coerceTo​(java.lang.Object obj, ColumnType colType)  
      static java.math.BigDecimal getBigDecimalValue​(java.lang.Object obj)
      This method attempts to convert the input value into a BigDecimal value.
      static java.math.BigInteger getBigIntegerValue​(java.lang.Object obj)
      This method attempts to convert the input value into a BigInteger value.
      static java.lang.Boolean getBooleanValue​(java.lang.Object obj)
      This method attempts to convert the input value into a Boolean value.
      static java.lang.Byte getByteValue​(java.lang.Object obj)
      This method attempts to convert the input value into a Byte value.
      static java.time.LocalDateTime getDateTimeValue​(java.lang.Object obj)  
      static java.time.LocalDate getDateValue​(java.lang.Object obj)  
      static java.lang.Double getDoubleValue​(java.lang.Object obj)
      This method attempts to convert the input value into a Double value.
      static java.lang.Float getFloatValue​(java.lang.Object obj)
      This method attempts to convert the input value into a Float value.
      static java.lang.Integer getIntegerValue​(java.lang.Object obj)
      This method attempts to convert the input value into an Integer value.
      static java.lang.Long getLongValue​(java.lang.Object obj)
      This method attempts to convert the input value into a Long value.
      static java.lang.Short getShortValue​(java.lang.Object obj)
      This method attempts to convert the input value into a Short value.
      static SQLDataType getSQLType​(java.lang.Object obj)
      This method attempts to assign a SQL data type to a value produced by the expression classes.
      static java.lang.String getStringValue​(java.lang.Object obj)
      This method converts the input value into a String value by calling Object.toString() on the input.
      static java.time.LocalTime getTimeValue​(java.lang.Object obj)  
      • Methods inherited from class java.lang.Object

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

      • sqlTypeMapping

        private static final java.util.HashMap<java.lang.Class,​SQLDataType> sqlTypeMapping
        A mapping from the various Java types used for expression results, to their corresponding SQL data types. The mapping is populated by a static initializer block.
    • Constructor Detail

      • TypeConverter

        public TypeConverter()
    • Method Detail

      • getBooleanValue

        public static java.lang.Boolean getBooleanValue​(java.lang.Object obj)
        This method attempts to convert the input value into a Boolean value. If the input is a nonzero number then the result is Boolean.TRUE; if it is a zero number then the result is Boolean.FALSE. Otherwise a TypeCastException is thrown.
        Parameters:
        obj - the input value to cast
        Returns:
        the input value cast to a Boolean
        Throws:
        TypeCastException - if the input value cannot be cast to a Boolean value.
      • getByteValue

        public static java.lang.Byte getByteValue​(java.lang.Object obj)
        This method attempts to convert the input value into a Byte value. If the input is a number then the result is generated from the Number.byteValue() method, possibly causing truncation or overflow to occur. If the input is a String that can be parsed into a byte then the result is the parsed value. If none of these cases hold then a TypeCastException is thrown.
        Parameters:
        obj - the input value to cast
        Returns:
        the input value cast to a Byte
        Throws:
        TypeCastException - if the input value cannot be cast to a byte.
      • getShortValue

        public static java.lang.Short getShortValue​(java.lang.Object obj)
        This method attempts to convert the input value into a Short value. If the input is a number then the result is generated from the Number.shortValue() method, possibly causing truncation or overflow to occur. If the input is a String that can be parsed into a short then the result is the parsed value. If none of these cases hold then a TypeCastException is thrown.
        Parameters:
        obj - the input value to cast
        Returns:
        the input value cast to a Short
        Throws:
        TypeCastException - if the input value cannot be cast to a short.
      • getIntegerValue

        public static java.lang.Integer getIntegerValue​(java.lang.Object obj)
        This method attempts to convert the input value into an Integer value. If the input is a number then the result is generated from the Number.intValue() method, possibly causing truncation or overflow to occur. If the input is a String that can be parsed into an integer then the result is the parsed value. If none of these cases hold then a TypeCastException is thrown.
        Parameters:
        obj - the input value to cast
        Returns:
        the input value cast to an Integer
        Throws:
        TypeCastException - if the input value cannot be cast to an integer.
      • getLongValue

        public static java.lang.Long getLongValue​(java.lang.Object obj)
        This method attempts to convert the input value into a Long value. If the input is a number then the result is generated from the Number.longValue() method, possibly causing truncation or overflow to occur. If the input is a String that can be parsed into a long then the result is the parsed value. If none of these cases hold then a TypeCastException is thrown.
        Parameters:
        obj - the input value to cast
        Returns:
        the input value cast to a Long
        Throws:
        TypeCastException - if the input value cannot be cast to a long.
      • getFloatValue

        public static java.lang.Float getFloatValue​(java.lang.Object obj)
        This method attempts to convert the input value into a Float value. If the input is a number then the result is generated from the Number.floatValue() method, possibly causing truncation or overflow to occur. If the input is a String that can be parsed into a float then the result is the parsed value. If none of these cases hold then a TypeCastException is thrown.
        Parameters:
        obj - the input value to cast
        Returns:
        the input value cast to a Float
        Throws:
        TypeCastException - if the input value cannot be cast to a float.
      • getDoubleValue

        public static java.lang.Double getDoubleValue​(java.lang.Object obj)
        This method attempts to convert the input value into a Double value. If the input is a number then the result is generated from the Number.doubleValue() method, possibly causing truncation or overflow to occur. If the input is a String that can be parsed into a double then the result is the parsed value. If none of these cases hold then a TypeCastException is thrown.
        Parameters:
        obj - the input value to cast
        Returns:
        the input value cast to a Double
        Throws:
        TypeCastException - if the input value cannot be cast to a double.
      • getBigIntegerValue

        public static java.math.BigInteger getBigIntegerValue​(java.lang.Object obj)
        This method attempts to convert the input value into a BigInteger value. If the input is a number then the result is generated from the Number.longValue() method, possibly causing truncation to occur. If the input is a String that can be parsed into a BigInteger then the result is the parsed value. If none of these cases hold then a TypeCastException is thrown.
        Parameters:
        obj - the input value to cast
        Returns:
        the input value cast to a BigInteger
        Throws:
        TypeCastException - if the input value cannot be cast to a BigInteger.
      • getBigDecimalValue

        public static java.math.BigDecimal getBigDecimalValue​(java.lang.Object obj)
        This method attempts to convert the input value into a BigDecimal value. If the input is a number then the result is generated from the Number.longValue() method, possibly causing truncation to occur. If the input is a String that can be parsed into a BigInteger then the result is the parsed value. If none of these cases hold then a TypeCastException is thrown.
        Parameters:
        obj - the input value to cast
        Returns:
        the input value cast to a BigInteger
        Throws:
        TypeCastException - if the input value cannot be cast to a BigInteger.
      • getStringValue

        public static java.lang.String getStringValue​(java.lang.Object obj)
        This method converts the input value into a String value by calling Object.toString() on the input.
        Parameters:
        obj - the input value to cast
        Returns:
        the input value cast to a String
      • getDateValue

        public static java.time.LocalDate getDateValue​(java.lang.Object obj)
      • getTimeValue

        public static java.time.LocalTime getTimeValue​(java.lang.Object obj)
      • getDateTimeValue

        public static java.time.LocalDateTime getDateTimeValue​(java.lang.Object obj)
      • coerceArithmetic

        public static Pair coerceArithmetic​(java.lang.Object obj1,
                                            java.lang.Object obj2)
                                     throws TypeCastException
        This function takes two arguments and coerces them to be the same numeric type, for use with arithmetic operations.

        If either or both of the arguments are null then no coercion is performed, and the results are returned in a pair-object. The reason for this is that the comparison will simply evaluate to UNKNOWN, so no coercion is required.

        Parameters:
        obj1 - the first input value to cast
        obj2 - the second input value to cast
        Returns:
        an object holding the two input values, both converted to a type suitable for arithmetic
        Throws:
        TypeCastException - if both inputs are non-null and they cannot both be cast to types suitable for arithmetic.
      • coerceTemporalAmountArithmetic

        private static Pair coerceTemporalAmountArithmetic​(java.time.temporal.TemporalAmount obj1,
                                                           java.lang.Object obj2)
                                                    throws TypeCastException
        Throws:
        TypeCastException
      • coerceComparison

        public static Pair coerceComparison​(java.lang.Object obj1,
                                            java.lang.Object obj2)
                                     throws TypeCastException
        This function takes two arguments and coerces them to be the same numeric type, for use with comparison operations. It is up to the caller to ensure that the types are actually comparable (although all recognized types in this function are comparable).

        If either or both of the arguments are null then no coercion is performed, and the results are returned in a pair-object. The reason for this is that the comparison will simply evaluate to UNKNOWN, so no coercion is required.

        Parameters:
        obj1 - the first input value to cast
        obj2 - the second input value to cast
        Returns:
        an object holding the two input values, both converted to a type suitable for comparison
        Throws:
        TypeCastException - if both inputs are non-null and they cannot both be cast to types suitable for comparison.
        Design Note:
        When new types are added in the future (e.g. date and/or time values), this function will need to be updated with the new types.
      • getSQLType

        public static SQLDataType getSQLType​(java.lang.Object obj)
        This method attempts to assign a SQL data type to a value produced by the expression classes. If a Java type is not recognized as a particular SQL data type then null is returned.
        Parameters:
        obj - The object to determine a SQL data type for.
        Returns:
        The corresponding SQL data type, or null if the input value's type doesn't have an obvious corresponding SQL data type.
      • coerceTo

        public static java.lang.Object coerceTo​(java.lang.Object obj,
                                                ColumnType colType)