@Test public void testParseNumber() throws Exception { assertEquals(new Integer(1), NumberUtils.parseNumber("1", Integer.class)); assertEquals(new Byte("1"), NumberUtils.parseNumber("1", Byte.class)); assertEquals(new Float(1.1), NumberUtils.parseNumber("1.1", Float.class)); assertEquals(new Double(1.1), NumberUtils.parseNumber("1.1", Double.class)); assertEquals(new Long(1), NumberUtils.parseNumber("1", Long.class)); assertEquals(new Short("1"), NumberUtils.parseNumber("1", Short.class)); assertEquals(new BigInteger("1"), NumberUtils.parseNumber("1", BigInteger.class)); }
@SuppressWarnings("unchecked") public static <T extends Number> T parseNumber(String text, Class<T> targetClass) { String trimmed = trimAllWhitespace(text); if (Byte.class == targetClass) { return (T) (isHexNumber(trimmed) ? Byte.decode(trimmed) : Byte.valueOf(trimmed)); } else if (Short.class == targetClass) { return (T) (isHexNumber(trimmed) ? Short.decode(trimmed) : Short.valueOf(trimmed)); } else if (Integer.class == targetClass) { return (T) (isHexNumber(trimmed) ? Integer.decode(trimmed) : Integer.valueOf(trimmed)); } else if (Long.class == targetClass) { return (T) (isHexNumber(trimmed) ? Long.decode(trimmed) : Long.valueOf(trimmed)); } else if (BigInteger.class == targetClass) { return (T) (isHexNumber(trimmed) ? decodeBigInteger(trimmed) : new BigInteger(trimmed)); } else if (Float.class == targetClass) { return (T) Float.valueOf(trimmed); } else if (Double.class == targetClass) { return (T) Double.valueOf(trimmed); } else if (BigDecimal.class == targetClass || Number.class == targetClass) { return (T) new BigDecimal(trimmed); } else { throw new IllegalArgumentException( "Cannot convert String [" + text + "] to target class [" + targetClass.getName() + "]"); } }
NumberUtils { @SuppressWarnings("unchecked") public static <T extends Number> T parseNumber(String text, Class<T> targetClass) { String trimmed = trimAllWhitespace(text); if (Byte.class == targetClass) { return (T) (isHexNumber(trimmed) ? Byte.decode(trimmed) : Byte.valueOf(trimmed)); } else if (Short.class == targetClass) { return (T) (isHexNumber(trimmed) ? Short.decode(trimmed) : Short.valueOf(trimmed)); } else if (Integer.class == targetClass) { return (T) (isHexNumber(trimmed) ? Integer.decode(trimmed) : Integer.valueOf(trimmed)); } else if (Long.class == targetClass) { return (T) (isHexNumber(trimmed) ? Long.decode(trimmed) : Long.valueOf(trimmed)); } else if (BigInteger.class == targetClass) { return (T) (isHexNumber(trimmed) ? decodeBigInteger(trimmed) : new BigInteger(trimmed)); } else if (Float.class == targetClass) { return (T) Float.valueOf(trimmed); } else if (Double.class == targetClass) { return (T) Double.valueOf(trimmed); } else if (BigDecimal.class == targetClass || Number.class == targetClass) { return (T) new BigDecimal(trimmed); } else { throw new IllegalArgumentException( "Cannot convert String [" + text + "] to target class [" + targetClass.getName() + "]"); } } }
NumberUtils { @SuppressWarnings("unchecked") public static <T extends Number> T parseNumber(String text, Class<T> targetClass) { String trimmed = trimAllWhitespace(text); if (Byte.class == targetClass) { return (T) (isHexNumber(trimmed) ? Byte.decode(trimmed) : Byte.valueOf(trimmed)); } else if (Short.class == targetClass) { return (T) (isHexNumber(trimmed) ? Short.decode(trimmed) : Short.valueOf(trimmed)); } else if (Integer.class == targetClass) { return (T) (isHexNumber(trimmed) ? Integer.decode(trimmed) : Integer.valueOf(trimmed)); } else if (Long.class == targetClass) { return (T) (isHexNumber(trimmed) ? Long.decode(trimmed) : Long.valueOf(trimmed)); } else if (BigInteger.class == targetClass) { return (T) (isHexNumber(trimmed) ? decodeBigInteger(trimmed) : new BigInteger(trimmed)); } else if (Float.class == targetClass) { return (T) Float.valueOf(trimmed); } else if (Double.class == targetClass) { return (T) Double.valueOf(trimmed); } else if (BigDecimal.class == targetClass || Number.class == targetClass) { return (T) new BigDecimal(trimmed); } else { throw new IllegalArgumentException( "Cannot convert String [" + text + "] to target class [" + targetClass.getName() + "]"); } }  }
NumberUtils { @SuppressWarnings("unchecked") public static <T extends Number> T parseNumber(String text, Class<T> targetClass) { String trimmed = trimAllWhitespace(text); if (Byte.class == targetClass) { return (T) (isHexNumber(trimmed) ? Byte.decode(trimmed) : Byte.valueOf(trimmed)); } else if (Short.class == targetClass) { return (T) (isHexNumber(trimmed) ? Short.decode(trimmed) : Short.valueOf(trimmed)); } else if (Integer.class == targetClass) { return (T) (isHexNumber(trimmed) ? Integer.decode(trimmed) : Integer.valueOf(trimmed)); } else if (Long.class == targetClass) { return (T) (isHexNumber(trimmed) ? Long.decode(trimmed) : Long.valueOf(trimmed)); } else if (BigInteger.class == targetClass) { return (T) (isHexNumber(trimmed) ? decodeBigInteger(trimmed) : new BigInteger(trimmed)); } else if (Float.class == targetClass) { return (T) Float.valueOf(trimmed); } else if (Double.class == targetClass) { return (T) Double.valueOf(trimmed); } else if (BigDecimal.class == targetClass || Number.class == targetClass) { return (T) new BigDecimal(trimmed); } else { throw new IllegalArgumentException( "Cannot convert String [" + text + "] to 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 parseNumber(String text, Class<T> targetClass) { String trimmed = trimAllWhitespace(text); if (Byte.class == targetClass) { return (T) (isHexNumber(trimmed) ? Byte.decode(trimmed) : Byte.valueOf(trimmed)); } else if (Short.class == targetClass) { return (T) (isHexNumber(trimmed) ? Short.decode(trimmed) : Short.valueOf(trimmed)); } else if (Integer.class == targetClass) { return (T) (isHexNumber(trimmed) ? Integer.decode(trimmed) : Integer.valueOf(trimmed)); } else if (Long.class == targetClass) { return (T) (isHexNumber(trimmed) ? Long.decode(trimmed) : Long.valueOf(trimmed)); } else if (BigInteger.class == targetClass) { return (T) (isHexNumber(trimmed) ? decodeBigInteger(trimmed) : new BigInteger(trimmed)); } else if (Float.class == targetClass) { return (T) Float.valueOf(trimmed); } else if (Double.class == targetClass) { return (T) Double.valueOf(trimmed); } else if (BigDecimal.class == targetClass || Number.class == targetClass) { return (T) new BigDecimal(trimmed); } else { throw new IllegalArgumentException( "Cannot convert String [" + text + "] to 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; }