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

TcastAs(Class clazz, Object obj)
cast As
if (obj == null) {
    return null;
try {
    if (clazz.isInstance(obj)) {
        return clazz.cast(obj);
} catch (ClassCastException ex) {
...
double[]castAsDoubleArray(int[] input)
cast As Double Array
double[] output;
output = new double[input.length];
for (int i = 0; i < input.length; i++) {
    output[i] = (double) input[i];
return output;
ClasscastClass(Class aClass)
cast Class
return (Class<T>) aClass;
UcastClone(U obj, T superClone)
cast Clone
@SuppressWarnings("unchecked")
U clone = (U) superClone;
return clone;
TcastEnum(Enum enum_param)
This method just is a workaround such that maven can compile and do not proclaim when an enum is directly casted!
return (T) enum_param;
UcastEnum(T from, U[] to)
cast Enum
return to[from.ordinal()];
StringcastForRegex(String textContent)
cast For Regex
return textContent.replace("\\", "\\\\").replace("/", "\\/").replace("*", "\\*").replace("+", "\\+")
        .replace(".", "\\.").replace("?", "\\?").replace(")", "\\)").replace("{", "\\{").replace("}", "\\}")
        .replace("(", "\\(").replace("[", "\\[").replace("]", "\\]").replace("%", "\\%");
StringcastingNum(double n)
utility
if (Math.ceil(n) == Math.floor(n)) {
    return ("" + (int) n);
} else {
    return "" + n;
ObjectcastingUtil(String value, Class classCasting)
casting Util
Object object = value;
if (Number.class.isAssignableFrom(classCasting)) {
    if (classCasting.isAssignableFrom(Double.class)) {
        return Double.valueOf(value);
    } else if (classCasting.isAssignableFrom(Long.class)) {
        return Long.valueOf(value);
    } else if (classCasting.isAssignableFrom(Float.class)) {
        return Float.valueOf(value);
...
TcastInstance(Object o, Class clazz)
cast Instance
try {
    return clazz.cast(o);
} catch (ClassCastException e) {
    throw new IllegalArgumentException(e);