- All Implemented Interfaces:
Serializable
,Comparable<NumberType>
,Constable
Represents a type of number that can be parsed from a string.
This enum contains the java default number types and their respective parsers.
This enum contains the java default number types and their respective parsers.
-
Nested Class Summary
Nested classes/interfaces inherited from class java.lang.Enum
Enum.EnumDesc<E extends Enum<E>>
-
Enum Constant Summary
Enum ConstantsEnum ConstantDescriptionRepresents aBigDecimal
number type.Represents aBigInteger
number type.Represents aByte
number type.Represents aDouble
number type.Represents aFloat
number type.Represents aInteger
number type.Represents aLong
number type.Represents aShort
number type. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final int
The bit size of the number type.
-1 if the number type is infinite (big integer or big decimal).private final boolean
Flag indicating if the number type is a floating point number.private final @Nullable Number
The maximum value of the number type.
Null if the number type is infinite (big integer or big decimal).private final @Nullable Number
The minimum value of the number type.
Null if the number type is infinite (big integer or big decimal).The java number class that this type represents.private final BiFunction
<String, Radix, ? extends Number> The parser function that converts a string to a number of this type based on the radix.private final char
The suffix of the number type.
If the number does not have a suffix by default in java, it will be the first character of the number type name.
'\0'
if the number type does not have a suffix.The radices supported by the number type. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprivate
NumberType
(@NotNull Class<? extends Number> numberClass, int bitSize, @Nullable Number minValue, @Nullable Number maxValue, boolean floatingPoint, char suffix, @NotNull Set<Radix> supportedRadices, @NotNull BiFunction<String, Radix, ? extends Number> radixParser) Constructs a new number type with the given parameters. -
Method Summary
Modifier and TypeMethodDescriptionboolean
canConvertTo
(@Nullable String value, @NotNull Radix radix) Checks if the number type can be converted to the given target type.
The following rules apply:
If the value is null, empty or blank, it cannot be converted If the radix is not supported by the number type, it cannot be converted If the value has a suffix, it must match the number type suffix The parser must not throw an exceptionboolean
canConvertTo
(@NotNull NumberType targetType) Checks if the number type can be converted to the given target type.
The following rules apply:private static @NotNull BiFunction
<String, Radix, ? extends Number> Creates a new big decimal parser function.
This parser function will handle the special cases of big decimals.int
Returns the bit size of the number type.static @Nullable NumberType
getBySuffix
(char suffix) Gets the number type by the given suffix.
Will return null if the suffix is'\0'
.@Nullable Number
Returns the maximum value of the number type.@Nullable Number
Returns the minimum value of the number type.Returns the java number class that this type represents.char
Returns the suffix of the number type.Returns the radices supported by the number type.private static @NotNull String
insertRadixPrefix
(@NotNull String value, @NotNull Radix radix) Inserts the radix prefix to the given value if it is not already present.
If the value is signed, the radix prefix will be inserted after the sign.
This method should only be used for floating point numbers.boolean
Returns if the number type is a floating point number.boolean
Returns if the number type is infinite (big integer or big decimal).boolean
Checks if the given number is in the range of the number type.
If the number type is infinite, this will always return true.boolean
isSupportedRadix
(@Nullable Radix radix) Checks if the number type supports the given radix.<T extends Number>
TparseNumber
(@Nullable String value, @Nullable Radix radix) Parses the given value to a number of this type based on the radix.
This method parses the value in soft mode and will handle invalid cases gracefully.
The following rules apply:
If the value is null or empty, it will be parsed as 0 If the radix is null, then the radix will be determined based on the value If the value contains a type suffix, it will be removed If the value contains a radix prefix, it will be removed<T extends Number>
TparseNumberStrict
(@NotNull String value, @NotNull Radix radix) Parses the given value to a number of this type based on the radix.
This method parses the value strictly and will throw an exception if the value is invalid.private static @NotNull String
removeRadixPrefixes
(@NotNull String value, Radix @NotNull ... radices) Removes the radix prefixes from the given value.
This method should only be used for integer numbers.private static @NotNull String
removeTypeSuffix
(@NotNull String value, @NotNull NumberType type, @NotNull Radix radix) Removes the type suffix from the given value if it is present.
If the radix is hexadecimal, the suffix must be outside the range of 'a' to 'f'.toString()
static NumberType
Returns 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.private static @NotNull BiFunction
<String, Radix, ? extends Number> wrapFloatingPointParser
(@NotNull Function<String, ? extends Number> parser) Wraps the floating point parser function to a number parser function.
The wrapped parser handles the special cases of floating point numbers.private static @NotNull BiFunction
<String, Radix, ? extends Number> wrapIntegerParser
(@NotNull BiFunction<String, Integer, ? extends Number> parser) Wraps the integer parser function to a number parser function.
The wrapped parser simply translates the radix to an integer.
-
Enum Constant Details
-
BYTE
Represents aByte
number type. -
SHORT
Represents aShort
number type. -
INTEGER
Represents aInteger
number type. -
LONG
Represents aLong
number type. -
BIG_INTEGER
Represents aBigInteger
number type. -
FLOAT
Represents aFloat
number type. -
DOUBLE
Represents aDouble
number type. -
BIG_DECIMAL
Represents aBigDecimal
number type.
-
-
Field Details
-
numberClass
The java number class that this type represents. -
bitSize
private final int bitSizeThe bit size of the number type.
-1 if the number type is infinite (big integer or big decimal). -
minValue
The minimum value of the number type.
Null if the number type is infinite (big integer or big decimal). -
maxValue
The maximum value of the number type.
Null if the number type is infinite (big integer or big decimal). -
floatingPoint
private final boolean floatingPointFlag indicating if the number type is a floating point number. -
suffix
private final char suffixThe suffix of the number type.
If the number does not have a suffix by default in java, it will be the first character of the number type name.
'\0'
if the number type does not have a suffix. -
supportedRadices
The radices supported by the number type. -
parser
The parser function that converts a string to a number of this type based on the radix.
-
-
Constructor Details
-
NumberType
private NumberType(@NotNull @NotNull Class<? extends Number> numberClass, int bitSize, @Nullable @Nullable Number minValue, @Nullable @Nullable Number maxValue, boolean floatingPoint, char suffix, @NotNull @NotNull Set<Radix> supportedRadices, @NotNull @NotNull BiFunction<String, Radix, ? extends Number> radixParser) Constructs a new number type with the given parameters.- Parameters:
numberClass
- The java number class that this type representsbitSize
- The bit size of the number typeminValue
- The minimum value of the number typemaxValue
- The maximum value of the number typefloatingPoint
- Flag indicating if the number type is a floating point numbersuffix
- The suffix of the number typesupportedRadices
- The radices supported by the number typeradixParser
- The parser function that converts a string to a number of this type based on the radix- Throws:
IllegalArgumentException
- If the minimum and maximum values are not both null or not nullNullPointerException
- If any of the parameters are null
-
-
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
-
wrapIntegerParser
@NotNull private static @NotNull BiFunction<String,Radix, wrapIntegerParser? extends Number> (@NotNull @NotNull BiFunction<String, Integer, ? extends Number> parser) Wraps the integer parser function to a number parser function.
The wrapped parser simply translates the radix to an integer.- Parameters:
parser
- The integer parser function- Returns:
- The wrapped parser function
- Throws:
NullPointerException
- If the parser is null
-
wrapFloatingPointParser
@NotNull private static @NotNull BiFunction<String,Radix, wrapFloatingPointParser? extends Number> (@NotNull @NotNull Function<String, ? extends Number> parser) Wraps the floating point parser function to a number parser function.
The wrapped parser handles the special cases of floating point numbers.- Parameters:
parser
- The floating point parser function- Returns:
- The wrapped parser function
- Throws:
NullPointerException
- If the parser is null
-
createBigDecimalParser
Creates a new big decimal parser function.
This parser function will handle the special cases of big decimals.- Returns:
- The big decimal parser function
- See Also:
-
insertRadixPrefix
@NotNull private static @NotNull String insertRadixPrefix(@NotNull @NotNull String value, @NotNull @NotNull Radix radix) Inserts the radix prefix to the given value if it is not already present.
If the value is signed, the radix prefix will be inserted after the sign.
This method should only be used for floating point numbers.- Parameters:
value
- The value to insert the radix prefix toradix
- The radix to insert- Returns:
- The value with the radix prefix inserted
- Throws:
NullPointerException
- If the value or radix is null
-
removeRadixPrefixes
@NotNull private static @NotNull String removeRadixPrefixes(@NotNull @NotNull String value, Radix @NotNull ... radices) Removes the radix prefixes from the given value.
This method should only be used for integer numbers.- Parameters:
value
- The value to remove the radix prefixes fromradices
- The radices to remove- Returns:
- The value with the radix prefixes removed
- Throws:
NullPointerException
- If the value is null
-
removeTypeSuffix
@NotNull private static @NotNull String removeTypeSuffix(@NotNull @NotNull String value, @NotNull @NotNull NumberType type, @NotNull @NotNull Radix radix) Removes the type suffix from the given value if it is present.
If the radix is hexadecimal, the suffix must be outside the range of 'a' to 'f'.- Parameters:
value
- The value to remove the type suffix fromtype
- The number type to check the suffix againstradix
- The radix to check the suffix against- Returns:
- The value with the type suffix removed
- Throws:
NullPointerException
- If the value, type or radix is nullIllegalArgumentException
- If the suffix is present but does not match the type suffix of the number type
-
getBySuffix
Gets the number type by the given suffix.
Will return null if the suffix is'\0'
.- Parameters:
suffix
- The suffix of the number type- Returns:
- The number type with the given suffix or null
-
getNumberClass
Returns the java number class that this type represents.- Returns:
- The number class
-
getBitSize
public int getBitSize()Returns the bit size of the number type.- Returns:
- The bit size or -1 if the number type is infinite (big integer or big decimal)
-
getMinValue
Returns the minimum value of the number type.- Returns:
- The minimum value or null if the number type is infinite (big integer or big decimal)
-
getMaxValue
Returns the maximum value of the number type.- Returns:
- The maximum value or null if the number type is infinite (big integer or big decimal)
-
isInfinite
public boolean isInfinite()Returns if the number type is infinite (big integer or big decimal).- Returns:
- True if the number type is infinite, false otherwise
-
isInRange
Checks if the given number is in the range of the number type.
If the number type is infinite, this will always return true.- Parameters:
number
- The number to check- Returns:
- True if the number is in the range, false otherwise
- Throws:
NullPointerException
- If the number is null
-
isFloatingPoint
public boolean isFloatingPoint()Returns if the number type is a floating point number.- Returns:
- True if the number type is a floating point number, false otherwise
-
getSuffix
public char getSuffix()Returns the suffix of the number type.- Returns:
- The suffix
-
getSupportedRadices
Returns the radices supported by the number type.- Returns:
- The supported radices
-
isSupportedRadix
Checks if the number type supports the given radix.- Parameters:
radix
- The radix to check- Returns:
- True if the number type supports the radix, false otherwise
-
canConvertTo
Checks if the number type can be converted to the given target type.
The following rules apply:
If the number type is...
- the same as the target type, it can be converted
- a floating point number and the target type is not, it cannot be converted
- infinite, the target type must also be infinite
- not infinite but the target type is, the number type can be converted
- the bit size is greater than the target type, it cannot be converted
- the minimum value is less than the target type, it cannot be converted
- the maximum value is greater than the target type, it cannot be converted
- Parameters:
targetType
- The target type to check- Returns:
- True if the number type can be converted to the target type, false otherwise
- Throws:
NullPointerException
- If the target type is null
-
canConvertTo
Checks if the number type can be converted to the given target type.
The following rules apply:
- If the value is null, empty or blank, it cannot be converted
- If the radix is not supported by the number type, it cannot be converted
- If the value has a suffix, it must match the number type suffix
- The parser must not throw an exception
- Parameters:
value
- The value to checkradix
- The radix to check- Returns:
- True if the number type can be converted to the target type, false otherwise
- Throws:
NullPointerException
- If the radix is null
-
parseNumber
@NotNull public <T extends Number> T parseNumber(@Nullable @Nullable String value, @Nullable @Nullable Radix radix) Parses the given value to a number of this type based on the radix.
This method parses the value in soft mode and will handle invalid cases gracefully.
The following rules apply:
- If the value is null or empty, it will be parsed as 0
- If the radix is null, then the radix will be determined based on the value
- If the value contains a type suffix, it will be removed
- If the value contains a radix prefix, it will be removed
- Type Parameters:
T
- The number type (auto-casted)- Parameters:
value
- The value to parseradix
- The radix to parse the value with- Returns:
- The parsed number
- Throws:
IllegalArgumentException
- If the radix is not supported, or the type suffix does not match the number type suffixNumberFormatException
- If the value cannot be parsed- See Also:
-
parseNumberStrict
@NotNull public <T extends Number> T parseNumberStrict(@NotNull @NotNull String value, @NotNull @NotNull Radix radix) Parses the given value to a number of this type based on the radix.
This method parses the value strictly and will throw an exception if the value is invalid.- Type Parameters:
T
- The number type (auto-casted)- Parameters:
value
- The value to parseradix
- The radix to parse the value with- Returns:
- The parsed number
- Throws:
NullPointerException
- If the value or radix is nullIllegalArgumentException
- If the value is empty or blank or the radix is not supportedNumberFormatException
- If the value contains an unexpected type suffix or cannot be parsed- See Also:
-
toString
- Overrides:
toString
in classEnum<NumberType>
-