Package edu.caltech.nanodb.expressions
Class TypeConverter
- java.lang.Object
-
- edu.caltech.nanodb.expressions.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 asObject
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 aBigDecimal
value.static java.math.BigInteger
getBigIntegerValue(java.lang.Object obj)
This method attempts to convert the input value into aBigInteger
value.static java.lang.Boolean
getBooleanValue(java.lang.Object obj)
This method attempts to convert the input value into aBoolean
value.static java.lang.Byte
getByteValue(java.lang.Object obj)
This method attempts to convert the input value into aByte
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 aDouble
value.static java.lang.Float
getFloatValue(java.lang.Object obj)
This method attempts to convert the input value into aFloat
value.static java.lang.Integer
getIntegerValue(java.lang.Object obj)
This method attempts to convert the input value into anInteger
value.static java.lang.Long
getLongValue(java.lang.Object obj)
This method attempts to convert the input value into aLong
value.static java.lang.Short
getShortValue(java.lang.Object obj)
This method attempts to convert the input value into aShort
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 aString
value by callingObject.toString()
on the input.static java.time.LocalTime
getTimeValue(java.lang.Object obj)
-
-
-
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.
-
-
Method Detail
-
getBooleanValue
public static java.lang.Boolean getBooleanValue(java.lang.Object obj)
This method attempts to convert the input value into aBoolean
value. If the input is a nonzero number then the result isBoolean.TRUE
; if it is a zero number then the result isBoolean.FALSE
. Otherwise aTypeCastException
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 aByte
value. If the input is a number then the result is generated from theNumber.byteValue()
method, possibly causing truncation or overflow to occur. If the input is aString
that can be parsed into a byte then the result is the parsed value. If none of these cases hold then aTypeCastException
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 aShort
value. If the input is a number then the result is generated from theNumber.shortValue()
method, possibly causing truncation or overflow to occur. If the input is aString
that can be parsed into a short then the result is the parsed value. If none of these cases hold then aTypeCastException
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 anInteger
value. If the input is a number then the result is generated from theNumber.intValue()
method, possibly causing truncation or overflow to occur. If the input is aString
that can be parsed into an integer then the result is the parsed value. If none of these cases hold then aTypeCastException
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 aLong
value. If the input is a number then the result is generated from theNumber.longValue()
method, possibly causing truncation or overflow to occur. If the input is aString
that can be parsed into a long then the result is the parsed value. If none of these cases hold then aTypeCastException
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 aFloat
value. If the input is a number then the result is generated from theNumber.floatValue()
method, possibly causing truncation or overflow to occur. If the input is aString
that can be parsed into a float then the result is the parsed value. If none of these cases hold then aTypeCastException
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 aDouble
value. If the input is a number then the result is generated from theNumber.doubleValue()
method, possibly causing truncation or overflow to occur. If the input is aString
that can be parsed into a double then the result is the parsed value. If none of these cases hold then aTypeCastException
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 aBigInteger
value. If the input is a number then the result is generated from theNumber.longValue()
method, possibly causing truncation to occur. If the input is aString
that can be parsed into a BigInteger then the result is the parsed value. If none of these cases hold then aTypeCastException
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 aBigDecimal
value. If the input is a number then the result is generated from theNumber.longValue()
method, possibly causing truncation to occur. If the input is aString
that can be parsed into a BigInteger then the result is the parsed value. If none of these cases hold then aTypeCastException
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 aString
value by callingObject.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 castobj2
- 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.
-
coerceTemporalArithmetic
private static Pair coerceTemporalArithmetic(java.time.temporal.Temporal obj1, java.lang.Object obj2) throws TypeCastException
- Throws:
TypeCastException
-
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 castobj2
- 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)
-
-