public abstract class NumberUtils
extends java.lang.Object
Mainly for internal use within the framework; consider Apache's Commons Lang for a more comprehensive suite of number utilities.
Modifier and Type | Field and Description |
---|---|
private static java.math.BigInteger |
LONG_MAX |
private static java.math.BigInteger |
LONG_MIN |
static java.util.Set<java.lang.Class<?>> |
STANDARD_NUMBER_TYPES
Standard number types (all immutable):
Byte, Short, Integer, Long, BigInteger, Float, Double, BigDecimal.
|
Constructor and Description |
---|
NumberUtils() |
Modifier and Type | Method and Description |
---|---|
private static long |
checkedLongValue(java.lang.Number number,
java.lang.Class<? extends java.lang.Number> targetClass)
Check for a
BigInteger /BigDecimal long overflow
before returning the given number as a long value. |
static <T extends java.lang.Number> |
convertNumberToTargetClass(java.lang.Number number,
java.lang.Class<T> targetClass)
Convert the given number into an instance of the given target class.
|
private static java.math.BigInteger |
decodeBigInteger(java.lang.String value)
Decode a
BigInteger from the supplied String value. |
private static boolean |
isHexNumber(java.lang.String value)
Determine whether the given
value String indicates a hex number,
i.e. |
static <T extends java.lang.Number> |
parseNumber(java.lang.String text,
java.lang.Class<T> targetClass)
Parse the given
text into a Number instance of the given
target class, using the corresponding decode / valueOf method. |
static <T extends java.lang.Number> |
parseNumber(java.lang.String text,
java.lang.Class<T> targetClass,
java.text.NumberFormat numberFormat)
Parse the given
text into a Number instance of the
given target class, using the supplied NumberFormat . |
private static void |
raiseOverflowException(java.lang.Number number,
java.lang.Class<?> targetClass)
Raise an overflow exception for the given number and target class.
|
private static final java.math.BigInteger LONG_MIN
private static final java.math.BigInteger LONG_MAX
public static final java.util.Set<java.lang.Class<?>> STANDARD_NUMBER_TYPES
public static <T extends java.lang.Number> T convertNumberToTargetClass(java.lang.Number number, java.lang.Class<T> targetClass) throws java.lang.IllegalArgumentException
number
- the number to converttargetClass
- the target class to convert tojava.lang.IllegalArgumentException
- if the target class is not supported
(i.e. not a standard Number subclass as included in the JDK)Byte
,
Short
,
Integer
,
Long
,
BigInteger
,
Float
,
Double
,
BigDecimal
private static long checkedLongValue(java.lang.Number number, java.lang.Class<? extends java.lang.Number> targetClass)
BigInteger
/BigDecimal
long overflow
before returning the given number as a long value.number
- the number to converttargetClass
- the target class to convert tojava.lang.IllegalArgumentException
- if there is an overflowraiseOverflowException(java.lang.Number, java.lang.Class<?>)
private static void raiseOverflowException(java.lang.Number number, java.lang.Class<?> targetClass)
number
- the number we tried to converttargetClass
- the target class we tried to convert tojava.lang.IllegalArgumentException
- if there is an overflowpublic static <T extends java.lang.Number> T parseNumber(java.lang.String text, java.lang.Class<T> targetClass)
text
into a Number
instance of the given
target class, using the corresponding decode
/ valueOf
method.
Trims the input String
before attempting to parse the number.
Supports numbers in hex format (with leading "0x", "0X", or "#") as well.
text
- the text to converttargetClass
- the target class to parse intojava.lang.IllegalArgumentException
- if the target class is not supported
(i.e. not a standard Number subclass as included in the JDK)Byte.decode(java.lang.String)
,
Short.decode(java.lang.String)
,
Integer.decode(java.lang.String)
,
Long.decode(java.lang.String)
,
decodeBigInteger(String)
,
Float.valueOf(java.lang.String)
,
Double.valueOf(java.lang.String)
,
BigDecimal.BigDecimal(String)
public static <T extends java.lang.Number> T parseNumber(java.lang.String text, java.lang.Class<T> targetClass, @Nullable java.text.NumberFormat numberFormat)
text
into a Number
instance of the
given target class, using the supplied NumberFormat
.
Trims the input String
before attempting to parse the number.
text
- the text to converttargetClass
- the target class to parse intonumberFormat
- the NumberFormat
to use for parsing (if
null
, this method falls back to parseNumber(String, Class)
)java.lang.IllegalArgumentException
- if the target class is not supported
(i.e. not a standard Number subclass as included in the JDK)NumberFormat.parse(java.lang.String, java.text.ParsePosition)
,
convertNumberToTargetClass(java.lang.Number, java.lang.Class<T>)
,
parseNumber(String, Class)
private static boolean isHexNumber(java.lang.String value)
value
String indicates a hex number,
i.e. needs to be passed into Integer.decode
instead of
Integer.valueOf
, etc.private static java.math.BigInteger decodeBigInteger(java.lang.String value)
BigInteger
from the supplied String
value.
Supports decimal, hex, and octal notation.
BigInteger.BigInteger(String, int)