@Test public void castBoolean(){ assertEquals(Boolean.TRUE, TypeUtil.cast(DataType.BOOLEAN, (byte)1)); assertEquals(Boolean.TRUE, TypeUtil.cast(DataType.BOOLEAN, (short)1)); assertEquals(Boolean.TRUE, TypeUtil.cast(DataType.BOOLEAN, 1)); assertEquals(Boolean.TRUE, TypeUtil.cast(DataType.BOOLEAN, 1l)); assertEquals(Boolean.TRUE, TypeUtil.cast(DataType.BOOLEAN, 1f)); assertEquals(Boolean.TRUE, TypeUtil.cast(DataType.BOOLEAN, 1.0f)); assertEquals(Boolean.TRUE, TypeUtil.cast(DataType.BOOLEAN, 1d)); assertEquals(Boolean.TRUE, TypeUtil.cast(DataType.BOOLEAN, 1.0d)); try { TypeUtil.cast(DataType.BOOLEAN, Math.nextUp(1f)); fail(); } catch(TypeCheckException tce){ } try { TypeUtil.cast(DataType.BOOLEAN, Math.nextUp(1d)); fail(); } catch(TypeCheckException tce){ } }
static public Object cast(DataType dataType, Object value){ switch(dataType){ case STRING: return toString(value); case INTEGER: return toInteger(value); case FLOAT: return toFloat(value); case DOUBLE: return toDouble(value); case BOOLEAN: return toBoolean(value); case DATE: return toDate(value); case TIME: return toTime(value); case DATE_TIME: return toDateTime(value); case DATE_DAYS_SINCE_0: throw new NotImplementedException(); case DATE_DAYS_SINCE_1960: return toDaysSinceDate(Epochs.YEAR_1960, value); case DATE_DAYS_SINCE_1970: return toDaysSinceDate(Epochs.YEAR_1970, value); case DATE_DAYS_SINCE_1980: return toDaysSinceDate(Epochs.YEAR_1980, value); case TIME_SECONDS: return toSecondsSinceMidnight(value); case DATE_TIME_SECONDS_SINCE_0: throw new NotImplementedException(); case DATE_TIME_SECONDS_SINCE_1960: return toSecondsSinceDate(Epochs.YEAR_1960, value); case DATE_TIME_SECONDS_SINCE_1970: return toSecondsSinceDate(Epochs.YEAR_1970, value); case DATE_TIME_SECONDS_SINCE_1980: return toSecondsSinceDate(Epochs.YEAR_1980, value); default: throw new IllegalArgumentException(); } }
TypeUtil { static public Object cast(DataType dataType, Object value){ switch(dataType){ case STRING: return toString(value); case INTEGER: return toInteger(value); case FLOAT: return toFloat(value); case DOUBLE: return toDouble(value); case BOOLEAN: return toBoolean(value); case DATE: return toDate(value); case TIME: return toTime(value); case DATE_TIME: return toDateTime(value); case DATE_DAYS_SINCE_0: throw new NotImplementedException(); case DATE_DAYS_SINCE_1960: return toDaysSinceDate(Epochs.YEAR_1960, value); case DATE_DAYS_SINCE_1970: return toDaysSinceDate(Epochs.YEAR_1970, value); case DATE_DAYS_SINCE_1980: return toDaysSinceDate(Epochs.YEAR_1980, value); case TIME_SECONDS: return toSecondsSinceMidnight(value); case DATE_TIME_SECONDS_SINCE_0: throw new NotImplementedException(); case DATE_TIME_SECONDS_SINCE_1960: return toSecondsSinceDate(Epochs.YEAR_1960, value); case DATE_TIME_SECONDS_SINCE_1970: return toSecondsSinceDate(Epochs.YEAR_1970, value); case DATE_TIME_SECONDS_SINCE_1980: return toSecondsSinceDate(Epochs.YEAR_1980, value); default: throw new IllegalArgumentException(); } } }
TypeUtil { static public Object cast(DataType dataType, Object value){ switch(dataType){ case STRING: return toString(value); case INTEGER: return toInteger(value); case FLOAT: return toFloat(value); case DOUBLE: return toDouble(value); case BOOLEAN: return toBoolean(value); case DATE: return toDate(value); case TIME: return toTime(value); case DATE_TIME: return toDateTime(value); case DATE_DAYS_SINCE_0: throw new NotImplementedException(); case DATE_DAYS_SINCE_1960: return toDaysSinceDate(Epochs.YEAR_1960, value); case DATE_DAYS_SINCE_1970: return toDaysSinceDate(Epochs.YEAR_1970, value); case DATE_DAYS_SINCE_1980: return toDaysSinceDate(Epochs.YEAR_1980, value); case TIME_SECONDS: return toSecondsSinceMidnight(value); case DATE_TIME_SECONDS_SINCE_0: throw new NotImplementedException(); case DATE_TIME_SECONDS_SINCE_1960: return toSecondsSinceDate(Epochs.YEAR_1960, value); case DATE_TIME_SECONDS_SINCE_1970: return toSecondsSinceDate(Epochs.YEAR_1970, value); case DATE_TIME_SECONDS_SINCE_1980: return toSecondsSinceDate(Epochs.YEAR_1980, value); default: throw new IllegalArgumentException(); } } private  TypeUtil(); }
TypeUtil { static public Object cast(DataType dataType, Object value){ switch(dataType){ case STRING: return toString(value); case INTEGER: return toInteger(value); case FLOAT: return toFloat(value); case DOUBLE: return toDouble(value); case BOOLEAN: return toBoolean(value); case DATE: return toDate(value); case TIME: return toTime(value); case DATE_TIME: return toDateTime(value); case DATE_DAYS_SINCE_0: throw new NotImplementedException(); case DATE_DAYS_SINCE_1960: return toDaysSinceDate(Epochs.YEAR_1960, value); case DATE_DAYS_SINCE_1970: return toDaysSinceDate(Epochs.YEAR_1970, value); case DATE_DAYS_SINCE_1980: return toDaysSinceDate(Epochs.YEAR_1980, value); case TIME_SECONDS: return toSecondsSinceMidnight(value); case DATE_TIME_SECONDS_SINCE_0: throw new NotImplementedException(); case DATE_TIME_SECONDS_SINCE_1960: return toSecondsSinceDate(Epochs.YEAR_1960, value); case DATE_TIME_SECONDS_SINCE_1970: return toSecondsSinceDate(Epochs.YEAR_1970, value); case DATE_TIME_SECONDS_SINCE_1980: return toSecondsSinceDate(Epochs.YEAR_1980, value); default: throw new IllegalArgumentException(); } } private  TypeUtil(); static String format(Object value); static Object parseOrCast(DataType dataType, Object value); static Object parse(DataType dataType, String value); static DaysSinceDate parseDaysSinceDate(LocalDate epoch, String value); static boolean equals(DataType dataType, Object value, Object referenceValue); static DataType getDataType(Object value); static DataType getDataType(Collection<?> values); static DataType getCommonDataType(DataType left, DataType right); static OpType getOpType(DataType dataType); static Object cast(DataType dataType, Object value); static V cast(Class<? extends V> clazz, Object value); static DataType getConstantDataType(Object value); static DataType getConstantDataType(String value); }
TypeUtil { static public Object cast(DataType dataType, Object value){ switch(dataType){ case STRING: return toString(value); case INTEGER: return toInteger(value); case FLOAT: return toFloat(value); case DOUBLE: return toDouble(value); case BOOLEAN: return toBoolean(value); case DATE: return toDate(value); case TIME: return toTime(value); case DATE_TIME: return toDateTime(value); case DATE_DAYS_SINCE_0: throw new NotImplementedException(); case DATE_DAYS_SINCE_1960: return toDaysSinceDate(Epochs.YEAR_1960, value); case DATE_DAYS_SINCE_1970: return toDaysSinceDate(Epochs.YEAR_1970, value); case DATE_DAYS_SINCE_1980: return toDaysSinceDate(Epochs.YEAR_1980, value); case TIME_SECONDS: return toSecondsSinceMidnight(value); case DATE_TIME_SECONDS_SINCE_0: throw new NotImplementedException(); case DATE_TIME_SECONDS_SINCE_1960: return toSecondsSinceDate(Epochs.YEAR_1960, value); case DATE_TIME_SECONDS_SINCE_1970: return toSecondsSinceDate(Epochs.YEAR_1970, value); case DATE_TIME_SECONDS_SINCE_1980: return toSecondsSinceDate(Epochs.YEAR_1980, value); default: throw new IllegalArgumentException(); } } private  TypeUtil(); static String format(Object value); static Object parseOrCast(DataType dataType, Object value); static Object parse(DataType dataType, String value); static DaysSinceDate parseDaysSinceDate(LocalDate epoch, String value); static boolean equals(DataType dataType, Object value, Object referenceValue); static DataType getDataType(Object value); static DataType getDataType(Collection<?> values); static DataType getCommonDataType(DataType left, DataType right); static OpType getOpType(DataType dataType); static Object cast(DataType dataType, Object value); static V cast(Class<? extends V> clazz, Object value); static DataType getConstantDataType(Object value); static DataType getConstantDataType(String value);  }