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

CcastOrNull(Object o, Class clazz)
cast Or Null
if (clazz.isAssignableFrom(o.getClass())) {
    return (C) o;
} else {
    return null;
TcastOrNull(Object o, Class cls)
Cast object to class or return null.
if (o == null)
    return null;
if (cls.isInstance(o))
    return cls.cast(o);
return null;
T2castOrThrowError(Class clazz, T1 object, String messageTemplate)
Attempts to cast an object to a given class, and if that does not succeed, throws an error with a message generated from a template following the format conventions of String#format(String,Object) , with formatting arguments being the object being cast, and the expected and actual received classes' simple names, in this order.
T2 result;
try {
    result = clazz.cast(object);
} catch (ClassCastException e) {
    String message = String.format(messageTemplate, object, clazz.getSimpleName(),
            object.getClass().getSimpleName());
    throw new Error(message);
return result;
TcastSafe(Class inClass, Object inValue, T inDefault)
cast Safe
if (inClass == null) {
    throw new IllegalArgumentException("no target class specified");
} else if (inValue != null && !inClass.isAssignableFrom(inValue.getClass())) {
    return inDefault;
try {
    return (T) inValue;
} catch (Exception e) {
...
Stringcaststring1(String tmpq)
caststring
String tmp1 = "";
if (tmpq.indexOf("-") <= 0) {
    int tt = tmpq.indexOf(".");
    if (tt > 0)
        tmp1 = tmpq.substring(0, tt);
    else
        tmp1 = tmpq;
} else {
...
TcastTo(Object o, Class cls)
cast To
Class<? extends Object> oc = o.getClass();
if (cls.isAssignableFrom(oc))
    return (T) o;
System.out.println("ReflectionUtils.castTo() -- returning null");
return null;
Object[]castToArray(Object object)
cast To Array
Object[] array = null;
if (object != null) {
    if (object instanceof String) {
        array = EMPTY_ARRAY;
    } else {
        array = (Object[]) object;
return array;
NumbercastToClass_strict(Number valueIn, Class toClassIn)
Method for intelligently converting between Number classes.
if (valueIn == null)
    return null;
if (valueIn.getClass() == toClassIn)
    return valueIn;
boolean hasFractionalComponent = (Math.floor(valueIn.doubleValue()) != valueIn.doubleValue());
if (hasFractionalComponent && (toClassIn.equals(Byte.class) || toClassIn.equals(Short.class)
        || toClassIn.equals(Integer.class) || toClassIn.equals(Long.class)))
    return null;
...
FloatcastToObject(Object value)
cast To Object
return (Float) value;
ObjectcastToPrimitive(Object value, String targetType)
cast To Primitive
if (value instanceof Number) {
    Number castValue = (Number) value;
    if ("B".equals(targetType) || "Ljava/lang/Byte;".equals(targetType)) {
        return castValue.byteValue();
    } else if ("D".equals(targetType) || "Ljava/lang/Double;".equals(targetType)) {
        return castValue.doubleValue();
    } else if ("F".equals(targetType) || "Ljava/lang/Float;".equals(targetType)) {
        return castValue.floatValue();
...