Java Utililty Methods Type Coerce

List of utility methods to do Type Coerce

Description

The list of methods to do Type Coerce are organized into topic(s).

Method

Classcoerce(final Class klass)
coerce
@SuppressWarnings("unchecked")
final Class<T> result = (Class<T>) klass;
return result;
Objectcoerce(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();
...
Objectcoerce(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);
...
Objectcoerce(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);
...
Tcoerce(Object value, Class clazz)
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;
Tcoerce(Object value, Class clazz)
coerce
return coerce(value, clazz, null);
booleancoerceBool(Object obj)
coerce Bool
return !(obj == null || obj.equals(false));
booleancoerceBool(Object val)
Coerces any value to boolean.
return !(val == null || val.equals(false));
longcoerceIntoComboId(Long entityId)
Coerces numeric entity ids into ids which correctly sorts into a jeditable combo.
if (entityId == null) {
    return 0;
return entityId + ID_OFFSET;
LongcoerceIntoEntityId(Long comboId)
Inverse operation of #coerceIntoComboId(Long)
if (comboId == null || comboId == 0) {
    return null;
return comboId - ID_OFFSET;