private T toNumber()

in src/main/java/org/apache/commons/beanutils2/converters/NumberConverter.java [408:495]


    private <T> T toNumber(final Class<?> sourceType, final Class<T> targetType, final Number value) {
        // Correct Number type already
        if (targetType.equals(value.getClass())) {
            return targetType.cast(value);
        }

        // Byte
        if (targetType.equals(Byte.class)) {
            final long longValue = value.longValue();
            if (longValue > Byte.MAX_VALUE) {
                throw ConversionException.format("%s value '%s' is too large for %s", toString(sourceType), value, toString(targetType));
            }
            if (longValue < Byte.MIN_VALUE) {
                throw ConversionException.format("%s value '%s' is too small %s", toString(sourceType), value, toString(targetType));
            }
            return targetType.cast(Byte.valueOf(value.byteValue()));
        }

        // Short
        if (targetType.equals(Short.class)) {
            final long longValue = value.longValue();
            if (longValue > Short.MAX_VALUE) {
                throw ConversionException.format("%s value '%s' is too large for %s", toString(sourceType), value, toString(targetType));
            }
            if (longValue < Short.MIN_VALUE) {
                throw ConversionException.format("%s value '%s' is too small %s", toString(sourceType), value, toString(targetType));
            }
            return targetType.cast(Short.valueOf(value.shortValue()));
        }

        // Integer
        if (targetType.equals(Integer.class)) {
            final long longValue = value.longValue();
            if (longValue > Integer.MAX_VALUE) {
                throw ConversionException.format("%s value '%s' is too large for %s", toString(sourceType), value, toString(targetType));
            }
            if (longValue < Integer.MIN_VALUE) {
                throw ConversionException.format("%s value '%s' is too small %s", toString(sourceType), value, toString(targetType));
            }
            return targetType.cast(Integer.valueOf(value.intValue()));
        }

        // Long
        if (targetType.equals(Long.class)) {
            return targetType.cast(Long.valueOf(value.longValue()));
        }

        // Float
        if (targetType.equals(Float.class)) {
            if (value.doubleValue() > Float.MAX_VALUE) {
                throw ConversionException.format("%s value '%s' is too large for %s", toString(sourceType), value, toString(targetType));
            }
            return targetType.cast(Float.valueOf(value.floatValue()));
        }

        // Double
        if (targetType.equals(Double.class)) {
            return targetType.cast(Double.valueOf(value.doubleValue()));
        }

        // BigDecimal
        if (targetType.equals(BigDecimal.class)) {
            if (value instanceof Float || value instanceof Double) {
                return targetType.cast(new BigDecimal(value.toString()));
            }
            if (value instanceof BigInteger) {
                return targetType.cast(new BigDecimal((BigInteger) value));
            }
            if (value instanceof BigDecimal) {
                return targetType.cast(new BigDecimal(value.toString()));
            }
            return targetType.cast(BigDecimal.valueOf(value.longValue()));
        }

        // BigInteger
        if (targetType.equals(BigInteger.class)) {
            if (value instanceof BigDecimal) {
                return targetType.cast(((BigDecimal) value).toBigInteger());
            }
            return targetType.cast(BigInteger.valueOf(value.longValue()));
        }

        final String msg = toString(getClass()) + " cannot handle conversion to '" + toString(targetType) + "'";
        if (log().isWarnEnabled()) {
            log().warn("    " + msg);
        }
        throw new ConversionException(msg);
    }