org.geotoolkit.util.converter
Class Numbers

Object
  extended by Static
      extended by Numbers

public final class Numbers
extends Static

Miscellaneous static methods working on Number objects, and a few primitive types by extension.

Since:
3.18 (derived from 2.5)
Version:
3.18
Author:
Martin Desruisseaux (IRD, Geomatys)
Module:
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
<N extends Number>
N
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
<T> T
valueOf(Class<T> type, String value)
          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 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

DOUBLE

public static final byte DOUBLE
Constants to be used in switch statements.

See Also:
Constant Field Values

FLOAT

public static final byte FLOAT
Constants to be used in switch statements.

See Also:
Constant Field Values

LONG

public static final byte LONG
Constants to be used in switch statements.

See Also:
Constant Field Values

INTEGER

public static final byte INTEGER
Constants to be used in switch statements.

See Also:
Constant Field Values

SHORT

public static final byte SHORT
Constants to be used in switch statements.

See Also:
Constant Field Values

BYTE

public static final byte BYTE
Constants to be used in switch statements.

See Also:
Constant Field Values

CHARACTER

public static final byte CHARACTER
Constants to be used in switch statements.

See Also:
Constant Field Values

BOOLEAN

public static final byte BOOLEAN
Constants to be used in switch statements.

See Also:
Constant Field Values

OTHER

public static final byte OTHER
Constants to be used in switch statements.

See Also:
Constant Field Values
Method Detail

isFloat

public static boolean isFloat(Class<?> type)
Returns true if the given type is a floating point type.

Parameters:
type - The type to test (may be null).
Returns:
true if type is the primitive or wrapper class of Float or Double.
See Also:
isInteger(Class)

isInteger

public static boolean isInteger(Class<?> type)
Returns true if the given type is an integer type. The integer types are Long, long, Integer, int, Short, short, Byte, byte and BigInteger.

Parameters:
type - The type to test (may be null).
Returns:
true if type is an integer type.
See Also:
isFloat(Class)

primitiveBitCount

public static int primitiveBitCount(Class<?> type)
                             throws IllegalArgumentException
Returns the number of bits used by primitive of the specified type. The given type must be a primitive type or its wrapper class.

Parameters:
type - The primitive type (may be null).
Returns:
The number of bits, or 0 if type is null.
Throws:
IllegalArgumentException - if the given type is unknown.

primitiveToWrapper

public static Class<?> primitiveToWrapper(Class<?> type)
Changes a primitive class to its wrapper (for example int to Integer). If the specified class is not a primitive type, then it is returned unchanged.

Parameters:
type - The primitive type (may be null).
Returns:
The type as a wrapper.
See Also:
wrapperToPrimitive(Class)

wrapperToPrimitive

public static Class<?> wrapperToPrimitive(Class<?> type)
Changes a wrapper class to its primitive (for example Integer to int). If the specified class is not a wrapper type, then it is returned unchanged.

Parameters:
type - The wrapper type (may be null).
Returns:
The type as a primitive.
See Also:
primitiveToWrapper(Class)

widestClass

public static Class<? extends Number> widestClass(Number n1,
                                                  Number n2)
                                           throws IllegalArgumentException
Returns the widest type of two numbers. Numbers 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.

Parameters:
n1 - The first number, or null.
n2 - The second number, or null.
Returns:
The widest type of the given numbers, or null if not n1 and n2 are null.
Throws:
IllegalArgumentException - If a number is not of a known type.
See Also:
widestClass(Number, Number), finestClass(Number, Number)

widestClass

public static Class<? extends Number> widestClass(Class<? extends Number> c1,
                                                  Class<? extends Number> c2)
                                           throws IllegalArgumentException
Returns the widest of the given types. Classes 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:

widestClass(Short.class, Long.class);
returns Long.class.

Parameters:
c1 - The first number type, or null.
c2 - The second number type, or null.
Returns:
The widest of the given types, or null if both c1 and c2 are null.
Throws:
IllegalArgumentException - If one of the given types is unknown.
See Also:
widestClass(Class, Class), finestClass(Number, Number)

finestClass

public static Class<? extends Number> finestClass(Number n1,
                                                  Number n2)
                                           throws IllegalArgumentException
Returns the finest type of two numbers. Numbers n1 and n2 must be instance of any of Byte, Short, Integer, Long, Float Double, BigInteger or BigDecimal types.

Parameters:
n1 - The first number.
n2 - The second number.
Returns:
The finest type of the given numbers.
Throws:
IllegalArgumentException - If a number is not of a known type.
See Also:
finestClass(Class, Class), widestClass(Class, Class)

finestClass

public static Class<? extends Number> finestClass(Class<? extends Number> c1,
                                                  Class<? extends Number> c2)
                                           throws IllegalArgumentException
Returns the finest of the given types. Classes 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:

