N - the number type of this instancepublic interface NumberType<N>
Standard implementations for basic Java number types are available at
StandardNumberType and MoreNumberTypes.
StandardNumberType,
NumberTypes.from(Class)| 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.
|
default Optional<N> |
convertIfNoLossOfMagnitude(Number number)
Converts the number to
this type only if no loss of magnitude and meaning occurs, otherwise returns
an empty optional. |
N |
convertToBounds(Number numberToConvert)
Returns a value of
this type that is the closest to the given number. |
N |
convertUnsafe(Number number)
Converts the given number to
this type without considering underflow or overflow. |
Class<N> |
getType() |
ValueRange<N> |
getValueRange()
Returns information about the value range this type can represent.
|
default boolean |
supportsAllValuesOf(NumberType<?> other)
Convenience method for
ValueRange.supportsAllValuesOf(ch.jalu.typeresolver.numbers.ValueRange<?>). |
N 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 convertIfNoLossOfMagnitude(java.lang.Number) and convertUnsafe(java.lang.Number).
numberToConvert - the number to convert (not null)default Optional<N> 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.
number - the number to potentially convert (not null)N 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 convertToBounds(java.lang.Number) to convert values that are beyond this type's range to the nearest possible
value, or 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
number - the number to cast (not null)ValueRange<N> getValueRange()
ValueRangeComparison compareToValueRange(Number number)
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
number - the number to processdefault boolean supportsAllValuesOf(NumberType<?> other)
ValueRange.supportsAllValuesOf(ch.jalu.typeresolver.numbers.ValueRange<?>). This method returns true if this type
can represent all values of the other type without loss of magnitude or meaning. Support for
decimals are not considered. See ValueRange.supportsAllValuesOf(ch.jalu.typeresolver.numbers.ValueRange<?>) for a more detailed description.
If this method returns true, then calling compareToValueRange(java.lang.Number) on this type
with any number of the given other type will always return ValueRangeComparison.WITHIN_RANGE.
This also means that convertIfNoLossOfMagnitude(java.lang.Number) will always return a non-empty Optional.
other - the number type to checkValueRange.supportsAllValuesOf(ch.jalu.typeresolver.numbers.ValueRange<?>)Copyright © 2023. All rights reserved.