Package org.apache.sis.util
Class Numbers
Static methods working with 
Number objects, and a few primitive types by extension.- Since:
- 0.3
- See Also:
Defined in the sis-utility module
- 
Field SummaryFieldsModifier and TypeFieldDescriptionstatic final byteConstant of value 0xb used inswitchstatements or as index in arrays.static final byteConstant of value 0xa used inswitchstatements or as index in arrays.static final byteConstant of value 0x1 used inswitchstatements or as index in arrays.static final byteConstant of value 0x3 used inswitchstatements or as index in arrays.static final byteConstant of value 0x2 used inswitchstatements or as index in arrays.static final byteConstant of value 0x9 used inswitchstatements or as index in arrays.static final byteConstant of value 0x8 used inswitchstatements or as index in arrays.static final byteConstant of value 0x7 used inswitchstatements or as index in arrays.static final byteConstant of value 0x5 used inswitchstatements or as index in arrays.static final byteConstant of value 0x6 used inswitchstatements or as index in arrays.static final byteConstant of value 0x0 used inswitchstatements or as index in arrays.static final byteConstant of value 0x4 used inswitchstatements or as index in arrays.
- 
Method SummaryModifier and TypeMethodDescriptionstatic <N extends Number>
 NCasts a number to the specified type.static bytegetEnumConstant(Class<?> type) Returns a numeric constant for the given type.static booleanReturnstrueif the giventypeis a floating point type.static booleanisInteger(Class<?> type) Returnstrueif the giventypeis an integer type.static booleanReturnstrueif the given number is null or NaN.static booleanReturnstrueif the giventypeis a floating point or an integer type.narrowestClass(Class<? extends Number> c1, Class<? extends Number> c2) Returns the narrowest of the given types.narrowestClass(Number value) Returns the smallest class capable to hold the specified value.narrowestClass(Number n1, Number n2) Returns the narrowest type of two numbers.static NumbernarrowestNumber(Number value) Returns the given number wrapped in the smallest class capable to hold the specified value.static NumbernarrowestNumber(String value) Returns the smallest number capable to hold the specified value.static intprimitiveBitCount(Class<?> type) Returns the number of bits used by primitive of the specified type.static <N> Class<N>primitiveToWrapper(Class<N> type) Changes a primitive class to its wrapper (for exampleinttoInteger).static <T> TConverts the specified string into a value object.static <T> TvalueOfNil(Class<T> type) Returns aNaN, zero, empty ornullvalue of the given type.widestClass(Class<? extends Number> c1, Class<? extends Number> c2) Returns the widest of the given types.widestClass(Number n1, Number n2) Returns the widest type of two numbers.static <N extends Number>
 NWraps the given floating-point value in aNumberof the specified class.static <N extends Number>
 NWraps the given integer value in aNumberof the specified class.static <N> Class<N>wrapperToPrimitive(Class<N> type) Changes a wrapper class to its primitive (for exampleIntegertoint).
- 
Field Details- 
BIG_DECIMALpublic static final byte BIG_DECIMALConstant of value 0xb used inswitchstatements or as index in arrays. This enumeration provides the following guarantees (some Apache SIS codes rely on them):- OTHERvalue is 0.
- Primitive types are enumerated in this exact order
       (from lower value to higher value, but not necessarily as consecutive values):
       BYTE,SHORT,INTEGER,LONG,FLOAT,DOUBLE.
- java.mathtypes of greater capacity than primitive types (- BIG_DECIMALand- BIG_INTEGER) have higher enumeration values.
- Fractionis considered as a kind of floating point value.
 - See Also:
 
- 
BIG_INTEGERpublic static final byte BIG_INTEGERConstant of value 0xa used inswitchstatements or as index in arrays. This enumeration provides the following guarantees (some Apache SIS codes rely on them):- OTHERvalue is 0.
- Primitive types are enumerated in this exact order
       (from lower value to higher value, but not necessarily as consecutive values):
       BYTE,SHORT,INTEGER,LONG,FLOAT,DOUBLE.
- java.mathtypes of greater capacity than primitive types (- BIG_DECIMALand- BIG_INTEGER) have higher enumeration values.
- Fractionis considered as a kind of floating point value.
 - See Also:
 
