Enum Class NumberType

java.lang.Object
java.lang.Enum<NumberType>
net.luis.utils.math.NumberType
All Implemented Interfaces:
Serializable, Comparable<NumberType>, Constable

public enum NumberType extends Enum<NumberType>
Represents a type of number that can be parsed from a string.
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 Constants
    Enum Constant
    Description
    Represents a BigDecimal number type.
    Represents a BigInteger number type.
    Represents a Byte number type.
    Represents a Double number type.
    Represents a Float number type.
    Represents a Integer number type.
    Represents a Long number type.
    Represents a Short number type.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private 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).
    private final Class<? extends Number>
    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.
    private final Set<Radix>
    The radices supported by the number type.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    private
    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 Type
    Method
    Description
    boolean
    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 exception
    boolean
    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.
    @NotNull Class<? extends Number>
    Returns the java number class that this type represents.
    char
    Returns the suffix of the number type.
    @NotNull @Unmodifiable Set<Radix>
    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
    isInRange(@NotNull Number number)
    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>
    T
    parseNumber(@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>
    T
    parseNumberStrict(@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'.
     
    static NumberType
    Returns the enum constant of this class with the specified name.
    static NumberType[]
    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.

    Methods inherited from class java.lang.Object

    getClass, notify, notifyAll, wait, wait, wait
  • Enum Constant Details

  • Field Details

    • numberClass

      private final Class<? extends Number> numberClass
      The java number class that this type represents.
    • bitSize

      private final int bitSize
      The bit size of the number type.
      -1 if the number type is infinite (big integer or big decimal).
    • minValue

      @Nullable private final @Nullable Number minValue
      The minimum value of the number type.
      Null if the number type is infinite (big integer or big decimal).
    • maxValue

      @Nullable private final @Nullable Number maxValue
      The maximum value of the number type.
      Null if the number type is infinite (big integer or big decimal).
    • floatingPoint

      private final boolean floatingPoint
      Flag indicating if the number type is a floating point number.
    • suffix

      private final char suffix
      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.
    • supportedRadices

      private final Set<Radix> supportedRadices
      The radices supported by the number type.
    • parser

      private final BiFunction<String,Radix,? extends Number> 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 represents
      bitSize - The bit size of the number type
      minValue - The minimum value of the number type
      maxValue - The maximum value of the number type
      floatingPoint - Flag indicating if the number type is a floating point number
      suffix - The suffix of the number type
      supportedRadices - The radices supported by the number type
      radixParser - 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 null
      NullPointerException - If any of the parameters are null
  • Method Details

    • values

      public static NumberType[] 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

      public static NumberType valueOf(String name)
      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 name
      NullPointerException - if the argument is null
    • wrapIntegerParser

      @NotNull private static @NotNull BiFunction<String,Radix,? extends Number> wrapIntegerParser(@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,? extends Number> wrapFloatingPointParser(@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

      @NotNull private static @NotNull BiFunction<String,Radix,? extends Number> 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 to
      radix - 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 from
      radices - 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 from
      type - The number type to check the suffix against
      radix - The radix to check the suffix against
      Returns:
      The value with the type suffix removed
      Throws:
      NullPointerException - If the value, type or radix is null
      IllegalArgumentException - If the suffix is present but does not match the type suffix of the number type
    • getBySuffix

      @Nullable public static @Nullable NumberType getBySuffix(char suffix)
      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

      @NotNull public @NotNull Class<? extends Number> 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

      @Nullable public @Nullable Number 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

      @Nullable public @Nullable Number 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

      public boolean isInRange(@NotNull @NotNull Number number)
      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

      @NotNull public @NotNull @Unmodifiable Set<Radix> getSupportedRadices()
      Returns the radices supported by the number type.
      Returns:
      The supported radices
    • isSupportedRadix

      public boolean isSupportedRadix(@Nullable @Nullable Radix radix)
      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

      public boolean canConvertTo(@NotNull @NotNull NumberType targetType)
      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

      public boolean canConvertTo(@Nullable @Nullable String value, @NotNull @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 exception
      Parameters:
      value - The value to check
      radix - 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 parse
      radix - 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 suffix
      NumberFormatException - 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 parse
      radix - The radix to parse the value with
      Returns:
      The parsed number
      Throws:
      NullPointerException - If the value or radix is null
      IllegalArgumentException - If the value is empty or blank or the radix is not supported
      NumberFormatException - If the value contains an unexpected type suffix or cannot be parsed
      See Also:
    • toString

      public String toString()
      Overrides:
      toString in class Enum<NumberType>