class 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() + "]");
    }
  }

}