|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
ObjectStatic
Numbers
public final class Numbers
Miscellaneous static methods working on Number objects, and a few primitive types
by extension.
| utility/geotk-utility (download) | View source code for this class |
| Field Summary | |
|---|---|
static byte |
BOOLEAN
Constants to be used in switch statements. |
static byte |
BYTE
Constants to be used in switch statements. |
static byte |
CHARACTER
Constants to be used in switch statements. |
static byte |
DOUBLE
Constants to be used in switch statements. |
static byte |
FLOAT
Constants to be used in switch statements. |
static byte |
INTEGER
Constants to be used in switch statements. |
static byte |
LONG
Constants to be used in switch statements. |
static byte |
OTHER
Constants to be used in switch statements. |
static byte |
SHORT
Constants to be used in switch statements. |
| Method Summary | ||
|---|---|---|
static
|
cast(Number n,
Class<N> c)
Casts a number to the specified class. |
|
static Class<? extends Number> |
finestClass(Class<? extends Number> c1,
Class<? extends Number> c2)
Returns the finest of the given types. |
|
static Class<? extends Number> |
finestClass(double value)
Returns the smallest class capable to hold the specified value. |
|
static Class<? extends Number> |
finestClass(long value)
Returns the smallest class capable to hold the specified value. |
|
static Class<? extends Number> |
finestClass(Number value)
Returns the smallest class capable to hold the specified value. |
|
static Class<? extends Number> |
finestClass(Number n1,
Number n2)
Returns the finest type of two numbers. |
|
static Number |
finestNumber(double value)
Returns the number of the smallest class capable to hold the specified value. |
|
static Number |
finestNumber(long value)
Returns the number of the smallest type capable to hold the specified value. |
|
static Number |
finestNumber(Number value)
Returns the number of the smallest class capable to hold the specified value. |
|
static Number |
finestNumber(String value)
Returns the smallest number capable to hold the specified value. |
|
static byte |
getEnumConstant(Class<?> type)
Returns one of DOUBLE, FLOAT, LONG, INTEGER,
SHORT, BYTE, CHARACTER, BOOLEAN or OTHER
constants 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 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
|
valueOf(Class<T> type,
String value)
Converts the specified string into a value object. |
|
static
|
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 Class<?> |
wrapperToPrimitive(Class<?> type)
Changes a wrapper class to its primitive (for example Integer to int). |
|
| Methods inherited from class Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
|---|
public static final byte DOUBLE
switch statements.
public static final byte FLOAT
switch statements.
public static final byte LONG
switch statements.
public static final byte INTEGER
switch statements.
public static final byte SHORT
switch statements.
public static final byte BYTE
switch statements.
public static final byte CHARACTER
switch statements.
public static final byte BOOLEAN
switch statements.
public static final byte OTHER
switch statements.
| Method Detail |
|---|
public static boolean isFloat(Class<?> type)
true if the given type is a floating point type.
type - The type to test (may be null).
true if type is the primitive or wrapper class of
Float or Double.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),
finestClass(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.
Example:
returnswidestClass(Short.class, Long.class);
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),
finestClass(Number, Number)
public static Class<? extends Number> finestClass(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.finestClass(Class, Class),
widestClass(Class, Class)
public static Class<? extends Number> finestClass(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.
Example:
returnsfinestClass(Short.class, Long.class);
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.finestClass(Number, Number),
widestClass(Class, Class)public static Class<? extends Number> finestClass(Number value)
null, then this method returns null. Otherwise this method delegates
to finestClass(double) or finestClass(long) depending on the value type.
value - The value to be wrapped in a finer (if possible) Number.
finestNumber(Number)public static Class<? extends Number> finestClass(double value)
finestClass(long), but extended to floating point values.
value - The value to be wrapped in a Number.
finestNumber(double)public static Class<? extends Number> finestClass(long value)
Byte.class;Short.class;Integer.class;Long.class;
value - The value to be wrapped in a Number.
finestNumber(long)public static Number finestNumber(Number value)
null, then this method returns null. Otherwise this
method delegates to finestNumber(double) or finestNumber(long)
depending on the value type.
value - The value to be wrapped in a finer (if possible) Number.
finestClass(Number)public static Number finestNumber(double value)
finestNumber(long), but extended to floating point values.
value - The value to be wrapped in a Number.
finestClass(double)public static Number finestNumber(long value)
Byte object.Short object.Integer object.Long object.
value - The value to be wrapped in a Number.
finestClass(long)
public static Number finestNumber(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.finestNumber(Number),
finestNumber(double),
finestNumber(long)
public static <N extends Number> N cast(Number n,
Class<N> c)
throws IllegalArgumentException
Byte,
Short, Integer, Long, Float or Double.
This method makes the following choice:
Double.class, then this method returns
Double.valueOf(n.doubleValue());Float.class, then this method returns
Float.valueOf(n.floatValue());Note: This method is intentionally restricted to primitive types. Other types likeBigDecimalare not the purpose of this method. See theConverterRegistryclass for a more generic method.
N - The class to cast to.n - The number to cast.c - The destination type.
IllegalArgumentException - If the given type is unknown.
public static <T> T valueOf(Class<T> type,
String value)
throws IllegalArgumentException,
NumberFormatException
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);Note: This method is intentionally restricted to primitive types, with the addition ofStringwhich can be though as an identity operation. Other types likeBigDecimalare not the purpose of this method. See theConverterRegistryclass for a more generic method.
T - The requested type.type - The requested type.value - the value to parse.
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 parseable 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:
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 types can also be seen as mathematical
objects.
T - The compile-time type of the requested object.type - The type of the object for which to get a nil value.
null.public static byte getEnumConstant(Class<?> type)
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.
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||