Enum Class NumberType
- All Implemented Interfaces:
Serializable, Comparable<NumberType>, Constable
Type of numbers recognized by Apache SIS, together with primitive types by extension.
This enumeration includes the standard Java numeric types together with subclasses of
Number
provided by SIS.
Order
Loosely speaking, enumeration values are ordered from narrowest type (withVOID viewed
as if it was a numeric type of size 0) to widest type, with types of variable size ordered last.
Some ambiguity exist, in particular between FRACTION, LONG and DOUBLE.
Nevertheless, this enumeration provides the following guarantees:
- The ordinal value of the
VOIDvalue is 0. - Primitive types are enumerated in this exact order, but not necessarily as consecutive values:
BYTE,SHORT,INTEGER,LONG,FLOAT,DOUBLE. BIG_INTEGERandBIG_DECIMALare ordered after primitive types.
- Since:
- 1.6
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from class Enum
Enum.EnumDesc<E> -
Enum Constant Summary
Enum ConstantsEnum ConstantDescriptionTheBigDecimalclass.TheBigIntegerclass.Thebooleanprimitive type or its wrapper class.Thebyteprimitive type or its wrapper class.Thecharprimitive type or its wrapper class.Thedoubleprimitive type or its wrapper class.Thefloatprimitive type or its wrapper class.TheFractionclass.Theintprimitive type or its wrapper class.Thelongprimitive type or its wrapper class.The null type, used when aClassargument isnull.AnyNumberclass not recognized by Apache SIS.Theshortprimitive type or its wrapper class.The void type, viewed as if it was a numeric type with a size of 0 bit. -
Method Summary
Modifier and TypeMethodDescriptionCasts a value to the specified type without checking whether is would cause precision lost.final Class<?> classOfValues(boolean primitive) Returns the primitive or wrapper class represented by this enumeration value.static Optional<NumberType> Returns the enumeration value for the given class.static Optional<NumberType> forClasses(Class<?>... types) Returns an enumeration value capable to represent at least approximately all the given classes.static NumberTypeforNumberClass(Class<?> type) Returns the enumeration value for the given numeric class.static NumberTypeforNumberClasses(Class<?>... types) Returns an enumeration value capable to represent at least approximately all the given numeric classes.booleanisConversionLossless(NumberType target) Returns whether conversion of values from this type to the specified type would be lossless.final booleanReturns whether this type supports thecast(…)andwrap(…)methods.final booleanReturnstrueif this type is capable to store fractional digits.static booleanisFractional(Class<?> type) Returnstrueif the argument is one of the types known to SIS as capable to store fractional digits.final booleanReturnstrueif this type is an integer type.static booleanisInteger(Class<?> type) Returnstrueif the argument is one of the integer types known to SIS.booleanisNarrowerThan(NumberType other) Returns whether this type is considered narrower than the specified type.final booleanisReal()Returnstrueif this type is any type of real numbers.static booleanReturnstrueif the argument is one of the real number types known to SIS.booleanisWiderThan(NumberType other) Returns whether this type is considered wider than the specified type.Comparable<?> Returns the closest value that can be interpreted asno data
, which is not the same as zero.Comparable<?> Converts the specified string into a value object.static <N> Class<N> primitiveToWrapper(Class<N> type) Changes a primitive class to its wrapper (for exampleinttoInteger).final OptionalIntsize()Returns the number of bits used in the representation of this type.static NumberTypeReturns the enum constant of this class with the specified name.static NumberType[]values()Returns an array containing the constants of this enum class, in the order they are declared.wrapExact(double number) Wraps the given floating-point value in an object of the type represented by this enumeration value.wrapExact(long number) Wraps the given integer value in an object of the type represented by this enumeration value.static <N> Class<N> wrapperToPrimitive(Class<N> type) Changes a wrapper class to its primitive (for exampleIntegertoint).Methods inherited from class Enum
clone, compareTo, describeConstable, equals, finalize, getDeclaringClass, hashCode, name, ordinal, toString, valueOf
-
Enum Constant Details
-
VOID
The void type, viewed as if it was a numeric type with a size of 0 bit. -
BOOLEAN
Thebooleanprimitive type or its wrapper class. This is sometime handled as an integer value which accepts only the values 0 and 1. This is considered as a type with a size of 1 bit. -
BYTE
Thebyteprimitive type or its wrapper class. -
CHARACTER
Thecharprimitive type or its wrapper class. This is sometime handled as a 16 bits unsigned integer value. This enumeration value is defined for completeness but is rarely used by SIS. -
SHORT
Theshortprimitive type or its wrapper class. -
INTEGER
Theintprimitive type or its wrapper class. -
FRACTION
-
LONG
Thelongprimitive type or its wrapper class. -
FLOAT
Thefloatprimitive type or its wrapper class. -
DOUBLE
Thedoubleprimitive type or its wrapper class. -
BIG_INTEGER
TheBigIntegerclass. -
BIG_DECIMAL
TheBigDecimalclass. -
NUMBER
AnyNumberclass not recognized by Apache SIS. -
NULL
The null type, used when aClassargument isnull. This is the only enumeration value for whichclassOfValues(boolean)returnsnull.
-
-
Method Details
-
values
Returns an array containing the constants of this enum class, in the order they are declared.- Returns:
- an array containing the constants of this enum class, in the order they are declared
-
valueOf
Returns the enum constant of this class with the specified name. The string must match exactly an identifier used to declare an enum constant in this class. (Extraneous whitespace characters are not permitted.)- Parameters:
name- the name of the enum constant to be returned.- Returns:
- the enum constant with the specified name
- Throws:
IllegalArgumentException- if this enum class has no constant with the specified nameNullPointerException- if the argument is null
-
forClass
-
forNumberClass
Returns the enumeration value for the given numeric class. This method is equivalent toforClass(type).orElseThrow()except that the exception is anIllegalArgumentException. The exception is never thrown if the given class is a primitive type, its wrapper, or a class assignable toNumber.- Parameters:
type- the primitive type orNumberclass for which to get an enumeration value, ornull.- Returns:
- the enumeration value for the given type. Never
nullbut may beNULL. - Throws:
IllegalArgumentException- if the given class cannot be mapped to any enumeration value.
-
forClasses
Returns an enumeration value capable to represent at least approximately all the given classes. Null elements are ignored. If the given array is null or does not contain at least one non-null element, thenNULLis returned. If the array contains at least one element that cannot be mapped to an enumeration value, then an empty value is returned.Accuracy
This method does not guarantee that conversions from the given classes to the returned type will be lossless. For example, conversions formlongtodoubleare not lossless. However, the results of those conversions should be reasonable approximations. -
forNumberClasses
Returns an enumeration value capable to represent at least approximately all the given numeric classes. This method is equivalent toexcept that the exception is an(types).orElseThrow()invalid reference
forClass
IllegalArgumentException. The exception is never thrown if all the given classes are primitive types, their wrappers, or classes assignable toNumber.- Parameters:
types- the primitive types orNumberclasses for which to get a common enumeration value.- Returns:
- the enumeration value for the given types. Never
nullbut may beNULL. - Throws:
IllegalArgumentException- if one of the given classes cannot be mapped to any enumeration value.
-
isInteger
public final boolean isInteger()Returnstrueif this type is an integer type. Those types areBYTE,SHORT,INTEGER,LONGandBIG_INTEGER.- Returns:
- whether this type is an integer type.
-
isInteger
Returnstrueif the argument is one of the integer types known to SIS. Those types arebyte,Byte,short,Short,int,Integer,long,LongandBigInteger.This method is mutually exclusive with
isFractional(): only one of those two methods may returntrue. However, both methods may returnfalse.- Parameters:
type- the primitive type orNumbersubclass to test (can benull).- Returns:
trueiftypeis one of the known types used to represent integers.
-
isFractional
public final boolean isFractional()Returnstrueif this type is capable to store fractional digits. Those types areFRACTION,FLOAT,DOUBLEandBIG_DECIMAL. Floating point types are considered as types or rational numbers where the denominator is a power of 2 or 10.This method is mutually exclusive with
isInteger(): only one of those two methods may returntrue. However, both methods may returnfalse.- Returns:
- whether this type is a rational or floating point type.
-
isFractional
Returnstrueif the argument is one of the types known to SIS as capable to store fractional digits. This method returnstruefor the following types:Fraction,float,Float,double,DoubleandBigDecimal.- Parameters:
type- the primitive type orNumbersubclass to test (can benull).- Returns:
trueiftypeis one of the known types capable to store fractional digits.
-
isReal
public final boolean isReal()Returnstrueif this type is any type of real numbers. This method returnstruefor all integer and fractional types.- Returns:
- whether this type is any type of real numbers.
-
isConvertible
public final boolean isConvertible()Returns whether this type supports thecast(…)andwrap(…)methods. This is true for all real numbers exceptNUMBER.- Returns:
- whether this type supports the
cast(…)andwrap(…)methods.
-
isReal
Returnstrueif the argument is one of the real number types known to SIS. This method returnstruefor all integer and fractional types.- Parameters:
type- the primitive type orNumbersubclass to test (can benull).- Returns:
trueiftypeis one of the known types used to represent real numbers.
-
isNarrowerThan
Returns whether this type is considered narrower than the specified type. If one of the type is unknown, then this method conservatively returnsfalse.Accuracy
Note that conversions from the narrowest class are not guaranteed to be lossless. For example,longis considered a narrower class thandouble, but conversion fromlongtodoublecan nevertheless loose accuracy. However, if this method returnstrue, then conversions toothertype should produce at least a reasonable approximation of the original value.- Parameters:
other- the other type.- Returns:
- whether this type is considered narrower than the specified type.
-
isWiderThan
Returns whether this type is considered wider than the specified type. If one of the type is unknown, then this method conservatively returnsfalse.Accuracy
Note that conversions to the widest class are not guaranteed to be lossless. For example,doubleis considered a wider class thanlong, but conversion fromlongtodoublecan nevertheless loose accuracy. However, if this method returnstrue, then conversions to this type should produce at least a reasonable approximation of the original value.- Parameters:
other- the other type.- Returns:
- whether this type is considered wider than the specified type.
-
isConversionLossless
Returns whether conversion of values from this type to the specified type would be lossless. If this type supports negative zero and NaN values, then the target type must also supports these values for the conversion to be considered lossless.- Parameters:
target- the target type.- Returns:
- whether conversions to the target type would be lossless, including negative zero and NaN.
-
classOfValues
Returns the primitive or wrapper class represented by this enumeration value. If there is no primitive type, then this method always returns the class. The returned class is null in the particular case of theNULLtype, and non-null for all other types.- Parameters:
primitive-truefor the primitive type, orfalsefor the wrapper class.- Returns:
- the primitive or wrapper class represented by this enumeration value, or
null. - See Also:
-
primitiveToWrapper
Changes 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 benull).- Returns:
- the type as a wrapper.
- See Also:
-
wrapperToPrimitive
Changes 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 benull).- Returns:
- the type as a primitive.
- See Also:
-
size
Returns the number of bits used in the representation of this type. This size is accurate for values objects such as primitive types. For other objects, the returned size does not include the space occupied by object headers and object references.- Returns:
- the number of bits, or empty if the size is unknown or variable.
-
cast
Casts a value to the specified type without checking whether is would cause precision lost. This method makes the following choice:- If this type is
DOUBLE, then this method returnsDouble.valueOf(number.doubleValue()); - Otherwise if this type is
FLOAT, then this method returnsFloat.valueOf(number.floatValue()); - And likewise for other types in this enumeration.
Number.fooValue()method - typically, the value is just rounded or truncated.- Parameters:
number- the number to cast, ornull.- Returns:
- the number cast to this type, or
nullif the given number was null. - Throws:
UnsupportedOperationException- if this type cannot cast numbers.NullPointerException- ifnumberis null.IllegalArgumentException- if the number cannot be converted to this type (e.g.,Double.NaNcannot be converted toBigDecimal).- See Also:
- If this type is
-
wrapExact
Wraps the given floating-point value in an object of the type represented by this enumeration value. The returned value is an instance of the wrapper class returned by. The given value shall be convertible to that type without precision lost, otherwise anclassOfValues(false)ArithmeticExceptionwill be thrown.This method does not guaranteed that the sign of zero is preserved (i.e., negative zero is considered equivalent to positive zero) and does not guaranteed that the various NaN values are preserved (i.e., NaN may be replaced by the canonical
Double.NaN).- Parameters:
number- the number to wrap.- Returns:
- the value wrapped in an object of the wrapper class represented by this enumeration value.
- Throws:
UnsupportedOperationException- if this type cannot wrap numbers.ArithmeticException- if the given value cannot be converted without precision lost.- See Also:
-
wrapExact
Wraps the given integer value in an object of the type represented by this enumeration value. The returned value is an instance of the wrapper class returned by. The given value shall be convertible to that type without precision lost, otherwise anclassOfValues(false)ArithmeticExceptionwill be thrown.- Parameters:
number- the number to wrap.- Returns:
- the value wrapped in an object of the wrapper class represented by this enumeration value.
- Throws:
UnsupportedOperationException- if this type cannot wrap numbers.ArithmeticException- if the given value cannot be converted without precision lost.- See Also:
-
parse
Converts the specified string into a value object. The returned value is an instance of the wrapper class returned by.classOfValues(false)- If this type is
DOUBLE, then this method returnsDouble.valueOf(value); - If this type is
FLOAT, then this method returnsFloat.valueOf(value); - And likewise for all other known types.
- Parameters:
text- the value to parse.- Returns:
- the value object.
- Throws:
UnsupportedOperationException- if this type does not support parsing.NullPointerException- iftextis null.IllegalArgumentException- iftextis not parsable as a value of this type.- See Also:
- If this type is
-
nilValue
Returns the closest value that can be interpreted asno data
, which is not the same as zero. This method returnsNaN,0orfalseornull, in this preference order. The returned value is an instance of the wrapper class returned by.classOfValues(false)- Returns:
- the
NaN,0orfalseornullvalue, in this preference order. - See Also:
-