@ -1,5 +1,5 @@
@@ -1,5 +1,5 @@
/ *
* Copyright 2002 - 2008 the original author or authors .
* Copyright 2002 - 2009 the original author or authors .
*
* Licensed under the Apache License , Version 2 . 0 ( the "License" ) ;
* you may not use this file except in compliance with the License .
@ -49,59 +49,60 @@ public abstract class NumberUtils {
@@ -49,59 +49,60 @@ public abstract class NumberUtils {
* @see java . lang . Double
* @see java . math . BigDecimal
* /
public static Number convertNumberToTargetClass ( Number number , Class targetClass )
@SuppressWarnings ( "unchecked" )
public static < T extends Number > T convertNumberToTargetClass ( Number number , Class < T > targetClass )
throws IllegalArgumentException {
Assert . notNull ( number , "Number must not be null" ) ;
Assert . notNull ( targetClass , "Target class must not be null" ) ;
if ( targetClass . isInstance ( number ) ) {
return number ;
return ( T ) number ;
}
else if ( targetClass . equals ( Byte . class ) ) {
long value = number . longValue ( ) ;
if ( value < Byte . MIN_VALUE | | value > Byte . MAX_VALUE ) {
raiseOverflowException ( number , targetClass ) ;
}
return number . byteValue ( ) ;
return ( T ) new Byte ( number . byteValue ( ) ) ;
}
else if ( targetClass . equals ( Short . class ) ) {
long value = number . longValue ( ) ;
if ( value < Short . MIN_VALUE | | value > Short . MAX_VALUE ) {
raiseOverflowException ( number , targetClass ) ;
}
return number . shortValue ( ) ;
return ( T ) new Short ( number . shortValue ( ) ) ;
}
else if ( targetClass . equals ( Integer . class ) ) {
long value = number . longValue ( ) ;
if ( value < Integer . MIN_VALUE | | value > Integer . MAX_VALUE ) {
raiseOverflowException ( number , targetClass ) ;
}
return number . intValue ( ) ;
return ( T ) new Integer ( number . intValue ( ) ) ;
}
else if ( targetClass . equals ( Long . class ) ) {
return number . longValue ( ) ;
return ( T ) new Long ( number . longValue ( ) ) ;
}
else if ( targetClass . equals ( BigInteger . class ) ) {
if ( number instanceof BigDecimal ) {
// do not lose precision - use BigDecimal's own conversion
return ( ( BigDecimal ) number ) . toBigInteger ( ) ;
return ( T ) ( ( BigDecimal ) number ) . toBigInteger ( ) ;
}
else {
// original value is not a Big* number - use standard long conversion
return BigInteger . valueOf ( number . longValue ( ) ) ;
return ( T ) BigInteger . valueOf ( number . longValue ( ) ) ;
}
}
else if ( targetClass . equals ( Float . class ) ) {
return number . floatValue ( ) ;
return ( T ) new Float ( number . floatValue ( ) ) ;
}
else if ( targetClass . equals ( Double . class ) ) {
return number . doubleValue ( ) ;
return ( T ) new Double ( number . doubleValue ( ) ) ;
}
else if ( targetClass . equals ( BigDecimal . class ) ) {
// always use BigDecimal(String) here to avoid unpredictability of BigDecimal(double)
// (see BigDecimal javadoc for details)
return new BigDecimal ( number . toString ( ) ) ;
return ( T ) new BigDecimal ( number . toString ( ) ) ;
}
else {
throw new IllegalArgumentException ( "Could not convert number [" + number + "] of type [" +
@ -138,34 +139,35 @@ public abstract class NumberUtils {
@@ -138,34 +139,35 @@ public abstract class NumberUtils {
* @see java . lang . Double # valueOf
* @see java . math . BigDecimal # BigDecimal ( String )
* /
public static Number parseNumber ( String text , Class targetClass ) {
@SuppressWarnings ( "unchecked" )
public static < T extends Number > T parseNumber ( String text , Class < T > targetClass ) {
Assert . notNull ( text , "Text must not be null" ) ;
Assert . notNull ( targetClass , "Target class must not be null" ) ;
String trimmed = StringUtils . trimAllWhitespace ( text ) ;
if ( targetClass . equals ( Byte . class ) ) {
return ( isHexNumber ( trimmed ) ? Byte . decode ( trimmed ) : Byte . valueOf ( trimmed ) ) ;
return ( T ) ( isHexNumber ( trimmed ) ? Byte . decode ( trimmed ) : Byte . valueOf ( trimmed ) ) ;
}
else if ( targetClass . equals ( Short . class ) ) {
return ( isHexNumber ( trimmed ) ? Short . decode ( trimmed ) : Short . valueOf ( trimmed ) ) ;
return ( T ) ( isHexNumber ( trimmed ) ? Short . decode ( trimmed ) : Short . valueOf ( trimmed ) ) ;
}
else if ( targetClass . equals ( Integer . class ) ) {
return ( isHexNumber ( trimmed ) ? Integer . decode ( trimmed ) : Integer . valueOf ( trimmed ) ) ;
return ( T ) ( isHexNumber ( trimmed ) ? Integer . decode ( trimmed ) : Integer . valueOf ( trimmed ) ) ;
}
else if ( targetClass . equals ( Long . class ) ) {
return ( isHexNumber ( trimmed ) ? Long . decode ( trimmed ) : Long . valueOf ( trimmed ) ) ;
return ( T ) ( isHexNumber ( trimmed ) ? Long . decode ( trimmed ) : Long . valueOf ( trimmed ) ) ;
}
else if ( targetClass . equals ( BigInteger . class ) ) {
return ( isHexNumber ( trimmed ) ? decodeBigInteger ( trimmed ) : new BigInteger ( trimmed ) ) ;
return ( T ) ( isHexNumber ( trimmed ) ? decodeBigInteger ( trimmed ) : new BigInteger ( trimmed ) ) ;
}
else if ( targetClass . equals ( Float . class ) ) {
return Float . valueOf ( trimmed ) ;
return ( T ) Float . valueOf ( trimmed ) ;
}
else if ( targetClass . equals ( Double . class ) ) {
return Double . valueOf ( trimmed ) ;
return ( T ) Double . valueOf ( trimmed ) ;
}
else if ( targetClass . equals ( BigDecimal . class ) | | targetClass . equals ( Number . class ) ) {
return new BigDecimal ( trimmed ) ;
return ( T ) new BigDecimal ( trimmed ) ;
}
else {
throw new IllegalArgumentException (
@ -188,7 +190,7 @@ public abstract class NumberUtils {
@@ -188,7 +190,7 @@ public abstract class NumberUtils {
* @see # convertNumberToTargetClass
* @see # parseNumber ( String , Class )
* /
public static Number parseNumber ( String text , Class targetClass , NumberFormat numberFormat ) {
public static < T extends Number > T parseNumber ( String text , Class < T > targetClass , NumberFormat numberFormat ) {
if ( numberFormat ! = null ) {
Assert . notNull ( text , "Text must not be null" ) ;
Assert . notNull ( targetClass , "Target class must not be null" ) ;