Class CharScannerNumberParserBase

java.lang.Object
io.github.mmm.scanner.number.CharScannerNumberParserBase
All Implemented Interfaces:
CharScannerNumberParser, CharScannerRadixHandler
Direct Known Subclasses:
CharScannerNumberParserLang, CharScannerNumberParserString

public abstract class CharScannerNumberParserBase extends Object implements CharScannerNumberParser
CharScannerNumberParser to parse java.lang Number values such as Long, Integer, Double or Float.
  • Field Details

    • NAN

      protected static final String NAN
      See Also:
    • INFINITY

      protected static final String INFINITY
      See Also:
    • builder

      protected StringBuilder builder
      StringBuilder to build the number as String. Initialized by sub-class to support lazy init.
    • error

      protected boolean error
      true if an error was detected and the result can only be an exception, false otherwise.
    • sign

      protected char sign
      The initial sign character +/- or '\0' for none.
    • radix

      protected int radix
      The radix of the digits, initially 10 but can be changed by radix(int, char).
    • radixChar

      protected char radixChar
      The radix character used by builder() if lazy created only for error.
    • digitsTotal

      protected int digitsTotal
      The total number of digits of mantissa that have been parsed.
    • digitsLeadingZeros

      protected int digitsLeadingZeros
      The leading zeros of mantissa.
    • digitsTrailingZeros

      protected int digitsTrailingZeros
      The current number of trailing zeros of mantissa.
    • dotPosition

      protected int dotPosition
      The position of the decimal dot in the mantissa.
    • exponentSymbol

      protected char exponentSymbol
      The exponent symbol character e/E/p/P or '\0' for none.
    • exponentSign

      protected char exponentSign
      The exponent sign character +/- or '\0' for none.
    • exponentDigitsTotal

      protected int exponentDigitsTotal
      The total number of digits of the exponent.
    • exponentDigitsLeadingZeros

      protected int exponentDigitsLeadingZeros
      The leading zeros of the exponent.
    • openDelimiter

      protected boolean openDelimiter
      true in case of a delimiter that has not been completed by a digit, false otherwise.
  • Constructor Details

  • Method Details

    • specials

      public static CharScannerNumberParserBase.CharScannerNumberSpecial[] specials(String delimiters, boolean specialNumbers)
      Parameters:
      delimiters - the accepted delimiter characters.
      specialNumbers - - true to accept the special numbers NAN and INFINITY.
      Returns:
      the resulting String array to pass to constructor.
    • isDigit

      protected final boolean isDigit(char c)
      Parameters:
      c - the character to check.
      Returns:
      true in case of a digit, false otherwise.
    • builder

      protected StringBuilder builder()
      Returns:
      the StringBuilder to build the number. Ensures initialization in case of lazy-init.
    • sign

      public boolean sign(char signChar)
      Description copied from interface: CharScannerNumberParser
      This method will only be called if the first character if the number is '+' or '-'.
      Specified by:
      sign in interface CharScannerNumberParser
      Parameters:
      signChar - '+' for positive number and '-' for negative number. If no sign is present this method will never be called.
      Returns:
      true if the sign shall be accepted and further characters shall be received, false to prevent consuming the sign or any further characters and abort the process (e.g. to parse only positive numbers without any sign).
    • radix

      public int radix(int newRadix, char c)
      Description copied from interface: CharScannerRadixHandler
      This method will be called if the first character is '0' and will also pass a lookahead of the next character as parameter symbol.
      Specified by:
      radix in interface CharScannerRadixHandler
      Parameters:
      newRadix - the radix. Will be 16 for "0x", 2 for "0b", 8 for "0" followed by an octal digit (0-7), and 0 in case an unexpected character was found after the first zero.
      c - the character followed by the leading zero. E.g. 'x' or 'X' for radix 16. You could even implement custom radix mode like "0o" for octal instead of "0" by returning 8 if symbol is 'o' or 'O'.
      Returns:
      the actual radix to use for further processing. If 0 (or less) is returned the radix is not accepted and "0" is treated as a leading zero rather than a prefix of the radix that will remain to be 10. Typically implementations will return the given radix, but to prevent octal parsing due to a leading zero (Java/C legacy) you can return 10 if 8 was given. If 8 is returned, the symbol will be consumed and skipped if it is not a digit while otherwise the digit will be consumed as part of the number even if it is greater than 7 (finally leading to a NumberFormatException).
    • appendRadix

      protected void appendRadix()
      Appends the radix to the number StringBuilder.
    • digit

      public boolean digit(int digit, char digitChar)
      Specified by:
      digit in interface CharScannerNumberParser
      Parameters:
      digit - the parsed digitChar as numeric digit to "append". Will never be negative but may be greater or equal to the radix returned by CharScannerRadixHandler.radix(int, char) as NumberFormatException has to be handled inside the receiver.
      digitChar - the original digit character. In case the number shall be received as String this makes your life simpler and allows to preserve the case.
      Returns:
      true if the given digit is accepted, false otherwise (exceeds the range of the number to parse and the digit should not be consumed). Typical implementations should always return true.
    • resetTrailingZeros

      protected void resetTrailingZeros()
      Resets the trailing zeros if a non zero digit was found for mantissa.
    • isDecimal

      protected abstract boolean isDecimal()
      Returns:
      true in case decimal numbers with dot() and/or exponent are accepted, false otherwise.
    • dot

      public boolean dot()
      Specified by:
      dot in interface CharScannerNumberParser
      Returns:
      true if the decimal dot ('.') shall be accepted, false otherwise (stop further processing e.g. to parse only integer numbers).
    • exponent

      public boolean exponent(char e, char signChar)
      Specified by:
      exponent in interface CharScannerNumberParser
      Parameters:
      e - the exponent character. Typically 'e' or 'E' but may also be 'p' or 'P' (for power used for hex base as 'E' is a hex-digit).
      signChar - the sign character ('+' or '-') or 0 for no sign.
      Returns:
      true if the scientific notation exponent is supported and the characters shall be consumed, false otherwise (stop further processing and do not consume characters).
    • appendExponent

      protected void appendExponent(boolean lazy)
      Appends the exponent symbols.
      Parameters:
      lazy - - true if called lazily to recreate the parsed String on numeric error, false otherwise (regular appending in String mode).
    • special

      public String special(char c)
      Description copied from interface: CharScannerNumberParser
      This method allows handling special characters like thousand delimiter (e.g. '_' or ',') or for the start of special numbers such as "NaN" or "Infinity". So for 'N' it can return "NaN" and for 'I' it can return "Infinity" to support these special numbers. For a delimiter it can return @other. Otherwise return null here.
      Specified by:
      special in interface CharScannerNumberParser
      Parameters:
      c - the special charater that was found (no digit, no dot, no exponent).
      Returns:
      null to stop without consuming the given character or a String that is expected (and shall start with the given special character). If that String was found in the scanner, CharScannerNumberParser.special(String) is called. Otherwise again not even the given characters gets consumed.
    • special

      public void special(String special)
      Specified by:
      special in interface CharScannerNumberParser
      Parameters:
      special - the special String that was found and consumed.
      See Also:
    • isValidDelimiterPosition

      protected final boolean isValidDelimiterPosition()
      Returns:
      true if the current position and state is valid to accept a digit delimiter.
    • toString

      public String toString()
      Overrides:
      toString in class Object