@Test public void castByStringTest() { Object val = ClassCast.cast("1", Integer.class.getName()); assertEquals(1, val); val = ClassCast.cast("1", Long.class.getName()); assertEquals(1L, val); val = ClassCast.cast("1.0", Double.class.getName()); assertEquals(1D, val); val = ClassCast.cast("1.0", Float.class.getName()); assertEquals(1F, val); String now = "2015-01-01 12:00:00.000"; val = ClassCast.cast(String.valueOf(now), Timestamp.class.getName()); assertEquals(Timestamp.valueOf(now), val); Map<String, String> map = new HashMap<String, String>() { private static final long serialVersionUID = 1L; { put("id", "id0"); put("name", "name0"); } }; val = ClassCast.cast(JSON.toJSONString(map), UseEntity.class.getName()); assertEquals(UseEntity.class.getName(), val.getClass().getName()); assertEquals("id0", ((UseEntity) val).getId()); assertEquals("name0", ((UseEntity) val).getName()); }
public static final Object cast(final String value, final String typeName) { if (value == null) { return null; } try { switch (typeName) { case INTEGER_OBJ: case INT: return Integer.valueOf(value); case LONG_OBJ: case LONG: return Long.valueOf(value); case DOUBLE_OBJ: case DOUBLE: return Double.valueOf(value); case FLOAT_OBJ: case FLOAT: return Float.valueOf(value); case BOOLEAN_OBJ: case BOOLEAN: if (ObjectCompare.isInList(value.toUpperCase(), "1", "YES", "Y", "TRUE")) { return Boolean.TRUE; } else if (ObjectCompare.isInList(value.toUpperCase(), "0", "NO", "N", "FALSE")) { return Boolean.FALSE; } else { return Boolean.valueOf(value); } case STRING: return value; case DATE_UTIL: case DATE_SQL: return DateFormat.parse(value, Pattern.DATETIME); case TIMESTAMP: final long time = DateFormat.parse(value, Pattern.TIMESTAMP).getTime(); return new Timestamp(time); default: return JSON.parseObject(value, Class.forName(typeName)); } } catch (final ClassCastException | ClassNotFoundException | ParseException e) { throw new org.nanoframework.commons.exception.ClassCastException(e.getMessage(), e); } }
ClassCast { public static final Object cast(final String value, final String typeName) { if (value == null) { return null; } try { switch (typeName) { case INTEGER_OBJ: case INT: return Integer.valueOf(value); case LONG_OBJ: case LONG: return Long.valueOf(value); case DOUBLE_OBJ: case DOUBLE: return Double.valueOf(value); case FLOAT_OBJ: case FLOAT: return Float.valueOf(value); case BOOLEAN_OBJ: case BOOLEAN: if (ObjectCompare.isInList(value.toUpperCase(), "1", "YES", "Y", "TRUE")) { return Boolean.TRUE; } else if (ObjectCompare.isInList(value.toUpperCase(), "0", "NO", "N", "FALSE")) { return Boolean.FALSE; } else { return Boolean.valueOf(value); } case STRING: return value; case DATE_UTIL: case DATE_SQL: return DateFormat.parse(value, Pattern.DATETIME); case TIMESTAMP: final long time = DateFormat.parse(value, Pattern.TIMESTAMP).getTime(); return new Timestamp(time); default: return JSON.parseObject(value, Class.forName(typeName)); } } catch (final ClassCastException | ClassNotFoundException | ParseException e) { throw new org.nanoframework.commons.exception.ClassCastException(e.getMessage(), e); } } }
ClassCast { public static final Object cast(final String value, final String typeName) { if (value == null) { return null; } try { switch (typeName) { case INTEGER_OBJ: case INT: return Integer.valueOf(value); case LONG_OBJ: case LONG: return Long.valueOf(value); case DOUBLE_OBJ: case DOUBLE: return Double.valueOf(value); case FLOAT_OBJ: case FLOAT: return Float.valueOf(value); case BOOLEAN_OBJ: case BOOLEAN: if (ObjectCompare.isInList(value.toUpperCase(), "1", "YES", "Y", "TRUE")) { return Boolean.TRUE; } else if (ObjectCompare.isInList(value.toUpperCase(), "0", "NO", "N", "FALSE")) { return Boolean.FALSE; } else { return Boolean.valueOf(value); } case STRING: return value; case DATE_UTIL: case DATE_SQL: return DateFormat.parse(value, Pattern.DATETIME); case TIMESTAMP: final long time = DateFormat.parse(value, Pattern.TIMESTAMP).getTime(); return new Timestamp(time); default: return JSON.parseObject(value, Class.forName(typeName)); } } catch (final ClassCastException | ClassNotFoundException | ParseException e) { throw new org.nanoframework.commons.exception.ClassCastException(e.getMessage(), e); } } private  ClassCast(); }
ClassCast { public static final Object cast(final String value, final String typeName) { if (value == null) { return null; } try { switch (typeName) { case INTEGER_OBJ: case INT: return Integer.valueOf(value); case LONG_OBJ: case LONG: return Long.valueOf(value); case DOUBLE_OBJ: case DOUBLE: return Double.valueOf(value); case FLOAT_OBJ: case FLOAT: return Float.valueOf(value); case BOOLEAN_OBJ: case BOOLEAN: if (ObjectCompare.isInList(value.toUpperCase(), "1", "YES", "Y", "TRUE")) { return Boolean.TRUE; } else if (ObjectCompare.isInList(value.toUpperCase(), "0", "NO", "N", "FALSE")) { return Boolean.FALSE; } else { return Boolean.valueOf(value); } case STRING: return value; case DATE_UTIL: case DATE_SQL: return DateFormat.parse(value, Pattern.DATETIME); case TIMESTAMP: final long time = DateFormat.parse(value, Pattern.TIMESTAMP).getTime(); return new Timestamp(time); default: return JSON.parseObject(value, Class.forName(typeName)); } } catch (final ClassCastException | ClassNotFoundException | ParseException e) { throw new org.nanoframework.commons.exception.ClassCastException(e.getMessage(), e); } } private  ClassCast(); static final Object cast(final String value, final String typeName); static final Object cast(final Object value, final String typeName); static Date parseDate(final String date); }
ClassCast { public static final Object cast(final String value, final String typeName) { if (value == null) { return null; } try { switch (typeName) { case INTEGER_OBJ: case INT: return Integer.valueOf(value); case LONG_OBJ: case LONG: return Long.valueOf(value); case DOUBLE_OBJ: case DOUBLE: return Double.valueOf(value); case FLOAT_OBJ: case FLOAT: return Float.valueOf(value); case BOOLEAN_OBJ: case BOOLEAN: if (ObjectCompare.isInList(value.toUpperCase(), "1", "YES", "Y", "TRUE")) { return Boolean.TRUE; } else if (ObjectCompare.isInList(value.toUpperCase(), "0", "NO", "N", "FALSE")) { return Boolean.FALSE; } else { return Boolean.valueOf(value); } case STRING: return value; case DATE_UTIL: case DATE_SQL: return DateFormat.parse(value, Pattern.DATETIME); case TIMESTAMP: final long time = DateFormat.parse(value, Pattern.TIMESTAMP).getTime(); return new Timestamp(time); default: return JSON.parseObject(value, Class.forName(typeName)); } } catch (final ClassCastException | ClassNotFoundException | ParseException e) { throw new org.nanoframework.commons.exception.ClassCastException(e.getMessage(), e); } } private  ClassCast(); static final Object cast(final String value, final String typeName); static final Object cast(final Object value, final String typeName); static Date parseDate(final String date);  }