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

longcastInttoLong(int x)
cast Intto Long
return x & 0x00000000ffffffffL;
TcastNode(Class jsonNode, Class classz)
cast Node
if (classz.isAssignableFrom(jsonNode)) {
    return (T) classz;
throw new ClassCastException("can not cast " + classz + " to " + jsonNode);
T[]castNonNullArray( T [] arr)
cast Non Null Array
assert arr != null : "Misuse of castNonNullArray: called with a null array argument";
for (int i = 0; i < arr.length; ++i) {
    assert arr[i] != null : "Misuse of castNonNull: called with a null array element";
    checkIfArray(arr[i]);
return ( T[]) arr;
RcastNullableParameterTo(String parameterName, T value, Class requiredType)
cast Nullable Parameter To
final R result;
if (value != null) {
    if (!requiredType.isInstance(value)) {
        throw new IllegalArgumentException(
                parameterName + " is not of type " + requiredType.getName() + ".");
    } else {
        result = requiredType.cast(value);
} else {
    result = null;
return result;
TcastNumber(Number num, Class clazz)
Casts provided number value to desired number type.
Number cNum = 0;
if (clazz.isAssignableFrom(Long.class)) {
    cNum = num.longValue();
} else if (clazz.isAssignableFrom(Integer.class)) {
    cNum = num.intValue();
} else if (clazz.isAssignableFrom(Byte.class)) {
    cNum = num.byteValue();
} else if (clazz.isAssignableFrom(Float.class)) {
...
TcastObject(Class clazz, Object object)
cast Object
return clazz.cast(object);
booleancastObject(Object fromObj, Class toClass)
castObject returns boolean value indicating whether the value need to be cast
if (toClass.equals(fromObj.getClass())) {
    return false;
} else if (toClass.isPrimitive()) {
    if (toClass.equals(long.class) && fromObj.getClass().equals(Long.class)) {
        return false;
    } else if (toClass.equals(int.class) && fromObj.getClass().equals(Integer.class)) {
        return false;
    } else if (toClass.equals(double.class) && fromObj.getClass().equals(Double.class)) {
...
TcastObject(Object ob)
Reads an object from an input and converts it to the appropriate type.
return (T) ob;
TcastOrDefault(Class type, Object value, T defaultValue, boolean allowNull)
Casts a value to another type, returning a default value if the cast is not possible or if the value is null and null values are not allowed.
if (value == null)
    return allowNull ? null : defaultValue;
if (!type.isAssignableFrom(value.getClass())) {
    return defaultValue;
return (T) value;
TcastOrDefault(Object o, Class type, T def)
cast Or Default
return o == null || !type.isInstance(o) ? def : (T) o;