NumberUtils { @SuppressWarnings("unchecked") public static <T> T convert(final Number number, final Class<T> targetClass) throws IllegalArgumentException { assertNotNull(number, "Number must not be null"); assertNotNull(targetClass, "Target class must not be null"); if (targetClass.isInstance(number)) { return (T) number; } else if (targetClass.equals(Byte.class) || targetClass.equals(byte.class)) { final long value = number.longValue(); if (value < Byte.MIN_VALUE || Byte.MAX_VALUE < value) { throwOverflowException(number, targetClass); } return (T) Byte.valueOf((byte) value); } else if (targetClass.equals(Short.class) || targetClass.equals(short.class)) { final long value = number.longValue(); if (value < Short.MIN_VALUE || Short.MAX_VALUE < value) { throwOverflowException(number, targetClass); } return (T) Short.valueOf((short) value); } else if (targetClass.equals(Integer.class) || targetClass.equals(int.class)) { final long value = number.longValue(); if (value < Integer.MIN_VALUE || Integer.MAX_VALUE < value) { throwOverflowException(number, targetClass); } return (T) Integer.valueOf((int) value); } else if (targetClass.equals(Long.class) || targetClass.equals(long.class)) { return (T) Long.valueOf(number.longValue()); } else if (targetClass.equals(BigInteger.class)) { if (number instanceof BigDecimal) { return (T) ((BigDecimal) number).toBigInteger(); } else { return (T) BigInteger.valueOf(number.longValue()); } } else if (targetClass.equals(Float.class) || targetClass.equals(float.class)) { return (T) Float.valueOf(number.floatValue()); } else if (targetClass.equals(Double.class) || targetClass.equals(double.class)) { return (T) Double.valueOf(number.doubleValue()); } else if (targetClass.equals(BigDecimal.class)) { return (T) new BigDecimal(number.toString()); } throw new IllegalArgumentException( "Could not convert number [" + number + "] of type [" + number.getClass().getName() + "] to unknown target class [" + targetClass.getName() + "]"); }  @SuppressWarnings("unchecked") static T convert(final Number number, final Class<T> targetClass); @SuppressWarnings("unchecked") static T parse(final String text, final Class<?> clazz); static T parse(final String text, final Class<T> targetClass,
      final NumberFormat numberFormat); static byte[] positiveToByteArray(final BigInteger positiveNumber); static BigInteger byteArrayToPositive(final byte[] rawBytes);  }
@Test public void testConvert() throws ClassNotFoundException { final Object[][] testParameters = new Object[][] {{127, Byte.class, (byte) 127}, {128, Short.class, (short) 128}, {255, Integer.class, 255}, {256L, Long.class, 256L}, {new BigInteger("511"), BigInteger.class, BigInteger.valueOf(511)}, {new BigDecimal("512"), BigInteger.class, new BigDecimal("512").toBigInteger()}, {1023f, Float.class, 1023f}, {1024d, Double.class, 1024d}, {new BigDecimal("2047"), BigDecimal.class, new BigDecimal("2047")}}; for (final Object[] testParameter : testParameters) { Object expected = Class.forName(testParameter[2].getClass().getName()).cast(testParameter[2]); Number number = (Number) testParameter[0]; Class<?> targetClass = (Class<?>) testParameter[1]; assertEquals(expected, NumberUtils.convert(number, targetClass)); } }