List of utility methods to do Object Type Case
C | castOrNull(Object o, Class cast Or Null if (clazz.isAssignableFrom(o.getClass())) { return (C) o; } else { return null; |
T | castOrNull(Object o, Class Cast object to class or return null. if (o == null) return null; if (cls.isInstance(o)) return cls.cast(o); return null; |
T2 | castOrThrowError(Class 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; |
T | castSafe(Class 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) { ... |
String | caststring1(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 { ... |
T | castTo(Object o, Class 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; |
Number | castToClass_strict(Number valueIn, Class extends Number> 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; ... |
Float | castToObject(Object value) cast To Object return (Float) value;
|
Object | castToPrimitive(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(); ... |