- 
FRACTIONpublic static final byte FRACTIONConstant of value 0x7 used inswitchstatements or as index in arrays. This enumeration provides the following guarantees (some Apache SIS codes rely on them):- OTHERvalue is 0.
- Primitive types are enumerated in this exact order
       (from lower value to higher value, but not necessarily as consecutive values):
       BYTE,SHORT,INTEGER,LONG,FLOAT,DOUBLE.
- java.mathtypes of greater capacity than primitive types (- BIG_DECIMALand- BIG_INTEGER) have higher enumeration values.
- Fractionis considered as a kind of floating point value.
 - See Also:
 
- 
DOUBLEpublic static final byte DOUBLEConstant of value 0x9 used inswitchstatements or as index in arrays. This enumeration provides the following guarantees (some Apache SIS codes rely on them):- OTHERvalue is 0.
- Primitive types are enumerated in this exact order
       (from lower value to higher value, but not necessarily as consecutive values):
       BYTE,SHORT,INTEGER,LONG,FLOAT,DOUBLE.
- java.mathtypes of greater capacity than primitive types (- BIG_DECIMALand- BIG_INTEGER) have higher enumeration values.
- Fractionis considered as a kind of floating point value.
 - See Also:
 
- 
FLOATpublic static final byte FLOATConstant of value 0x8 used inswitchstatements or as index in arrays. This enumeration provides the following guarantees (some Apache SIS codes rely on them):- OTHERvalue is 0.
- Primitive types are enumerated in this exact order
       (from lower value to higher value, but not necessarily as consecutive values):
       BYTE,SHORT,INTEGER,LONG,FLOAT,DOUBLE.
- java.mathtypes of greater capacity than primitive types (- BIG_DECIMALand- BIG_INTEGER) have higher enumeration values.
- Fractionis considered as a kind of floating point value.
 - See Also:
 
- 
LONGpublic static final byte LONGConstant of value 0x6 used inswitchstatements or as index in arrays. This enumeration provides the following guarantees (some Apache SIS codes rely on them):- OTHERvalue is 0.
- Primitive types are enumerated in this exact order
       (from lower value to higher value, but not necessarily as consecutive values):
       BYTE,SHORT,INTEGER,LONG,FLOAT,DOUBLE.
- java.mathtypes of greater capacity than primitive types (- BIG_DECIMALand- BIG_INTEGER) have higher enumeration values.
- Fractionis considered as a kind of floating point value.
 - See Also:
 
- 
INTEGERpublic static final byte INTEGERConstant of value 0x5 used inswitchstatements or as index in arrays. This enumeration provides the following guarantees (some Apache SIS codes rely on them):- OTHERvalue is 0.
- Primitive types are enumerated in this exact order
       (from lower value to higher value, but not necessarily as consecutive values):
       BYTE,SHORT,INTEGER,LONG,FLOAT,DOUBLE.
- java.mathtypes of greater capacity than primitive types (- BIG_DECIMALand- BIG_INTEGER) have higher enumeration values.
- Fractionis considered as a kind of floating point value.
 - See Also:
 
- 
SHORTpublic static final byte SHORTConstant of value 0x4 used inswitchstatements or as index in arrays. This enumeration provides the following guarantees (some Apache SIS codes rely on them):- OTHERvalue is 0.
- Primitive types are enumerated in this exact order
       (from lower value to higher value, but not necessarily as consecutive values):
       BYTE,SHORT,INTEGER,LONG,FLOAT,DOUBLE.
- java.mathtypes of greater capacity than primitive types (- BIG_DECIMALand- BIG_INTEGER) have higher enumeration values.
- Fractionis considered as a kind of floating point value.
 - See Also:
 
- 
BYTEpublic static final byte BYTEConstant of value 0x3 used inswitchstatements or as index in arrays. This enumeration provides the following guarantees (some Apache SIS codes rely on them):- OTHERvalue is 0.
- Primitive types are enumerated in this exact order
       (from lower value to higher value, but not necessarily as consecutive values):
       BYTE,SHORT,INTEGER,LONG,FLOAT,DOUBLE.
- java.mathtypes of greater capacity than primitive types (- BIG_DECIMALand- BIG_INTEGER) have higher enumeration values.
- Fractionis considered as a kind of floating point value.
 - See Also:
 
