}
// Byte
if (targetType.equals(Byte.class)) {
long longValue = value.longValue();
if (longValue > Byte.MAX_VALUE) {
throw new ConversionException(this.toString(sourceType) + " value '" + value + "' is too large for " + this.toString(targetType));
}
if (longValue < Byte.MIN_VALUE) {
throw new ConversionException(this.toString(sourceType) + " value '" + value + "' is too small " + this.toString(targetType));
}
return new Byte(value.byteValue());
}
// Short
if (targetType.equals(Short.class)) {
long longValue = value.longValue();
if (longValue > Short.MAX_VALUE) {
throw new ConversionException(this.toString(sourceType) + " value '" + value + "' is too large for " + this.toString(targetType));
}
if (longValue < Short.MIN_VALUE) {
throw new ConversionException(this.toString(sourceType) + " value '" + value + "' is too small " + this.toString(targetType));
}
return new Short(value.shortValue());
}
// Integer
if (targetType.equals(Integer.class)) {
long longValue = value.longValue();
if (longValue > Integer.MAX_VALUE) {
throw new ConversionException(this.toString(sourceType) + " value '" + value + "' is too large for " + this.toString(targetType));
}
if (longValue < Integer.MIN_VALUE) {
throw new ConversionException(this.toString(sourceType) + " value '" + value + "' is too small " + this.toString(targetType));
}
return new Integer(value.intValue());
}
// Long
if (targetType.equals(Long.class)) {
return new Long(value.longValue());
}
// Float
if (targetType.equals(Float.class)) {
if (value.doubleValue() > Float.MAX_VALUE) {
throw new ConversionException(this.toString(sourceType) + " value '" + value + "' is too large for " + this.toString(targetType));
}
return new Float(value.floatValue());
}
// Double
if (targetType.equals(Double.class)) {
return new Double(value.doubleValue());
}
// BigDecimal
if (targetType.equals(BigDecimal.class)) {
if (value instanceof Float || value instanceof Double) {
return new BigDecimal(value.toString());
} else if (value instanceof BigInteger) {
return new BigDecimal((BigInteger) value);
} else {
return BigDecimal.valueOf(value.longValue());
}
}
// BigInteger
if (targetType.equals(BigInteger.class)) {
if (value instanceof BigDecimal) {
return ((BigDecimal) value).toBigInteger();
} else {
return BigInteger.valueOf(value.longValue());
}
}
String msg = this.toString(this.getClass()) + " cannot handle conversion to '" + this.toString(targetType) + "'";
throw new ConversionException(msg);
}