Java Utililty Methods Object Type Case

List of utility methods to do Object Type Case

Description

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

Method

Tcast(Class type, Object key, Object value)
cast
if (value == null) {
    return null;
if (!type.isAssignableFrom(value.getClass())) {
    throw new Exception("Value has invalid type" + (key == null ? "" : " for key: " + key)
            + " -- expected '" + type.getName() + "', got: " + value.getClass().getName());
return (T) value;
...
Tcast(Class type, Object obj)
Cast an object type to the specified type.
return (type.isInstance(obj)) ? type.cast(obj) : null;
int[]cast(double[] arr)
cast double[] to int[]
int[] carr = new int[arr.length];
for (int i = 0; i < arr.length; i++) {
    carr[i] = (int) arr[i];
return carr;
Classcast(final Class clazz)
Casts a class to a required type
return (Class<T>) clazz;
Rcast(final Number number, final Class returnType)
Casts the number to the provided return type.
if ((number == null) || (returnType == null)) {
    return null;
if (returnType == Double.class) {
    final Double value = number.doubleValue();
    return returnType.cast(value);
} else if (returnType == Float.class) {
    final Float value = number.floatValue();
...
Tcast(final Object o)
Utility method to perform generic casts without dealing with Unchecked cast warnings (or having to apply SuppressWarnings("unchecked") annotations).
return (T) o;
Tcast(final Object obj, final Class type)
Casts the given object to the specified type, or null if the types are incompatible.
if (!canCast(obj, type))
    return null;
@SuppressWarnings("unchecked")
final T result = (T) obj;
return result;
Tcast(final Object object)
Auto cast an object.
return (T) object;
Tcast(final Object original)
Casting in the way that it is not a problem for findbugs yet it can throw a classcast exception.
@SuppressWarnings("unchecked")
T result = (T) original;
return result;
shortcast(int value)
cast
return value > Short.MAX_VALUE ? Short.MAX_VALUE
        : value < Short.MIN_VALUE ? Short.MIN_VALUE : (short) value;