- 
CHARACTERpublic static final byte CHARACTERConstant of value 0x2 used inswitchstatements or as index in arrays. This enumeration provides the following guarantees (some Apache SIS codes rely on them):- OTHERvalue is 0.
- Primitive types are enumerated in this exact order
       (from lower value to higher value, but not necessarily as consecutive values):
       BYTE,SHORT,INTEGER,LONG,FLOAT,DOUBLE.
- java.mathtypes of greater capacity than primitive types (- BIG_DECIMALand- BIG_INTEGER) have higher enumeration values.
- Fractionis considered as a kind of floating point value.
 - See Also:
 
- 
BOOLEANpublic static final byte BOOLEANConstant of value 0x1 used inswitchstatements or as index in arrays. This enumeration provides the following guarantees (some Apache SIS codes rely on them):- OTHERvalue is 0.
- Primitive types are enumerated in this exact order
       (from lower value to higher value, but not necessarily as consecutive values):
       BYTE,SHORT,INTEGER,LONG,FLOAT,DOUBLE.
- java.mathtypes of greater capacity than primitive types (- BIG_DECIMALand- BIG_INTEGER) have higher enumeration values.
- Fractionis considered as a kind of floating point value.
 - See Also:
 
- 
OTHERpublic static final byte OTHERConstant of value 0x0 used inswitchstatements or as index in arrays. This enumeration provides the following guarantees (some Apache SIS codes rely on them):- OTHERvalue is 0.
- Primitive types are enumerated in this exact order
       (from lower value to higher value, but not necessarily as consecutive values):
       BYTE,SHORT,INTEGER,LONG,FLOAT,DOUBLE.
- java.mathtypes of greater capacity than primitive types (- BIG_DECIMALand- BIG_INTEGER) have higher enumeration values.
- Fractionis considered as a kind of floating point value.
 - See Also:
 
 
- 
- 
Method Details- 
isFloatReturnstrueif the giventypeis a floating point type. The floating point types areFloat,float,Double,doubleandBigDecimal.Fractionis also considered as a kind of floating point values.- Parameters:
- type- the primitive type or wrapper class to test (can be- null).
- Returns:
- trueif- typeis one of the known types capable to represent floating point numbers.
- See Also:
 
- 
isIntegerReturnstrueif the giventypeis an integer type. The integer types areByte,byte,Short,short,Integer,int,Long,longandBigInteger.- Parameters:
- type- the primitive type or wrapper class to test (can be- null).
- Returns:
- trueif- typeis an integer type.
- See Also:
 
- 
isNumberReturnstrueif the giventypeis a floating point or an integer type. This method returnstrueif eitherisFloat(Class)orisInteger(Class)returnstruefor the given argument, or if the type is assignable toNumber.- Parameters:
- type- the primitive type or wrapper class to test (can be- null).
- Returns:
- trueif- typeis a- Numberor a primitive floating point or integer type.
- Since:
- 1.1
- See Also:
 
- 
isNaNReturnstrueif the given number is null or NaN. Current implementation recognizesFloatandDoubletypes.- Parameters:
- value- the number to test (may be- null).
- Returns:
- trueif the given number is null or NaN.
- Since:
- 1.1
- See Also:
 
- 
primitiveBitCountReturns 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 (can be- null).
- Returns:
- the number of bits, or 0 if typeis null.
- Throws:
- IllegalArgumentException- if the given type is not one of the types supported by this- Numbersclass.
 
- 
primitiveToWrapperChanges a primitive class to its wrapper (for exampleinttoInteger). If the specified class is not a primitive type, then it is returned unchanged.- Type Parameters:
- N- the primitive and wrapper type (both have the same parametric declaration).
- Parameters:
- type- the primitive type (can be- null).
- Returns:
- the type as a wrapper.
- See Also:
 
- 
wrapperToPrimitiveChanges a wrapper class to its primitive (for exampleIntegertoint). If the specified class is not a wrapper type, then it is returned unchanged.- Type Parameters:
- N- the primitive and wrapper type (both have the same parametric declaration).
- Parameters:
- type- the wrapper type (can be- null).
- Returns:
- the type as a primitive.
- See Also:
 
