Package com.jgoodies.common.format
Class EmptyNumberFormat
- java.lang.Object
-
- java.text.Format
-
- java.text.NumberFormat
-
- com.jgoodies.common.format.EmptyNumberFormat
-
- All Implemented Interfaces:
java.io.Serializable
,java.lang.Cloneable
public final class EmptyNumberFormat extends java.text.NumberFormat
Wraps a givenNumberFormat
and adds behavior to convert to/from the empty string. Therefore it holds an empty value that is mapped to/from the empty string. The#format
result of the empty value is the empty string, and the#parse
result of the empty string is the empty value. In all other cases the formatting and parsing is forwarded to the wrapped NumberFormat.Examples:
new EmptyNumberFormat(NumberFormat().getInstance()); new EmptyNumberFormat(NumberFormat().getIntegerInstance(), -1);
- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description private java.text.NumberFormat
delegate
private java.lang.Number
emptyValue
-
Constructor Summary
Constructors Constructor Description EmptyNumberFormat(java.text.NumberFormat delegate)
Constructs an EmptyNumberFormat that wraps the given mandatory format to convertnull
to the empty string and vice versa.EmptyNumberFormat(java.text.NumberFormat delegate, int emptyValue)
Constructs an EmptyNumberFormat that wraps the given mandatory format to convert the givenemptyValue
to the empty string and vice versa.EmptyNumberFormat(java.text.NumberFormat delegate, java.lang.Number emptyValue)
Constructs an EmptyNumberFormat that wraps the given mandatory format to convert the givenemptyValue
to the empty string and vice versa.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description java.lang.StringBuffer
format(double number, java.lang.StringBuffer toAppendTo, java.text.FieldPosition pos)
java.lang.StringBuffer
format(long number, java.lang.StringBuffer toAppendTo, java.text.FieldPosition pos)
java.lang.StringBuffer
format(java.lang.Object obj, java.lang.StringBuffer toAppendTo, java.text.FieldPosition pos)
java.lang.Number
parse(java.lang.String source)
java.lang.Number
parse(java.lang.String source, java.text.ParsePosition pos)
java.lang.Object
parseObject(java.lang.String source)
-
Methods inherited from class java.text.NumberFormat
clone, equals, format, format, getAvailableLocales, getCurrency, getCurrencyInstance, getCurrencyInstance, getInstance, getInstance, getIntegerInstance, getIntegerInstance, getMaximumFractionDigits, getMaximumIntegerDigits, getMinimumFractionDigits, getMinimumIntegerDigits, getNumberInstance, getNumberInstance, getPercentInstance, getPercentInstance, getRoundingMode, hashCode, isGroupingUsed, isParseIntegerOnly, parseObject, setCurrency, setGroupingUsed, setMaximumFractionDigits, setMaximumIntegerDigits, setMinimumFractionDigits, setMinimumIntegerDigits, setParseIntegerOnly, setRoundingMode
-
-
-
-
Constructor Detail
-
EmptyNumberFormat
public EmptyNumberFormat(java.text.NumberFormat delegate)
Constructs an EmptyNumberFormat that wraps the given mandatory format to convertnull
to the empty string and vice versa.- Parameters:
delegate
- the format that handles the standard cases- Throws:
java.lang.NullPointerException
- ifdelegate
isnull
-
EmptyNumberFormat
public EmptyNumberFormat(java.text.NumberFormat delegate, int emptyValue)
Constructs an EmptyNumberFormat that wraps the given mandatory format to convert the givenemptyValue
to the empty string and vice versa.- Parameters:
delegate
- the format that handles non-null
valuesemptyValue
- the representation of the empty string- Throws:
java.lang.NullPointerException
- ifdelegate
isnull
-
EmptyNumberFormat
public EmptyNumberFormat(java.text.NumberFormat delegate, java.lang.Number emptyValue)
Constructs an EmptyNumberFormat that wraps the given mandatory format to convert the givenemptyValue
to the empty string and vice versa.- Parameters:
delegate
- the format that handles non-null
valuesemptyValue
- the representation of the empty string- Throws:
java.lang.NullPointerException
- ifdelegate
isnull
-
-
Method Detail
-
format
public java.lang.StringBuffer format(java.lang.Object obj, java.lang.StringBuffer toAppendTo, java.text.FieldPosition pos)
If
obj
is equal to the emptyValue,toAppendTo
is returned. Otherwise the format is forwarded to the delegate.- Overrides:
format
in classjava.text.NumberFormat
-
format
public java.lang.StringBuffer format(double number, java.lang.StringBuffer toAppendTo, java.text.FieldPosition pos)
- Specified by:
format
in classjava.text.NumberFormat
-
format
public java.lang.StringBuffer format(long number, java.lang.StringBuffer toAppendTo, java.text.FieldPosition pos)
- Specified by:
format
in classjava.text.NumberFormat
-
parseObject
public java.lang.Object parseObject(java.lang.String source) throws java.text.ParseException
If
source
is empty or whitespace, the emptyValue is returned. Otherwise parsing is forwarded to the delegate - indirectly viaparse(String, ParsePosition)
.- Overrides:
parseObject
in classjava.text.Format
- Throws:
java.text.ParseException
-
parse
public java.lang.Number parse(java.lang.String source) throws java.text.ParseException
If
source
is empty or whitespace, the emptyValue is returned. Otherwise parsing is forwarded to the delegate - indirectly viaparse(String, ParsePosition)
.- Overrides:
parse
in classjava.text.NumberFormat
- Throws:
java.text.ParseException
-
parse
public java.lang.Number parse(java.lang.String source, java.text.ParsePosition pos)
- Specified by:
parse
in classjava.text.NumberFormat
-
-