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);  }
@Test public void cast(){ assertEquals(1, TypeUtil.cast(DataType.INTEGER, (byte)1)); assertEquals(1, TypeUtil.cast(DataType.INTEGER, (short)1)); assertEquals(1, TypeUtil.cast(DataType.INTEGER, 1)); assertEquals(1, TypeUtil.cast(DataType.INTEGER, 1l)); assertEquals(1, TypeUtil.cast(DataType.INTEGER, true)); assertEquals(1f, TypeUtil.cast(DataType.FLOAT, (byte)1)); assertEquals(1f, TypeUtil.cast(DataType.FLOAT, (short)1)); assertEquals(1f, TypeUtil.cast(DataType.FLOAT, 1)); assertEquals(1f, TypeUtil.cast(DataType.FLOAT, 1l)); assertEquals(1f, TypeUtil.cast(DataType.FLOAT, 1f)); assertEquals(1f, TypeUtil.cast(DataType.FLOAT, true)); assertEquals(1d, TypeUtil.cast(DataType.DOUBLE, (byte)1)); assertEquals(1d, TypeUtil.cast(DataType.DOUBLE, (short)1)); assertEquals(1d, TypeUtil.cast(DataType.DOUBLE, 1)); assertEquals(1d, TypeUtil.cast(DataType.DOUBLE, 1l)); assertEquals(1d, TypeUtil.cast(DataType.DOUBLE, 1f)); assertEquals(1d, TypeUtil.cast(DataType.DOUBLE, 1d)); assertEquals(1d, TypeUtil.cast(DataType.DOUBLE, true)); }
@Test public void castInteger(){ try { TypeUtil.cast(DataType.INTEGER, Long.valueOf(Integer.MIN_VALUE - 1l)); fail(); } catch(TypeCheckException tce){ } try { TypeUtil.cast(DataType.INTEGER, Long.valueOf(Integer.MAX_VALUE + 1l)); fail(); } catch(TypeCheckException tce){ } assertEquals(1, TypeUtil.cast(DataType.INTEGER, 1f)); assertEquals(1, TypeUtil.cast(DataType.INTEGER, 1.0f)); try { TypeUtil.cast(DataType.INTEGER, Math.nextUp(1f)); fail(); } catch(TypeCheckException tce){ } assertEquals(1, TypeUtil.cast(DataType.INTEGER, 1d)); assertEquals(1, TypeUtil.cast(DataType.INTEGER, 1.0d)); try { TypeUtil.cast(DataType.INTEGER, Math.nextUp(1d)); fail(); } catch(TypeCheckException tce){ } try { TypeUtil.cast(DataType.INTEGER, Double.valueOf(Integer.MIN_VALUE - 1d)); fail(); } catch(TypeCheckException tce){ } try { TypeUtil.cast(DataType.INTEGER, Double.valueOf(Integer.MAX_VALUE + 1d)); fail(); } catch(TypeCheckException tce){ } }
@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){ } }