public enum StandardNumberType extends Enum<StandardNumberType> implements NumberType
NumberType implementations for standard Java number types: byte, short, integer, long, float, double,
BigInteger and BigDecimal. Allows to convert from one type to another and to compare the types' value ranges
among each other.
To use a number type with type safety, please use the constants TYPE_BYTE, TYPE_SHORT etc.
fromClass(Class),
NumberType| Enum Constant and Description |
|---|
BIG_DECIMAL
Big decimal: supports decimals and has a theoretically infinite range of supported values.
|
BIG_INTEGER
Big integer: integer with a theoretically infinite range of supported values.
|
BYTE
Byte: [-128, 127].
|
DOUBLE
Double: [-1.7976931348623157E308, 1.7976931348623157E308].
|
FLOAT
Float: [-3.4028235E38, 3.4028235E38].
|
INTEGER
Integer: [-2147483648, 2147483647].
|
LONG
Long: [-9223372036854775808, 9223372036854775807].
|
SHORT
Short: [-32768, 32767].
|
| Modifier and Type | Field and Description |
|---|---|
static NumberType<BigDecimal> |
TYPE_BIG_DECIMAL |
static NumberType<BigInteger> |
TYPE_BIG_INTEGER |
static NumberType<Byte> |
TYPE_BYTE |
static NumberType<Double> |
TYPE_DOUBLE |
static NumberType<Float> |
TYPE_FLOAT |
static NumberType<Integer> |
TYPE_INTEGER |
static NumberType<Long> |
TYPE_LONG |
static NumberType<Short> |
TYPE_SHORT |
| Modifier and Type | Method and Description |
|---|---|
ValueRangeComparison |
compareToValueRange(Number number)
Compares the given value to this type's value range, indicating whether it can be represented by this type
without loss of magnitude, or why it cannot.
|
Optional<Number> |
convertIfNoLossOfMagnitude(Number number)
Converts the number to
this type only if no loss of magnitude and meaning occurs, otherwise returns
an empty optional. |
Number |
convertToBounds(Number numberToConvert)
Returns a value of
this type that is the closest to the given number. |
abstract Number |
convertUnsafe(Number number)
Converts the given number to
this type without considering underflow or overflow. |
static @Nullable StandardNumberType |
fromClass(Class<?> clazz)
Returns the
NumberType instance of this enum that corresponds to the given class, null otherwise. |
static <T extends Number> |
fromNumberClass(@Nullable Class<T> clazz)
Returns the instance that corresponds to this class, or null if not applicable.
|
Class<? extends Number> |
getType() |
ValueRange<?> |
getValueRange()
Returns information about the value range this type can represent.
|
static Stream<NumberType<? extends Number>> |
streamThroughAll()
Creates a stream of all number types in this enum, typed as
NumberType. |
static Stream<NumberType<? extends Number>> |
streamThroughPrimitiveTypes()
Creates a stream of the number type instances that represent one of the six Java number types that
are associated with a primitive type: byte, short, int, long, float, and double.
|
String |
toString() |
static StandardNumberType |
valueOf(String name)
Returns the enum constant of this type with the specified name.
|
static StandardNumberType[] |
values()
Returns an array containing the constants of this enum type, in
the order they are declared.
|
clone, compareTo, equals, finalize, getDeclaringClass, hashCode, name, ordinal, valueOfsupportsAllValuesOfpublic static final StandardNumberType BYTE
public static final StandardNumberType SHORT
public static final StandardNumberType INTEGER
public static final StandardNumberType LONG
public static final StandardNumberType FLOAT
public static final StandardNumberType DOUBLE
public static final StandardNumberType BIG_INTEGER
public static final StandardNumberType BIG_DECIMAL
public static final NumberType<Byte> TYPE_BYTE
public static final NumberType<Short> TYPE_SHORT
public static final NumberType<Integer> TYPE_INTEGER
public static final NumberType<Long> TYPE_LONG
public static final NumberType<Float> TYPE_FLOAT
public static final NumberType<Double> TYPE_DOUBLE
public static final NumberType<BigInteger> TYPE_BIG_INTEGER
public static final NumberType<BigDecimal> TYPE_BIG_DECIMAL
public static StandardNumberType[] values()
for (StandardNumberType c : StandardNumberType.values()) System.out.println(c);
public static StandardNumberType valueOf(String name)
name - the name of the enum constant to be returned.IllegalArgumentException - if this enum type has no constant with the specified nameNullPointerException - if the argument is nullpublic Class<? extends Number> getType()
getType in interface NumberTypepublic ValueRange<?> getValueRange()
The range is always typed the same as this entry's type.
getValueRange in interface NumberTypepublic Optional<Number> convertIfNoLossOfMagnitude(Number number)
this type only if no loss of magnitude and meaning occurs, otherwise returns
an empty optional. For example, BYTE.convertIfNoLossOfMagnitude(200) returns an empty Optional (as 200
exceeds the max value of the Byte type, 127), while BYTE.convertIfNoLossOfMagnitude(12) would return
an Optional with (byte) 12 as value.
Examples:
T_SHORT.convertIfNoLossOfMagnitude(40); // Optional.of((short) 40)
T_SHORT.convertIfNoLossOfMagnitude(100_000L); // Optional.empty()
T_FLOAT.convertIfNoLossOfMagnitude(Double.NaN); // Optional.of(Float.NaN)
T_LONG.convertIfNoLossOfMagnitude(Double.NaN); // Optional.empty()
Implementations by this library support all instantiable Number classes of the JDK as input. An
IllegalArgumentException is thrown for other types.
Always returns a number of this entry's type.
convertIfNoLossOfMagnitude in interface NumberTypenumber - the number to potentially convert (not null)public Number convertToBounds(Number numberToConvert)
this type that is the closest to the given number. The implementations of this library
can convert every instantiable Number class of the JDK (e.g. Integer, AtomicLong, BigDecimal, LongAdder).
An IllegalArgumentException is thrown if the number type is unknown.
this type, the minimum or maximum value is returned.
Other special cases:NaN is converted to 0 if the type does not support the NaN value.
Examples from StandardNumberType:
T_INTEGER.convertToBounds(new BigDecimal("1234.6")); // returns `1234`
T_DOUBLE.convertToBounds(new AtomicLong(-800)); // returns `-800.0`
T_BYTE.convertToBounds(200); // returns `(byte) 127`, the max value of Byte
T_SHORT.convertToBounds(Double.NEGATIVE_INFINITY); // returns `(short) -32768`, the min value of Short
T_BIG_DECIMAL.convertToBounds(Double.NEGATIVE_INFINITY); // returns `0`
T_LONG.convertToBounds(Double.NEGATIVE_INFINITY); // returns `Long.MIN_VALUE`
To catch NaN and infinity before this method, it is possible to use
ValueRangeComparison.getErrorForNonFiniteValue(Number).
Other conversion methods on this interface are NumberType.convertIfNoLossOfMagnitude(java.lang.Number) and NumberType.convertUnsafe(java.lang.Number).
Always returns a number of this entry's type.
convertToBounds in interface NumberTypenumberToConvert - the number to convert (not null)public abstract Number convertUnsafe(Number number)
this type without considering underflow or overflow. Among primitive types,
this method behaves like casting one type to another. For example, byte b = (byte) 200
assigns a value of -56 to b due to overflow. Similarly, BYTE.convertUnsafe(200) returns -56.
This method never returns null. When this type is a BigDecimal or BigInteger, NaN and infinity
values from Float and Double are converted to 0. For instance,
BIG_DECIMAL.convertUnsafe(Double.POSITIVE_INFINITY) returns BigDecimal.ZERO.
For other types, the conversion follows the default behavior of Java: e.g.,
BYTE.convertUnsafe(Double.NaN) = (byte) Double.NaN = (byte) 0.
Use NumberType.convertToBounds(java.lang.Number) to convert values that are beyond this type's range to the nearest possible
value, or NumberType.convertIfNoLossOfMagnitude(java.lang.Number) to only convert the number if it can be represented by the type.
Some implementations by this library throw an IllegalArgumentException if a number is provided that is
not of an instantiable Number class of the JDK.
Examples:
T_BYTE.convertUnsafe(-255); // returns (byte) 1, due to underflow
T_LONG.convertUnsafe(200); // returns 200L
T_FLOAT.convertUnsafe(Double.POSITIVE_INFINITY); // returns Float.POSITIVE_INFINITY
T_INTEGER.convertUnsafe(Double.POSITIVE_INFINITY); // returns Integer.MAX_VALUE
T_BIG_INTEGER.convertUnsafe(Double.POSITIVE_INFINITY); // returns BigInteger.ZERO
Always returns a number of this entry's type.
convertUnsafe in interface NumberTypenumber - the number to cast (not null)public ValueRangeComparison compareToValueRange(Number number)
NumberTypeNumberType.convertToBounds(java.lang.Number) if you want to convert the number
to this type and use the closest possible value.
The comparison result represents the relationship between the given number and the set of possible values
for this type, expressed through the ValueRangeComparison enum. Examples:
ValueRangeComparison result1 = T_INTEGER.compareToValueRange(42L);
// result1 = ValueRangeComparison.WITHIN_RANGE
ValueRangeComparison result2 = T_INTEGER.compareToValueRange(Double.POSITIVE_INFINITY);
// result2 = ValueRangeComparison.UNSUPPORTED_POSITIVE_INFINITY
compareToValueRange in interface NumberTypenumber - the number to process@Nullable public static @Nullable StandardNumberType fromClass(Class<?> clazz)
NumberType instance of this enum that corresponds to the given class, null otherwise.
Primitive and reference types are supported (e.g., short.class and Short.class both map to
SHORT).
fromNumberClass(java.lang.Class<T>) to get the same entry as a typed NumberType.clazz - the class to find the number type for@Nullable public static <T extends Number> @Nullable NumberType<T> fromNumberClass(@Nullable @Nullable Class<T> clazz)
short.class and Short.class both map to
TYPE_SHORT).T - the number typeclazz - the class to find the number type forNumberTypes.from(java.lang.Class<T>)public static Stream<NumberType<? extends Number>> streamThroughAll()
NumberType.public static Stream<NumberType<? extends Number>> streamThroughPrimitiveTypes()
public String toString()
toString in class Enum<StandardNumberType>Copyright © 2023. All rights reserved.