public static boolean isCase(Object switchValue, Object caseExpression) throws Throwable { if (caseExpression == null) { return switchValue == null; } return DefaultTypeTransformation.castToBoolean(invokeMethodN(caseExpression.getClass(), caseExpression, "isCase", new Object[]{switchValue})); }
public static boolean equals(Object obj, Object other) { return DefaultTypeTransformation.compareEqual(obj, other); }
private static Object adjustNewValue(Object[] objects, Object newValue) { Class arrayComponentClass = objects.getClass().getComponentType(); Object adjustedNewVal = newValue; if (newValue instanceof Number) { if (!arrayComponentClass.equals(newValue.getClass())) { adjustedNewVal = DefaultTypeTransformation.castToType(newValue, arrayComponentClass); } } else if (Character.class.isAssignableFrom(arrayComponentClass)) { adjustedNewVal = DefaultTypeTransformation.getCharFromSizeOneString(newValue); } else if (Number.class.isAssignableFrom(arrayComponentClass)) { if (newValue instanceof Character || newValue instanceof String || newValue instanceof GString) { Character ch = DefaultTypeTransformation.getCharFromSizeOneString(newValue); adjustedNewVal = DefaultTypeTransformation.castToType(ch, arrayComponentClass); } } else if (arrayComponentClass.isArray()) { adjustedNewVal = DefaultTypeTransformation.castToType(newValue, arrayComponentClass); } return adjustedNewVal; }
public static Collection arrayAsCollection(Object value) { if (value.getClass().getComponentType().isPrimitive()) { return primitiveArrayToList(value); } return arrayAsCollection((Object[]) value); }
private static Object castToPrimitive(Object object, Class type) { if (type == boolean.class) { return booleanUnbox(object); } else if (type == byte.class) { return byteUnbox(object); } else if (type == char.class) { return charUnbox(object); } else if (type == short.class) { return shortUnbox(object); } else if (type == int.class) { return intUnbox(object); } else if (type == long.class) { return longUnbox(object); } else if (type == float.class) { return floatUnbox(object); } else if (type == double.class) { Double answer = doubleUnbox(object); //throw a runtime exception if conversion would be out-of-range for the type. if (!(object instanceof Double) && (answer == Double.NEGATIVE_INFINITY || answer == Double.POSITIVE_INFINITY)) { throw new GroovyRuntimeException("Automatic coercion of " + object.getClass().getName() + " value " + object + " to double failed. Value is out of range."); } return answer; } //nothing else possible throw new GroovyCastException(object, type); }
return asArray(object, type); return object.toString(); } else if (type == Character.class) { return box(castToChar(object)); } else if (type == Boolean.class) { return box(castToBoolean(object)); } else if (type == Class.class) { return castToClass(object); } else if (Number.class.isAssignableFrom(type)) { Number n = castToNumber(object); if (type == Byte.class) { return new Byte(n.byteValue()); return box(booleanUnbox(object)); } else if (type == byte.class) { return box(byteUnbox(object)); } else if (type == char.class) { return box(charUnbox(object)); } else if (type == short.class) { return box(shortUnbox(object)); } else if (type == int.class) { return box(intUnbox(object)); } else if (type == long.class) { return box(longUnbox(object)); } else if (type == float.class) { return box(floatUnbox(object)); } else if (type == double.class) { Double answer = new Double(doubleUnbox(object));
public static boolean truth(Object o) { if (DefaultTypeTransformation.booleanUnbox(o)) return true; return false; }
public static Object castToType(Object object, Class type) { if (object == null) return null; if (type == Object.class) return object; final Class aClass = object.getClass(); if (type == aClass) return object; if (type.isAssignableFrom(aClass)) return object; if (ReflectionCache.isArray(type)) return asArray(object, type); if (type.isEnum()) { return ShortTypeHandling.castToEnum(object, type); } else if (Collection.class.isAssignableFrom(type)) { return continueCastOnCollection(object, type); } else if (type == String.class) { return InvokerHelper.toString(object); } else if (type == Character.class) { return ShortTypeHandling.castToChar(object); } else if (type == Boolean.class) { return castToBoolean(object); } else if (type == Class.class) { return ShortTypeHandling.castToClass(object); } else if (type.isPrimitive()) { return castToPrimitive(object, type); } return continueCastOnNumber(object, type); }
@Override public final double evaluate(final ScalingPolicyEvaluationContext context) { this.context.set(context); try{ return DefaultTypeTransformation.doubleUnbox(run()); } finally { this.context.remove(); } }
public static boolean isTruthy(Object obj) { return DefaultTypeTransformation.castToBoolean(obj); }
@SuppressWarnings("unchecked") public static <T> T coerce(Object obj, Class<T> type) { // can't use `type.cast()` because it will fail if `type` is a primitive type return (T) DefaultTypeTransformation.castToType(obj, type); }
/** * Attempts to create an Iterator for the given object by first * converting it to a Collection. * * @param o an object * @return an Iterator for the given Object. * @see org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation#asCollection(java.lang.Object) * @since 1.0 */ public static Iterator iterator(Object o) { return DefaultTypeTransformation.asCollection(o).iterator(); }
public static Object asArray(Object object, Class type) { if (type.isAssignableFrom(object.getClass())) { return object; } Collection list = asCollection(object); int size = list.size(); Class elementType = type.getComponentType(); Object array = Array.newInstance(elementType, size); int idx = 0; for (Iterator iter = list.iterator(); iter.hasNext(); idx++) { Object element = iter.next(); Array.set(array, idx, castToType(element, elementType)); } return array; }
/** * Provides a hook for type casting of the given object to the required type * * @param type of object to convert the given object to * @param object the object to be converted * @return the original object or a new converted value * @throws Throwable if the type casting fails */ public static Object castToType(Object object, Class type) throws Throwable { try { return DefaultTypeTransformation.castToType(object, type); } catch (GroovyRuntimeException gre) { throw unwrap(gre); } }
public static <T> Collection<T> asCollection(T[] value) { return arrayAsCollection(value); }
return Array.newInstance(clazz.getComponentType(), 0); return DefaultTypeTransformation.asArray(delegate, clazz);
Collection list = asCollection(object); int size = list.size(); Class elementType = type.getComponentType(); for (Iterator iter = list.iterator(); iter.hasNext(); idx++) { Object element = iter.next(); Array.setBoolean(array, idx, booleanUnbox(element)); for (Iterator iter = list.iterator(); iter.hasNext(); idx++) { Object element = iter.next(); Array.setByte(array, idx, byteUnbox(element)); for (Iterator iter = list.iterator(); iter.hasNext(); idx++) { Object element = iter.next(); Array.setChar(array, idx, charUnbox(element)); for (Iterator iter = list.iterator(); iter.hasNext(); idx++) { Object element = iter.next(); Array.setDouble(array, idx, doubleUnbox(element)); for (Iterator iter = list.iterator(); iter.hasNext(); idx++) { Object element = iter.next(); Array.setFloat(array, idx, floatUnbox(element)); for (Iterator iter = list.iterator(); iter.hasNext(); idx++) { Object element = iter.next(); Array.setInt(array, idx, intUnbox(element)); for (Iterator iter = list.iterator(); iter.hasNext(); idx++) {
return asArray(object, type); return object.toString(); } else if (type == Character.class) { return box(castToChar(object)); } else if (type == Boolean.class) { return box(castToBoolean(object)); } else if (type == Class.class) { return castToClass(object); } else if (Number.class.isAssignableFrom(type)) { Number n = castToNumber(object, type); if (type == Byte.class) { return new Byte(n.byteValue()); return box(booleanUnbox(object)); } else if (type == byte.class) { return box(byteUnbox(object)); } else if (type == char.class) { return box(charUnbox(object)); } else if (type == short.class) { return box(shortUnbox(object)); } else if (type == int.class) { return box(intUnbox(object)); } else if (type == long.class) { return box(longUnbox(object)); } else if (type == float.class) { return box(floatUnbox(object)); } else if (type == double.class) { Double answer = new Double(doubleUnbox(object));
public Next receive(Object o) { boolean b = DefaultTypeTransformation.booleanUnbox(o); return k.receive(!b); } });
public static boolean booleanUnbox(Object value) { return castToBoolean(value); }