} else if (type == Double.class) {
Double answer = new Double(n.doubleValue());
//throw a runtime exception if conversion would be out-of-range for the type.
if (!(n instanceof Double) && (answer.doubleValue() == Double.NEGATIVE_INFINITY
|| answer.doubleValue() == Double.POSITIVE_INFINITY)) {
throw new GroovyRuntimeException("Automatic coercion of " + n.getClass().getName()
+ " value " + n + " to double failed. Value is out of range.");
}
return answer;
} else if (type == BigDecimal.class) {
return new BigDecimal(n.toString());
} else if (type == BigInteger.class) {
if (object instanceof Float || object instanceof Double) {
BigDecimal bd = new BigDecimal(n.doubleValue());
return bd.toBigInteger();
} else if (object instanceof BigDecimal) {
return ((BigDecimal) object).toBigInteger();
} else {
return new BigInteger(n.toString());
}
}
} else if (type.isPrimitive()) {
if (type == boolean.class) {
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));
//throw a runtime exception if conversion would be out-of-range for the type.
if (!(object instanceof Double) && (answer.doubleValue() == Double.NEGATIVE_INFINITY
|| answer.doubleValue() == Double.POSITIVE_INFINITY)) {
throw new GroovyRuntimeException("Automatic coercion of " + aClass.getName()
+ " value " + object + " to double failed. Value is out of range.");
}
return answer;
}
}