public class NumberFormatter extends NumberFormatParser
Modifier and Type | Field and Description |
---|---|
private java.lang.Runnable |
deferredError
Any error related to a value being formatted occurred before the format parse is complete.
|
private boolean |
digitsVisited
Set to
true by if any of visitLeftDigits(int, int) or
visitRightDigits(int, int, int) callbacks was called |
private java.lang.String |
format
The format string, used for error reporting from callbacks
|
private boolean |
isNegative
true if we are formatting a negative number. |
private java.lang.String |
leftDigits
The left (integer) part of the formatted number
|
private java.lang.String |
rightDigits
The right (fraction) part of the formatted number
|
private java.lang.StringBuilder |
sb
Collector for the output formatted value
|
private boolean |
signVisited
Set to
true by visitSign(int, int) digits callback |
private java.math.BigDecimal |
value
The value being formatted
|
Constructor and Description |
---|
NumberFormatter(java.math.BigDecimal value,
java.lang.String fmt)
The constructor.
|
Modifier and Type | Method and Description |
---|---|
java.lang.String |
format()
Do the formatting.
|
private void |
setDeferredError(java.lang.String reportedValue)
Set the deferred error action.
|
protected void |
visitFillChar(char c)
A fill character was parsed.
|
protected void |
visitLeftDigits(int startIdx,
int endIdx)
Non-empty left digits section was parsed.
|
protected void |
visitRightDigits(int startIdx,
int endIdx,
int separators)
Right digits section was parsed (may be empty).
|
protected void |
visitSign(int startIdx,
int endIdx)
A sign spec was parsed.
|
parse
private java.lang.StringBuilder sb
private final java.math.BigDecimal value
private final boolean isNegative
true
if we are formatting a negative number.private final java.lang.String leftDigits
private final java.lang.String rightDigits
private boolean digitsVisited
true
by if any of visitLeftDigits(int, int)
or
visitRightDigits(int, int, int)
callbacks was calledprivate boolean signVisited
true
by visitSign(int, int)
digits callbackprivate final java.lang.String format
private java.lang.Runnable deferredError
public NumberFormatter(java.math.BigDecimal value, java.lang.String fmt)
value
- the value to formatfmt
- the format stringpublic final java.lang.String format()
protected void visitFillChar(char c)
visitFillChar
in class NumberFormatParser
c
- the characterprotected void visitSign(int startIdx, int endIdx)
visitSign
in class NumberFormatParser
startIdx
- the section start index in format string, zero-based, inclusiveendIdx
- the section end index in format string, zero-based, exclusiveprotected void visitLeftDigits(int startIdx, int endIdx)
visitLeftDigits
in class NumberFormatParser
startIdx
- the section start index in format string, zero-based, inclusiveendIdx
- the section end index in format string, zero-based, exclusiveprotected void visitRightDigits(int startIdx, int endIdx, int separators)
visitRightDigits
in class NumberFormatParser
startIdx
- the section start index in format string, zero-based, inclusiveendIdx
- the section end index in format string, zero-based, exclusiveseparators
- the number of digit separator ',' character in right digitsprivate final void setDeferredError(java.lang.String reportedValue)
Implementation should call this method only once when the first value-related error is detected.
reportedValue
- the value as string to use in the error message