@Test public void testConvertNumberToTargetClass() throws Exception { assertEquals(new Integer(1), NumberUtils.convertNumberToTargetClass(1.1, Integer.class)); assertEquals(new Byte("1"), NumberUtils.convertNumberToTargetClass(1.1, Byte.class)); assertEquals(new Float(1.1), NumberUtils.convertNumberToTargetClass(1.1, Float.class)); assertEquals(new Double(1.1), NumberUtils.convertNumberToTargetClass(1.1, Double.class)); assertEquals(new Long(1), NumberUtils.convertNumberToTargetClass(1.1, Long.class)); assertEquals(new Short("1"), NumberUtils.convertNumberToTargetClass(1.1, Short.class)); assertEquals(new BigInteger("1"), NumberUtils.convertNumberToTargetClass(1, BigInteger.class)); }
@SuppressWarnings("unchecked") public static <T extends Number> T convertNumberToTargetClass(Number number, Class<T> targetClass) throws IllegalArgumentException { if (targetClass.isInstance(number)) { return (T) number; } else if (Byte.class == targetClass) { long value = checkedLongValue(number, targetClass); if (value < Byte.MIN_VALUE || value > Byte.MAX_VALUE) { raiseOverflowException(number, targetClass); } return (T) Byte.valueOf(number.byteValue()); } else if (Short.class == targetClass) { long value = checkedLongValue(number, targetClass); if (value < Short.MIN_VALUE || value > Short.MAX_VALUE) { raiseOverflowException(number, targetClass); } return (T) Short.valueOf(number.shortValue()); } else if (Integer.class == targetClass) { long value = checkedLongValue(number, targetClass); if (value < Integer.MIN_VALUE || value > Integer.MAX_VALUE) { raiseOverflowException(number, targetClass); } return (T) Integer.valueOf(number.intValue()); } else if (Long.class == targetClass) { long value = checkedLongValue(number, targetClass); return (T) Long.valueOf(value); } else if (BigInteger.class == targetClass) { if (number instanceof BigDecimal) { return (T) ((BigDecimal) number).toBigInteger(); } else { return (T) BigInteger.valueOf(number.longValue()); } } else if (Float.class == targetClass) { return (T) Float.valueOf(number.floatValue()); } else if (Double.class == targetClass) { return (T) Double.valueOf(number.doubleValue()); } else if (BigDecimal.class == targetClass) { return (T) new BigDecimal(number.toString()); } else if (AtomicInteger.class == targetClass) { return (T) new AtomicInteger(number.intValue()); } else if (AtomicLong.class == targetClass) { return (T) new AtomicLong(number.longValue()); } else { throw new IllegalArgumentException("Could not convert number [" + number + "] of type [" + number.getClass().getName() + "] to unsupported target class [" + targetClass.getName() + "]"); } }
NumberUtils { @SuppressWarnings("unchecked") public static <T extends Number> T convertNumberToTargetClass(Number number, Class<T> targetClass) throws IllegalArgumentException { if (targetClass.isInstance(number)) { return (T) number; } else if (Byte.class == targetClass) { long value = checkedLongValue(number, targetClass); if (value < Byte.MIN_VALUE || value > Byte.MAX_VALUE) { raiseOverflowException(number, targetClass); } return (T) Byte.valueOf(number.byteValue()); } else if (Short.class == targetClass) { long value = checkedLongValue(number, targetClass); if (value < Short.MIN_VALUE || value > Short.MAX_VALUE) { raiseOverflowException(number, targetClass); } return (T) Short.valueOf(number.shortValue()); } else if (Integer.class == targetClass) { long value = checkedLongValue(number, targetClass); if (value < Integer.MIN_VALUE || value > Integer.MAX_VALUE) { raiseOverflowException(number, targetClass); } return (T) Integer.valueOf(number.intValue()); } else if (Long.class == targetClass) { long value = checkedLongValue(number, targetClass); return (T) Long.valueOf(value); } else if (BigInteger.class == targetClass) { if (number instanceof BigDecimal) { return (T) ((BigDecimal) number).toBigInteger(); } else { return (T) BigInteger.valueOf(number.longValue()); } } else if (Float.class == targetClass) { return (T) Float.valueOf(number.floatValue()); } else if (Double.class == targetClass) { return (T) Double.valueOf(number.doubleValue()); } else if (BigDecimal.class == targetClass) { return (T) new BigDecimal(number.toString()); } else if (AtomicInteger.class == targetClass) { return (T) new AtomicInteger(number.intValue()); } else if (AtomicLong.class == targetClass) { return (T) new AtomicLong(number.longValue()); } else { throw new IllegalArgumentException("Could not convert number [" + number + "] of type [" + number.getClass().getName() + "] to unsupported target class [" + targetClass.getName() + "]"); } } }
NumberUtils { @SuppressWarnings("unchecked") public static <T extends Number> T convertNumberToTargetClass(Number number, Class<T> targetClass) throws IllegalArgumentException { if (targetClass.isInstance(number)) { return (T) number; } else if (Byte.class == targetClass) { long value = checkedLongValue(number, targetClass); if (value < Byte.MIN_VALUE || value > Byte.MAX_VALUE) { raiseOverflowException(number, targetClass); } return (T) Byte.valueOf(number.byteValue()); } else if (Short.class == targetClass) { long value = checkedLongValue(number, targetClass); if (value < Short.MIN_VALUE || value > Short.MAX_VALUE) { raiseOverflowException(number, targetClass); } return (T) Short.valueOf(number.shortValue()); } else if (Integer.class == targetClass) { long value = checkedLongValue(number, targetClass); if (value < Integer.MIN_VALUE || value > Integer.MAX_VALUE) { raiseOverflowException(number, targetClass); } return (T) Integer.valueOf(number.intValue()); } else if (Long.class == targetClass) { long value = checkedLongValue(number, targetClass); return (T) Long.valueOf(value); } else if (BigInteger.class == targetClass) { if (number instanceof BigDecimal) { return (T) ((BigDecimal) number).toBigInteger(); } else { return (T) BigInteger.valueOf(number.longValue()); } } else if (Float.class == targetClass) { return (T) Float.valueOf(number.floatValue()); } else if (Double.class == targetClass) { return (T) Double.valueOf(number.doubleValue()); } else if (BigDecimal.class == targetClass) { return (T) new BigDecimal(number.toString()); } else if (AtomicInteger.class == targetClass) { return (T) new AtomicInteger(number.intValue()); } else if (AtomicLong.class == targetClass) { return (T) new AtomicLong(number.longValue()); } else { throw new IllegalArgumentException("Could not convert number [" + number + "] of type [" + number.getClass().getName() + "] to unsupported target class [" + targetClass.getName() + "]"); } }  }
NumberUtils { @SuppressWarnings("unchecked") public static <T extends Number> T convertNumberToTargetClass(Number number, Class<T> targetClass) throws IllegalArgumentException { if (targetClass.isInstance(number)) { return (T) number; } else if (Byte.class == targetClass) { long value = checkedLongValue(number, targetClass); if (value < Byte.MIN_VALUE || value > Byte.MAX_VALUE) { raiseOverflowException(number, targetClass); } return (T) Byte.valueOf(number.byteValue()); } else if (Short.class == targetClass) { long value = checkedLongValue(number, targetClass); if (value < Short.MIN_VALUE || value > Short.MAX_VALUE) { raiseOverflowException(number, targetClass); } return (T) Short.valueOf(number.shortValue()); } else if (Integer.class == targetClass) { long value = checkedLongValue(number, targetClass); if (value < Integer.MIN_VALUE || value > Integer.MAX_VALUE) { raiseOverflowException(number, targetClass); } return (T) Integer.valueOf(number.intValue()); } else if (Long.class == targetClass) { long value = checkedLongValue(number, targetClass); return (T) Long.valueOf(value); } else if (BigInteger.class == targetClass) { if (number instanceof BigDecimal) { return (T) ((BigDecimal) number).toBigInteger(); } else { return (T) BigInteger.valueOf(number.longValue()); } } else if (Float.class == targetClass) { return (T) Float.valueOf(number.floatValue()); } else if (Double.class == targetClass) { return (T) Double.valueOf(number.doubleValue()); } else if (BigDecimal.class == targetClass) { return (T) new BigDecimal(number.toString()); } else if (AtomicInteger.class == targetClass) { return (T) new AtomicInteger(number.intValue()); } else if (AtomicLong.class == targetClass) { return (T) new AtomicLong(number.longValue()); } else { throw new IllegalArgumentException("Could not convert number [" + number + "] of type [" + number.getClass().getName() + "] to unsupported target class [" + targetClass.getName() + "]"); } }  @SuppressWarnings("unchecked") static T convertNumberToTargetClass(Number number, Class<T> targetClass); @SuppressWarnings("unchecked") static T parseNumber(String text, Class<T> targetClass); static String trimAllWhitespace(String str); }
NumberUtils { @SuppressWarnings("unchecked") public static <T extends Number> T convertNumberToTargetClass(Number number, Class<T> targetClass) throws IllegalArgumentException { if (targetClass.isInstance(number)) { return (T) number; } else if (Byte.class == targetClass) { long value = checkedLongValue(number, targetClass); if (value < Byte.MIN_VALUE || value > Byte.MAX_VALUE) { raiseOverflowException(number, targetClass); } return (T) Byte.valueOf(number.byteValue()); } else if (Short.class == targetClass) { long value = checkedLongValue(number, targetClass); if (value < Short.MIN_VALUE || value > Short.MAX_VALUE) { raiseOverflowException(number, targetClass); } return (T) Short.valueOf(number.shortValue()); } else if (Integer.class == targetClass) { long value = checkedLongValue(number, targetClass); if (value < Integer.MIN_VALUE || value > Integer.MAX_VALUE) { raiseOverflowException(number, targetClass); } return (T) Integer.valueOf(number.intValue()); } else if (Long.class == targetClass) { long value = checkedLongValue(number, targetClass); return (T) Long.valueOf(value); } else if (BigInteger.class == targetClass) { if (number instanceof BigDecimal) { return (T) ((BigDecimal) number).toBigInteger(); } else { return (T) BigInteger.valueOf(number.longValue()); } } else if (Float.class == targetClass) { return (T) Float.valueOf(number.floatValue()); } else if (Double.class == targetClass) { return (T) Double.valueOf(number.doubleValue()); } else if (BigDecimal.class == targetClass) { return (T) new BigDecimal(number.toString()); } else if (AtomicInteger.class == targetClass) { return (T) new AtomicInteger(number.intValue()); } else if (AtomicLong.class == targetClass) { return (T) new AtomicLong(number.longValue()); } else { throw new IllegalArgumentException("Could not convert number [" + number + "] of type [" + number.getClass().getName() + "] to unsupported target class [" + targetClass.getName() + "]"); } }  @SuppressWarnings("unchecked") static T convertNumberToTargetClass(Number number, Class<T> targetClass); @SuppressWarnings("unchecked") static T parseNumber(String text, Class<T> targetClass); static String trimAllWhitespace(String str); static final Set<Class<?>> STANDARD_NUMBER_TYPES; }