public final class Numbers extends Static
Number objects, and a few primitive types by extension.MathFunctionsDefined in the sis-utility module
| Modifier and Type | Field and Description |
|---|---|
static byte |
BIG_DECIMAL
Constant of value 10 used in
switch statements or as index in arrays. |
static byte |
BIG_INTEGER
Constant of value 9 used in
switch statements or as index in arrays. |
static byte |
BOOLEAN
Constant of value 1 used in
switch statements or as index in arrays. |
static byte |
BYTE
Constant of value 3 used in
switch statements or as index in arrays. |
static byte |
CHARACTER
Constant of value 2 used in
switch statements or as index in arrays. |
static byte |
DOUBLE
Constant of value 8 used in
switch statements or as index in arrays. |
static byte |
FLOAT
Constant of value 7 used in
switch statements or as index in arrays. |
static byte |
INTEGER
Constant of value 5 used in
switch statements or as index in arrays. |
static byte |
LONG
Constant of value 6 used in
switch statements or as index in arrays. |
static byte |
OTHER
Constant of value 0 used in
switch statements or as index in arrays. |
static byte |
SHORT
Constant of value 4 used in
switch statements or as index in arrays. |
| Modifier and Type | Method and Description |
|---|---|
static <N extends Number> |
cast(Number number,
Class<N> type)
Casts a number to the specified type.
|
static byte |
getEnumConstant(Class<?> type)
Returns a numeric constant for the given type.
|
static boolean |
isFloat(Class<?> type)
Returns
true if the given type is a floating point type. |
static boolean |
isInteger(Class<?> type)
Returns
true if the given type is an integer type. |
static Class<? extends Number> |
narrowestClass(Class<? extends Number> c1,
Class<? extends Number> c2)
Returns the narrowest of the given types.
|
static Class<? extends Number> |
narrowestClass(Number value)
Returns the smallest class capable to hold the specified value.
|
static Class<? extends Number> |
narrowestClass(Number n1,
Number n2)
Returns the narrowest type of two numbers.
|
static Number |
narrowestNumber(Number value)
Returns the given number wrapped in the smallest class capable to hold the specified value.
|
static Number |
narrowestNumber(String value)
Returns the smallest number capable to hold the specified value.
|
static int |
primitiveBitCount(Class<?> type)
Returns the number of bits used by primitive of the specified type.
|
static Class<?> |
primitiveToWrapper(Class<?> type)
Changes a primitive class to its wrapper (for example
int to Integer). |
static <T> T |
valueOf(String value,
Class<T> type)
Converts the specified string into a value object.
|
static <T> T |
valueOfNil(Class<T> type)
Returns a
NaN, zero, empty or null value of the given type. |
static Class<? extends Number> |
widestClass(Class<? extends Number> c1,
Class<? extends Number> c2)
Returns the widest of the given types.
|
static Class<? extends Number> |
widestClass(Number n1,
Number n2)
Returns the widest type of two numbers.
|
static <N extends Number> |
wrap(double value,
Class<N> type)
Wraps the given floating-point value in a
Number of the specified class. |
static <N extends Number> |
wrap(long value,
Class<N> type)
Wraps the given integer value in a
Number of the specified class. |
static Class<?> |
wrapperToPrimitive(Class<?> type)
Changes a wrapper class to its primitive (for example
Integer to int). |
public static final byte BIG_DECIMAL
switch statements or as index in arrays.
This enumeration provides the following guarantees (some Apache SIS code rely on them):
OTHER value is 0.BYTE, SHORT, INTEGER, LONG, FLOAT, DOUBLE.java.math types of greater capacity that primitive types (BIG_DECIMAL
and BIG_INTEGER) have higher enumeration values.public static final byte BIG_INTEGER
switch statements or as index in arrays.
This enumeration provides the following guarantees (some Apache SIS code rely on them):
OTHER value is 0.BYTE, SHORT, INTEGER, LONG, FLOAT, DOUBLE.java.math types of greater capacity that primitive types (BIG_DECIMAL
and BIG_INTEGER) have higher enumeration values.public static final byte DOUBLE
switch statements or as index in arrays.
This enumeration provides the following guarantees (some Apache SIS code rely on them):
OTHER value is 0.BYTE, SHORT, INTEGER, LONG, FLOAT, DOUBLE.java.math types of greater capacity that primitive types (BIG_DECIMAL
and BIG_INTEGER) have higher enumeration values.public static final byte FLOAT
switch statements or as index in arrays.
This enumeration provides the following guarantees (some Apache SIS code rely on them):
OTHER value is 0.BYTE, SHORT, INTEGER, LONG, FLOAT, DOUBLE.java.math types of greater capacity that primitive types (BIG_DECIMAL
and BIG_INTEGER) have higher enumeration values.public static final byte LONG
switch statements or as index in arrays.
This enumeration provides the following guarantees (some Apache SIS code rely on them):
OTHER value is 0.BYTE, SHORT, INTEGER, LONG, FLOAT, DOUBLE.java.math types of greater capacity that primitive types (BIG_DECIMAL
and BIG_INTEGER) have higher enumeration values.public static final byte INTEGER
switch statements or as index in arrays.
This enumeration provides the following guarantees (some Apache SIS code rely on them):
OTHER value is 0.BYTE, SHORT, INTEGER, LONG, FLOAT, DOUBLE.java.math types of greater capacity that primitive types (BIG_DECIMAL
and BIG_INTEGER) have higher enumeration values.public static final byte SHORT
switch statements or as index in arrays.
This enumeration provides the following guarantees (some Apache SIS code rely on them):
OTHER value is 0.BYTE, SHORT, INTEGER, LONG, FLOAT, DOUBLE.java.math types of greater capacity that primitive types (BIG_DECIMAL
and BIG_INTEGER) have higher enumeration values.public static final byte BYTE
switch statements or as index in arrays.
This enumeration provides the following guarantees (some Apache SIS code rely on them):
OTHER value is 0.BYTE, SHORT, INTEGER, LONG, FLOAT, DOUBLE.java.math types of greater capacity that primitive types (BIG_DECIMAL
and BIG_INTEGER) have higher enumeration values.public static final byte CHARACTER
switch statements or as index in arrays.
This enumeration provides the following guarantees (some Apache SIS code rely on them):
OTHER value is 0.BYTE, SHORT, INTEGER, LONG, FLOAT, DOUBLE.java.math types of greater capacity that primitive types (BIG_DECIMAL
and BIG_INTEGER) have higher enumeration values.public static final byte BOOLEAN
switch statements or as index in arrays.
This enumeration provides the following guarantees (some Apache SIS code rely on them):
OTHER value is 0.BYTE, SHORT, INTEGER, LONG, FLOAT, DOUBLE.java.math types of greater capacity that primitive types (BIG_DECIMAL
and BIG_INTEGER) have higher enumeration values.public static final byte OTHER
switch statements or as index in arrays.
This enumeration provides the following guarantees (some Apache SIS code rely on them):
OTHER value is 0.BYTE, SHORT, INTEGER, LONG, FLOAT, DOUBLE.java.math types of greater capacity that primitive types (BIG_DECIMAL
and BIG_INTEGER) have higher enumeration values.public static boolean isFloat(Class<?> type)
true if the given type is a floating point type.
The list of floating point types include primitive and wrapper classes of
Float and Double, together with the BigDecimal class.type - the type to test (may be null).true if type is one of the known types capable to represent floating point numbers.isInteger(Class)public static boolean isInteger(Class<?> type)
true if the given type is an integer type. The integer types are
Long, long, Integer, int, Short, short,
Byte, byte and BigInteger.type - the type to test (may be null).true if type is an integer type.isFloat(Class)public static int primitiveBitCount(Class<?> type) throws IllegalArgumentException
type - the primitive type (may be null).type is null.IllegalArgumentException - if the given type is unknown.public static Class<?> primitiveToWrapper(Class<?> type)
int to Integer).
If the specified class is not a primitive type, then it is returned unchanged.type - the primitive type (may be null).wrapperToPrimitive(Class)public static Class<?> wrapperToPrimitive(Class<?> type)
Integer to int).
If the specified class is not a wrapper type, then it is returned unchanged.type - the wrapper type (may be null).primitiveToWrapper(Class)public static Class<? extends Number> widestClass(Number n1, Number n2) throws IllegalArgumentException
n1 and n2 can be instance of
Byte, Short, Integer, Long, Float, Double,
BigInteger or BigDecimal types.
If one of the given argument is null, then this method returns the class of the non-null argument.
If both arguments are null, then this method returns null.
n1 - the first number, or null.n2 - the second number, or null.null if not n1 and n2 are null.IllegalArgumentException - if a number is not of a known type.widestClass(Number, Number),
narrowestClass(Number, Number)public static Class<? extends Number> widestClass(Class<? extends Number> c1, Class<? extends Number> c2) throws IllegalArgumentException
c1 and c2 can be
Byte, Short, Integer, Long, Float,
Double, BigInteger or BigDecimal types.
If one of the given argument is null, then this method returns the non-null argument.
If both arguments are null, then this method returns null.
widestClass(Short.class, Long.class);returns
Long.class.c1 - the first number type, or null.c2 - the second number type, or null.null if both c1 and c2 are null.IllegalArgumentException - if one of the given types is unknown.widestClass(Class, Class),
narrowestClass(Number, Number)public static Class<? extends Number> narrowestClass(Number n1, Number n2) throws IllegalArgumentException
n1 and n2 must be instance
of any of Byte, Short, Integer, Long, Float
Double, BigInteger or BigDecimal types.n1 - the first number.n2 - the second number.IllegalArgumentException - if a number is not of a known type.narrowestClass(Class, Class),
widestClass(Class, Class)public static Class<? extends Number> narrowestClass(Class<? extends Number> c1, Class<? extends Number> c2) throws IllegalArgumentException
c1 and c2 can be
Byte, Short, Integer, Long, Float,
Double, BigInteger or BigDecimal types.
If one of the given argument is null, then this method returns the non-null argument.
If both arguments are null, then this method returns null.
narrowestClass(Short.class, Long.class);returns
Short.class.c1 - the first number type, or null.c2 - the second number type, or null.null if both c1 and c2 are null.IllegalArgumentException - if one of the given types is unknown.narrowestClass(Number, Number),
widestClass(Class, Class)public static Class<? extends Number> narrowestClass(Number value)
null, then this method returns null.double to an other type
without precision lost, return Double.class.float to an other type
without precision lost, return Float.class.Byte.class;Short.class;Integer.class;Long.class;value - the value to be wrapped in a finer (if possible) Number.narrowestNumber(Number)public static Number narrowestNumber(Number value)
return cast(value, narrowestClass(value));
value - the value to be wrapped in a finer (if possible) Number.narrowestClass(Number),
cast(Number, Class)public static Number narrowestNumber(String value) throws NumberFormatException
value - the value to be wrapped in a Number.NumberFormatException - if the given value can not be parsed as a number.narrowestNumber(Number)public static <N extends Number> N cast(Number number, Class<N> type) throws IllegalArgumentException
Byte,
Short, Integer, Long, Float, Double,
BigInteger or BigDecimal.
This method makes the following choice:
null or an instance of the given type, then it is returned unchanged.Double.class, then this method returns
Double.valueOf(number.doubleValue());Float.class, then this method returns
Float.valueOf(number.floatValue());widestClass(Class, Class)
or narrowestClass(Number). If nevertheless the given type is not wide enough,
then the behavior depends on the implementation of the corresponding
Number.fooValue() method - typically, the value is just rounded or truncated.N - the class to cast to.number - the number to cast, or null.type - the destination type.null if the given value was null.IllegalArgumentException - if the given type is not one of the primitive wrappers for numeric types.public static <N extends Number> N wrap(double value, Class<N> type) throws IllegalArgumentException
Number of the specified class.
The given type shall be one of Byte, Short, Integer, Long,
Float, Double, BigInteger and BigDecimal classes.
Furthermore, the given value shall be convertible to the given class without precision lost,
otherwise an IllegalArgumentException will be thrown.N - the wrapper class.value - the value to wrap.type - the desired wrapper class.IllegalArgumentException - if the given type is not one of the primitive wrappers for numeric types,
or if the given value can not be wrapped in an instance of the given class without precision lost.public static <N extends Number> N wrap(long value, Class<N> type) throws IllegalArgumentException
Number of the specified class.
The given type shall be one of Byte, Short, Integer, Long,
Float, Double, BigInteger and BigDecimal classes.
Furthermore, the given value shall be convertible to the given class without precision lost,
otherwise an IllegalArgumentException will be thrown.N - the wrapper class.value - the value to wrap.type - the desired wrapper class.IllegalArgumentException - if the given type is not one of the primitive wrappers for numeric types,
or if the given value can not be wrapped in an instance of the given class without precision lost.public static <T> T valueOf(String value, Class<T> type) throws IllegalArgumentException, NumberFormatException
BigDecimal, BigInteger,
Double, Float, Long, Integer, Short, Byte,
Boolean, Character or String according the specified type.
This method makes the following choice:
Double.class, then this method returns
Double.valueOf(value);Float.class, then this method returns
Float.valueOf(value);T - the requested type.value - the value to parse.type - the requested type.null if value was null.IllegalArgumentException - if type is not a recognized type.NumberFormatException - if type is a subclass of Number
and the string value is not parsable as a number of the specified type.public static <T> T valueOfNil(Class<T> type)
NaN, zero, empty or null value of the given type. This method
tries to return the closest value that can be interpreted as "none", which
is usually not the same than "zero". More specifically:
float
or double), then this method returns Float.NaN or Double.NaN
depending on the given type.long, int, short, byte or char), then this
method returns the zero value of the given type.boolean primitive type, then this
method returns Boolean.FALSE.null.Numbers class, the scope of this method has been
extended to array and collection types because those objects can also be seen as
mathematical concepts.T - the compile-time type of the requested object.type - the type of the object for which to get a nil value.null.NilObjectpublic static byte getEnumConstant(Class<?> type)
BIG_DECIMAL, BIG_INTEGER,
DOUBLE, FLOAT, LONG, INTEGER,
SHORT, BYTE, CHARACTER, BOOLEAN, or OTHER
constants for the given type. This is a commodity for usage in switch statements.type - a type (usually either a primitive type or its wrapper).OTHER if unknown.Copyright © 2010–2017 The Apache Software Foundation. All rights reserved.