java.lang.Object
io.github.mmm.scanner.number.CharScannerNumberParserBase
- All Implemented Interfaces:
CharScannerNumberParser,CharScannerRadixHandler
- Direct Known Subclasses:
CharScannerNumberParserLang,CharScannerNumberParserString
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceInterface for handling of a special number syntax.static classCharScannerNumberParserBase.CharScannerNumberSpecialfor digit delimiters like '_'.static classCharScannerNumberParserBase.CharScannerNumberSpecialfor non-numeric number literals likeNAN. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected StringBuilderStringBuilderto build the number asString.protected intThe leading zeros of mantissa.protected intThe total number of digits of mantissa that have been parsed.protected intThe current number of trailing zeros of mantissa.protected intThe position of the decimal dot in the mantissa.protected booleantrueif an error was detected and the result can only be an exception,falseotherwise.protected intThe leading zeros of the exponent.protected intThe total number of digits of the exponent.protected charThe exponent sign character +/- or '\0' for none.protected charThe exponent symbol character e/E/p/P or '\0' for none.protected static final Stringprotected static final Stringprotected booleantruein case of a delimiter that has not been completed by a digit,falseotherwise.protected intThe radix of the digits, initially10but can be changed byradix(int, char).protected charThe radix character used bybuilder()if lazy created only for error.protected charThe initial sign character +/- or '\0' for none. -
Constructor Summary
ConstructorsConstructorDescriptionCharScannerNumberParserBase(CharScannerRadixHandler radixMode, CharScannerNumberParserBase.CharScannerNumberSpecial... specials) The constructor. -
Method Summary
Modifier and TypeMethodDescriptionprotected voidappendExponent(boolean lazy) Appends theexponentsymbols.protected voidAppends the radix to the numberStringBuilder.protected StringBuilderbuilder()booleandigit(int digit, char digitChar) booleandot()booleanexponent(char e, char signChar) protected abstract booleanprotected final booleanisDigit(char c) protected final booleanintradix(int newRadix, char c) This method will be called if the first character is '0' and will also pass a lookahead of the next character as parametersymbol.protected voidResets the trailing zeros if a non zero digit was found for mantissa.booleansign(char signChar) This method will only be called if the first character if the number is '+' or '-'.special(char c) This method allows handling special characters like thousand delimiter (e.g.voidtoString()
-
Field Details
-
NAN
- See Also:
-
INFINITY
- See Also:
-
builder
StringBuilderto build the number asString. Initialized by sub-class to support lazy init. -
error
protected boolean errortrueif an error was detected and the result can only be an exception,falseotherwise. -
sign
protected char signThe initial sign character +/- or '\0' for none. -
radix
protected int radixThe radix of the digits, initially10but can be changed byradix(int, char). -
radixChar
protected char radixCharThe radix character used bybuilder()if lazy created only for error. -
digitsTotal
protected int digitsTotalThe total number of digits of mantissa that have been parsed. -
digitsLeadingZeros
protected int digitsLeadingZerosThe leading zeros of mantissa. -
digitsTrailingZeros
protected int digitsTrailingZerosThe current number of trailing zeros of mantissa. -
dotPosition
protected int dotPositionThe position of the decimal dot in the mantissa. -
exponentSymbol
protected char exponentSymbolThe exponent symbol character e/E/p/P or '\0' for none. -
exponentSign
protected char exponentSignThe exponent sign character +/- or '\0' for none. -
exponentDigitsTotal
protected int exponentDigitsTotalThe total number of digits of the exponent. -
exponentDigitsLeadingZeros
protected int exponentDigitsLeadingZerosThe leading zeros of the exponent. -
openDelimiter
protected boolean openDelimitertruein case of a delimiter that has not been completed by a digit,falseotherwise.
-
-
Constructor Details
-
CharScannerNumberParserBase
public CharScannerNumberParserBase(CharScannerRadixHandler radixMode, CharScannerNumberParserBase.CharScannerNumberSpecial... specials) The constructor.- Parameters:
radixMode- theCharScannerRadixHandlerforradix(int, char).specials- thespecial numbersanddelimiters.
-
-
Method Details
-
specials
public static CharScannerNumberParserBase.CharScannerNumberSpecial[] specials(String delimiters, boolean specialNumbers) - Parameters:
delimiters- the accepteddelimitercharacters.specialNumbers- -trueto accept the special numbersNANandINFINITY.- Returns:
- the resulting
Stringarray to pass toconstructor.
-
isDigit
protected final boolean isDigit(char c) - Parameters:
c- the character to check.- Returns:
truein case of a digit,falseotherwise.
-
builder
- Returns:
- the
StringBuilderto build the number. Ensures initialization in case of lazy-init.
-
sign
public boolean sign(char signChar) Description copied from interface:CharScannerNumberParserThis method will only be called if the first character if the number is '+' or '-'.- Specified by:
signin interfaceCharScannerNumberParser- Parameters:
signChar- '+' for positive number and '-' for negative number. If no sign is present this method will never be called.- Returns:
trueif the sign shall be accepted and further characters shall be received,falseto 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:CharScannerRadixHandlerThis method will be called if the first character is '0' and will also pass a lookahead of the next character as parametersymbol.- Specified by:
radixin interfaceCharScannerRadixHandler- Parameters:
newRadix- the radix. Will be16for "0x",2for "0b",8for "0" followed by an octal digit (0-7), and0in 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 returning8ifsymbolis '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 be10. Typically implementations will return the givenradix, but to prevent octal parsing due to a leading zero (Java/C legacy) you can return10if8was given. If8is 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 aNumberFormatException).
-
appendRadix
protected void appendRadix()Appends the radix to the numberStringBuilder. -
digit
public boolean digit(int digit, char digitChar) - Specified by:
digitin interfaceCharScannerNumberParser- Parameters:
digit- the parseddigitCharas numeric digit to "append". Will never be negative but may be greater or equal to the radix returned byCharScannerRadixHandler.radix(int, char)asNumberFormatExceptionhas to be handled inside the receiver.digitChar- the original digit character. In case the number shall be received asStringthis makes your life simpler and allows to preserve the case.- Returns:
trueif the given digit is accepted,falseotherwise (exceeds the range of the number to parse and the digit should not be consumed). Typical implementations should always returntrue.
-
resetTrailingZeros
protected void resetTrailingZeros()Resets the trailing zeros if a non zero digit was found for mantissa. -
isDecimal
protected abstract boolean isDecimal() -
dot
public boolean dot()- Specified by:
dotin interfaceCharScannerNumberParser- Returns:
trueif the decimal dot ('.') shall be accepted,falseotherwise (stop further processing e.g. to parse only integer numbers).
-
exponent
public boolean exponent(char e, char signChar) - Specified by:
exponentin interfaceCharScannerNumberParser- 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 '-') or0for no sign.- Returns:
trueif the scientific notation exponent is supported and the characters shall be consumed,falseotherwise (stop further processing and do not consume characters).
-
appendExponent
protected void appendExponent(boolean lazy) Appends theexponentsymbols. -
special
Description copied from interface:CharScannerNumberParserThis 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 returnnullhere.- Specified by:
specialin interfaceCharScannerNumberParser- Parameters:
c- the special charater that was found (no digit, no dot, no exponent).- Returns:
nullto stop without consuming the given character or aStringthat is expected (and shall start with the given special character). If thatStringwas found in the scanner,CharScannerNumberParser.special(String)is called. Otherwise again not even the given characters gets consumed.
-
special
- Specified by:
specialin interfaceCharScannerNumberParser- Parameters:
special- the specialStringthat was found and consumed.- See Also:
-
isValidDelimiterPosition
protected final boolean isValidDelimiterPosition()- Returns:
trueif the current position and state is valid to accept a digit delimiter.
-
toString
-