- 
widestClasspublic static Class<? extends Number> widestClass(Number n1, Number n2) throws IllegalArgumentException Returns the widest type of two numbers. Numbersn1andn2can be instance ofByte,Short,Integer,Long,Float,Double,Fraction,BigIntegerorBigDecimaltypes.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 nullif bothn1andn2are null.
- Throws:
- IllegalArgumentException- if a number is not an instance of a supported type.
- See Also:
 
- 
widestClasspublic static Class<? extends Number> widestClass(Class<? extends Number> c1, Class<? extends Number> c2) throws IllegalArgumentException Returns the widest of the given types. Classesc1andc2can beByte,Short,Integer,Long,Float,Double,Fraction,BigIntegerorBigDecimaltypes.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: in the following code,typeis set toLong.class:Class<?> type = widestClass(Short.class, 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 nullif bothc1andc2are null.
- Throws:
- IllegalArgumentException- if one of the given types is not supported by this- Numbersclass.
- See Also:
 
- 
narrowestClasspublic static Class<? extends Number> narrowestClass(Number n1, Number n2) throws IllegalArgumentException Returns the narrowest type of two numbers. Numbersn1andn2can be instance ofByte,Short,Integer,Long,Float,Double,Fraction,BigIntegerorBigDecimaltypes.- Parameters:
- n1- the first number, or- null.
- n2- the second number, or- null.
- Returns:
- the narrowest type of the given numbers, or nullif bothn1andn2are null.
- Throws:
- IllegalArgumentException- if a number is not an instance of a supported type.
- See Also:
 
- 
narrowestClasspublic static Class<? extends Number> narrowestClass(Class<? extends Number> c1, Class<? extends Number> c2) throws IllegalArgumentException Returns the narrowest of the given types. Classesc1andc2can beByte,Short,Integer,Long,Float,Double,Fraction,BigIntegerorBigDecimaltypes.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: in the following code,typeis set toShort.class:Class<?> type = widestClass(Short.class, Long.class); - Parameters:
- c1- the first number type, or- null.
- c2- the second number type, or- null.
- Returns:
- the narrowest of the given types, or nullif bothc1andc2are null.
- Throws:
- IllegalArgumentException- if one of the given types is not supported by this- Numbersclass.
- See Also:
 
- 
narrowestClassReturns the smallest class capable to hold the specified value. This method applies the following choices, in that order:- If the given value is null, then this method returnsnull.
- Otherwise if the given value cannot be casted from doubleto another type without precision lost, returnDouble.class.
- Otherwise if the given value cannot be casted from floatto another type without precision lost, returnFloat.class.
- Otherwise if the given value is between 0x80 and
       0x7f, then this method returns Byte.class;
- Otherwise if the given value is between -32768 and
       32767, then this method returns Short.class;
- Otherwise if the given value is between -2147483648 and
       2147483647, then this method returns Integer.class;
- Otherwise this method returns Long.class;
 - Parameters:
- value- the value to be wrapped in a finer (if possible)- Number.
- Returns:
- the narrowest type capable to hold the given value.
- See Also:
 
- If the given value is 
- 
narrowestNumberReturns the given number wrapped in the smallest class capable to hold the specified value. This method is equivalent to the following code, in a slightly more efficient way:return cast(value, narrowestClass(value)); - Parameters:
- value- the value to be wrapped in a finer (if possible)- Number.
- Returns:
- the narrowest type capable to hold the given value.
- See Also:
 
- 
narrowestNumberReturns the smallest number capable to hold the specified value.- Parameters:
- value- the value to be wrapped in a- Number.
- Returns:
- the narrowest type capable to hold the given value.
- Throws:
- NumberFormatException- if the given value cannot be parsed as a number.
- See Also:
 
- 
castpublic static <N extends Number> N cast(Number number, Class<N> type) throws IllegalArgumentException Casts a number to the specified type. The target type can be one ofByte,Short,Integer,Long,Float,Double,Fraction,BigIntegerorBigDecimal. This method makes the following choice:- If the given value is nullor an instance of the given type, then it is returned unchanged.
- Otherwise if the given type is Double.class, then this method returnsDouble.valueOf(number.doubleValue());
- Otherwise if the given type is Float.class, then this method returnsFloat.valueOf(number.floatValue());
- And likewise for all remaining known types.
 widestClass(Class, Class)ornarrowestClass(Number). If nevertheless the given type is not wide enough, then the behavior depends on the implementation of the correspondingNumber.fooValue()method - typically, the value is just rounded or truncated.- Type Parameters:
- N- the class to cast to.
- Parameters:
- number- the number to cast, or- null.
- type- the destination type.
- Returns:
- the number casted to the given type, or nullif the given value was null.
- Throws:
- IllegalArgumentException- if the given type is not supported by this- Numbersclass, or the number cannot be converted to the specified type (e.g.- Double.NaNcannot be converted to- BigDecimal).
 
- If the given value is 
- 
wrappublic static <N extends Number> N wrap(double value, Class<N> type) throws IllegalArgumentException Wraps the given floating-point value in aNumberof the specified class. The given type shall be one ofByte,Short,Integer,Long,Float,Double,Fraction,BigIntegerandBigDecimalclasses. Furthermore, the given value shall be convertible to the given class without precision lost, otherwise anIllegalArgumentExceptionwill be thrown.- Type Parameters:
- N- the wrapper class.
- Parameters:
- value- the value to wrap.
- type- the desired wrapper class.
- Returns:
- the value wrapped in an object of the given class.
- Throws:
- IllegalArgumentException- if the given type is not supported by this- Numbersclass, or if the given value cannot be wrapped in an instance of the given class without precision lost.
 
- 
wrapWraps the given integer value in aNumberof the specified class. The given type shall be one ofByte,Short,Integer,Long,Float,Double,Fraction,BigIntegerandBigDecimalclasses. Furthermore, the given value shall be convertible to the given class without precision lost, otherwise anIllegalArgumentExceptionwill be thrown.- Type Parameters:
- N- the wrapper class.
- Parameters:
- value- the value to wrap.
- type- the desired wrapper class.
- Returns:
- the value wrapped in an object of the given class.
- Throws:
- IllegalArgumentException- if the given type is not supported by this- Numbersclass, or if the given value cannot be wrapped in an instance of the given class without precision lost.
- Since:
- 0.8
 
- 
valueOfpublic static <T> T valueOf(String value, Class<T> type) throws IllegalArgumentException, NumberFormatException Converts the specified string into a value object. The value object can be an instance ofBigDecimal,BigInteger,Fraction,Double,Float,Long,Integer,Short,Byte,Boolean,CharacterorStringaccording the specified type. This method makes the following choice:- If the given type is Double.class, then this method returnsDouble.valueOf(value);
- If the given type is Float.class, then this method returnsFloat.valueOf(value);
- And likewise for all remaining known types.
 - Type Parameters:
- T- the requested type.
- Parameters:
- value- the value to parse.
- type- the requested type.
- Returns:
- the value object, or nullifvaluewas null.
- Throws:
- IllegalArgumentException- if- typeis not a recognized type.
- NumberFormatException- if- typeis a subclass of- Numberand the string value is not parsable as a number of the specified type.
 
- If the given type is 
- 
valueOfNilReturns aNaN, zero, empty ornullvalue 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:- If the given type is a floating point primitive type (floatordouble), then this method returnsFloat.NaNorDouble.NaNdepending on the given type.
- If the given type is an integer primitive type or the character type
       (long,int,short,byteorchar), then this method returns the zero value of the given type.
- If the given type is the booleanprimitive type, then this method returnsBoolean.FALSE.
- If the given type is an array or a collection, then this method returns an empty array or collection. The given type is honored on a best effort basis.
- For all other cases, including the wrapper classes of primitive types, this method
       returns null.
 Numbersclass, the scope of this method has been extended to array and collection types because those objects can also be seen as mathematical concepts.- 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.
- See Also:
 
- If the given type is a floating point primitive type (
- 
getEnumConstantReturns a numeric constant for the given type. The constants areBIG_DECIMAL,BIG_INTEGER,FRACTION,DOUBLE,FLOAT,LONG,INTEGER,SHORT,BYTE,CHARACTER,BOOLEAN, orOTHERconstants for the given type. This is a commodity for usage inswitchstatements.- Parameters:
- type- a type (usually either a primitive type or its wrapper), or- null.
- Returns:
- the constant for the given type, or OTHERif unknown.
 
 
-