finestClass(Short.class, Long.class);
returns Short.class.

Parameters:
c1 - The first number type, or null.
c2 - The second number type, or null.
Returns:
The finest of the given types, or null if both c1 and c2 are null.
Throws:
IllegalArgumentException - If one of the given types is unknown.
See Also:
finestClass(Number, Number), widestClass(Class, Class)

finestClass

public static Class<? extends Number> finestClass(Number value)
Returns the smallest class capable to hold the specified value. If the given value is null, then this method returns null. Otherwise this method delegates to finestClass(double) or finestClass(long) depending on the value type.

Parameters:
value - The value to be wrapped in a finer (if possible) Number.
Returns:
The finest type capable to hold the given value.
Since:
3.06
See Also:
finestNumber(Number)

finestClass

public static Class<? extends Number> finestClass(double value)
Returns the smallest class capable to hold the specified value. This is similar to finestClass(long), but extended to floating point values.

Parameters:
value - The value to be wrapped in a Number.
Returns:
The finest type capable to hold the given value.
See Also:
finestNumber(double)

finestClass

public static Class<? extends Number> finestClass(long value)
Returns the smallest class capable to hold the specified value. This method makes the following choice:

Parameters:
value - The value to be wrapped in a Number.
Returns:
The finest type capable to hold the given value.
Since:
3.00
See Also:
finestNumber(long)

finestNumber

public static Number finestNumber(Number value)
Returns the number of the smallest class capable to hold the specified value. If the given value is null, then this method returns null. Otherwise this method delegates to finestNumber(double) or finestNumber(long) depending on the value type.

Parameters:
value - The value to be wrapped in a finer (if possible) Number.
Returns:
The finest type capable to hold the given value.
Since:
3.06
See Also:
finestClass(Number)

finestNumber

public static Number finestNumber(double value)
Returns the number of the smallest class capable to hold the specified value. This is similar to finestNumber(long), but extended to floating point values.

Parameters:
value - The value to be wrapped in a Number.
Returns:
The finest type capable to hold the given value.
See Also:
finestClass(double)

finestNumber

public static Number finestNumber(long value)
Returns the number of the smallest type capable to hold the specified value. This method makes the following choice:

Parameters:
value - The value to be wrapped in a Number.
Returns:
The given value as a number of the finest type capable to hold it.
Since:
3.00
See Also:
finestClass(long)

finestNumber

public static Number finestNumber(String value)
                           throws NumberFormatException
Returns the smallest number capable to hold the specified value.

Parameters:
value - The value to be wrapped in a Number.
Returns:
The finest type capable to hold the given value.
Throws:
NumberFormatException - if the given value can not be parsed as a number.
Since:
3.00
See Also:
finestNumber(Number), finestNumber(double), finestNumber(long)

cast

public static <N extends Number> N cast(Number n,
                                        Class<N> c)
                             throws IllegalArgumentException
Casts a number to the specified class. The class must by one of Byte, Short, Integer, Long, Float or Double. This method makes the following choice:

Note: This method is intentionally restricted to primitive types. Other types like BigDecimal are not the purpose of this method. See the ConverterRegistry class for a more generic method.

Type Parameters:
N - The class to cast to.
Parameters:
n - The number to cast.
c - The destination type.
Returns:
The number casted to the given type.
Throws:
IllegalArgumentException - If the given type is unknown.

valueOf

public static <T> T valueOf(Class<T> type,
                            String value)
                 throws IllegalArgumentException,
                        NumberFormatException
Converts the specified string into a value object. The value object can be an instance of Double, Float, Long, Integer, Short, Byte, Boolean, Character or String according the specified type. This method makes the following choice:

Note: This method is intentionally restricted to primitive types, with the addition of String which can be though as an identity operation. Other types like BigDecimal are not the purpose of this method. See the ConverterRegistry class for a more generic method.

Type Parameters:
T - The requested type.
Parameters:
type - The requested type.
value - the value to parse.
Returns:
The value object, or null if value was null.
Throws:
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.

valueOfNil

public static <T> T valueOfNil(Class<T> type)
Returns a 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:

Despite being defined in the 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.

Type Parameters:
T - The compile-time type of the requested object.
Parameters:
type - The type of the object for which to get a nil value.
Returns:
An object of the given type which represents a nil value, or null.
Since:
3.18

getEnumConstant

public static byte getEnumConstant(Class<?> type)
Returns one of DOUBLE, FLOAT, LONG, INTEGER, SHORT, BYTE, CHARACTER, BOOLEAN or OTHER constants for the given type. This is a commodity for usage in switch statements.

Parameters:
type - A type (usually either a primitive type or its wrapper).
Returns:
The constant for the given type, or OTHER if unknown.


Copyright © 2009-2012 Geotoolkit.org. All Rights Reserved.