@Test public void testParse() throws ClassNotFoundException { final Object[][] testParameters = new Object[][] {{"127", Byte.class, (byte) 127}, {"128", Short.class, (short) 128}, {"255", Integer.class, 255}, {"256", Long.class, 256L}, {"511", BigInteger.class, BigInteger.valueOf(511)}, {"512", BigInteger.class, new BigDecimal("512").toBigInteger()}, {"1023", Float.class, 1023f}, {"1024", Double.class, 1024d}, {"2047", BigDecimal.class, new BigDecimal("2047")}}; for (final Object[] testParameter : testParameters) { Object expected = Class.forName(testParameter[2].getClass().getName()).cast(testParameter[2]); String text = (String) testParameter[0]; Class<?> clazz = (Class<?>) testParameter[1]; assertEquals(expected, NumberUtils.parse(text, clazz)); } }
@SuppressWarnings("unchecked") public static <T> T parse(final String text, final Class<?> clazz) { assertNotNull(text, "Text must not be null"); assertNotNull(clazz, "Target class must not be null"); final String trimmed = StringUtils.trim(text); if (clazz.equals(Byte.class) || clazz.equals(byte.class)) { return (T) Byte.decode(trimmed); } else if (clazz.equals(Short.class) || clazz.equals(short.class)) { return (T) Short.decode(trimmed); } else if (clazz.equals(Integer.class) || clazz.equals(int.class)) { return (T) Integer.decode(trimmed); } else if (clazz.equals(Long.class) || clazz.equals(long.class)) { return (T) Long.decode(trimmed); } else if (clazz.equals(BigInteger.class)) { return (T) decodeBigInteger(trimmed); } else if (clazz.equals(Float.class) || clazz.equals(float.class)) { return (T) Float.valueOf(trimmed); } else if (clazz.equals(Double.class) || clazz.equals(double.class)) { return (T) Double.valueOf(trimmed); } else if (clazz.equals(BigDecimal.class) || clazz.equals(Number.class)) { return (T) new BigDecimal(trimmed); } else { throw new IllegalArgumentException( "Cannot convert String [" + text + "] to target class [" + clazz.getName() + "]"); } }
NumberUtils { @SuppressWarnings("unchecked") public static <T> T parse(final String text, final Class<?> clazz) { assertNotNull(text, "Text must not be null"); assertNotNull(clazz, "Target class must not be null"); final String trimmed = StringUtils.trim(text); if (clazz.equals(Byte.class) || clazz.equals(byte.class)) { return (T) Byte.decode(trimmed); } else if (clazz.equals(Short.class) || clazz.equals(short.class)) { return (T) Short.decode(trimmed); } else if (clazz.equals(Integer.class) || clazz.equals(int.class)) { return (T) Integer.decode(trimmed); } else if (clazz.equals(Long.class) || clazz.equals(long.class)) { return (T) Long.decode(trimmed); } else if (clazz.equals(BigInteger.class)) { return (T) decodeBigInteger(trimmed); } else if (clazz.equals(Float.class) || clazz.equals(float.class)) { return (T) Float.valueOf(trimmed); } else if (clazz.equals(Double.class) || clazz.equals(double.class)) { return (T) Double.valueOf(trimmed); } else if (clazz.equals(BigDecimal.class) || clazz.equals(Number.class)) { return (T) new BigDecimal(trimmed); } else { throw new IllegalArgumentException( "Cannot convert String [" + text + "] to target class [" + clazz.getName() + "]"); } } }
NumberUtils { @SuppressWarnings("unchecked") public static <T> T parse(final String text, final Class<?> clazz) { assertNotNull(text, "Text must not be null"); assertNotNull(clazz, "Target class must not be null"); final String trimmed = StringUtils.trim(text); if (clazz.equals(Byte.class) || clazz.equals(byte.class)) { return (T) Byte.decode(trimmed); } else if (clazz.equals(Short.class) || clazz.equals(short.class)) { return (T) Short.decode(trimmed); } else if (clazz.equals(Integer.class) || clazz.equals(int.class)) { return (T) Integer.decode(trimmed); } else if (clazz.equals(Long.class) || clazz.equals(long.class)) { return (T) Long.decode(trimmed); } else if (clazz.equals(BigInteger.class)) { return (T) decodeBigInteger(trimmed); } else if (clazz.equals(Float.class) || clazz.equals(float.class)) { return (T) Float.valueOf(trimmed); } else if (clazz.equals(Double.class) || clazz.equals(double.class)) { return (T) Double.valueOf(trimmed); } else if (clazz.equals(BigDecimal.class) || clazz.equals(Number.class)) { return (T) new BigDecimal(trimmed); } else { throw new IllegalArgumentException( "Cannot convert String [" + text + "] to target class [" + clazz.getName() + "]"); } }  }
NumberUtils { @SuppressWarnings("unchecked") public static <T> T parse(final String text, final Class<?> clazz) { assertNotNull(text, "Text must not be null"); assertNotNull(clazz, "Target class must not be null"); final String trimmed = StringUtils.trim(text); if (clazz.equals(Byte.class) || clazz.equals(byte.class)) { return (T) Byte.decode(trimmed); } else if (clazz.equals(Short.class) || clazz.equals(short.class)) { return (T) Short.decode(trimmed); } else if (clazz.equals(Integer.class) || clazz.equals(int.class)) { return (T) Integer.decode(trimmed); } else if (clazz.equals(Long.class) || clazz.equals(long.class)) { return (T) Long.decode(trimmed); } else if (clazz.equals(BigInteger.class)) { return (T) decodeBigInteger(trimmed); } else if (clazz.equals(Float.class) || clazz.equals(float.class)) { return (T) Float.valueOf(trimmed); } else if (clazz.equals(Double.class) || clazz.equals(double.class)) { return (T) Double.valueOf(trimmed); } else if (clazz.equals(BigDecimal.class) || clazz.equals(Number.class)) { return (T) new BigDecimal(trimmed); } else { throw new IllegalArgumentException( "Cannot convert String [" + text + "] to target class [" + clazz.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); }
NumberUtils { @SuppressWarnings("unchecked") public static <T> T parse(final String text, final Class<?> clazz) { assertNotNull(text, "Text must not be null"); assertNotNull(clazz, "Target class must not be null"); final String trimmed = StringUtils.trim(text); if (clazz.equals(Byte.class) || clazz.equals(byte.class)) { return (T) Byte.decode(trimmed); } else if (clazz.equals(Short.class) || clazz.equals(short.class)) { return (T) Short.decode(trimmed); } else if (clazz.equals(Integer.class) || clazz.equals(int.class)) { return (T) Integer.decode(trimmed); } else if (clazz.equals(Long.class) || clazz.equals(long.class)) { return (T) Long.decode(trimmed); } else if (clazz.equals(BigInteger.class)) { return (T) decodeBigInteger(trimmed); } else if (clazz.equals(Float.class) || clazz.equals(float.class)) { return (T) Float.valueOf(trimmed); } else if (clazz.equals(Double.class) || clazz.equals(double.class)) { return (T) Double.valueOf(trimmed); } else if (clazz.equals(BigDecimal.class) || clazz.equals(Number.class)) { return (T) new BigDecimal(trimmed); } else { throw new IllegalArgumentException( "Cannot convert String [" + text + "] to target class [" + clazz.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);  }