List of utility methods to do Type Coerce
Class | coerce(final Class> klass) coerce @SuppressWarnings("unchecked") final Class<T> result = (Class<T>) klass; return result; |
Object | coerce(Object obj, Class> c) coerce if (obj == null || c == null) { return null; if (obj.getClass() == c) { return obj; if (c == Long.class) { return (obj instanceof String) ? Long.parseLong(obj.toString().trim()) : ((Number) obj).longValue(); ... |
Object | coerce(Object val, Class clz) coerce if (val == null) return val; if (val.getClass() == clz) return val; if (val instanceof String && (clz == Double.class || clz == double.class)) return convertDouble((String) val); if (val instanceof String && (clz == Integer.class || clz == int.class)) return convertInteger((String) val); ... |
Object | coerce(Object val, Class> c) Tries to coerce a value to a specific (simple) data type. if (val == null || c == null) { return null; } else if (val.getClass() == c) { return val; } else if (c == String.class) { return castString(val); } else if (c == Boolean.class) { return coerceBool(val); ... |
T | coerce(Object value, Class coerce if (clazz.isAssignableFrom(value.getClass())) { return (T) value; if (value instanceof String) { String str = (String) value; if (clazz.equals(Double.class)) { return (T) Double.valueOf(Double.parseDouble(str)); if (clazz.equals(Float.class)) { return (T) Float.valueOf(Float.parseFloat(str)); if (clazz.equals(Long.class)) { return (T) Long.valueOf(Long.parseLong(str)); if (clazz.equals(Integer.class)) { return (T) Integer.valueOf(Integer.parseInt(str)); if (clazz.equals(Boolean.class)) { return (T) Boolean.valueOf(Boolean.parseBoolean(str)); } else { throw new IllegalArgumentException( "Cannot convert value of " + value.getClass() + " to class = " + clazz.getName()); return (T) value; |
T | coerce(Object value, Class coerce return coerce(value, clazz, null);
|
boolean | coerceBool(Object obj) coerce Bool return !(obj == null || obj.equals(false));
|
boolean | coerceBool(Object val) Coerces any value to boolean. return !(val == null || val.equals(false)); |
long | coerceIntoComboId(Long entityId) Coerces numeric entity ids into ids which correctly sorts into a jeditable combo. if (entityId == null) { return 0; return entityId + ID_OFFSET; |
Long | coerceIntoEntityId(Long comboId) Inverse operation of #coerceIntoComboId(Long) if (comboId == null || comboId == 0) { return null; return comboId - ID_OFFSET; |