com.jgoodies.common.format
Class EmptyNumberFormat

java.lang.Object
  extended by java.text.Format
      extended by java.text.NumberFormat
          extended by com.jgoodies.common.format.EmptyNumberFormat
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable

public final class EmptyNumberFormat
extends java.text.NumberFormat

Wraps a given NumberFormat 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);
 

Version:
$Revision: 1.6 $
Author:
Karsten Lentzsch
See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class java.text.NumberFormat
java.text.NumberFormat.Field
 
Field Summary
 
Fields inherited from class java.text.NumberFormat
FRACTION_FIELD, INTEGER_FIELD
 
Constructor Summary
EmptyNumberFormat(java.text.NumberFormat delegate)
          Constructs an EmptyNumberFormat that wraps the given mandatory format to convert null 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 given emptyValue 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 given emptyValue to the empty string and vice versa.
 
Method Summary
 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
 
Methods inherited from class java.text.Format
format, formatToCharacterIterator
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

EmptyNumberFormat

public EmptyNumberFormat(java.text.NumberFormat delegate)
Constructs an EmptyNumberFormat that wraps the given mandatory format to convert null to the empty string and vice versa.

Parameters:
delegate - the format that handles the standard cases
Throws:
java.lang.NullPointerException - if delegate is null

EmptyNumberFormat

public EmptyNumberFormat(java.text.NumberFormat delegate,
                         int emptyValue)
Constructs an EmptyNumberFormat that wraps the given mandatory format to convert the given emptyValue to the empty string and vice versa.

Parameters:
delegate - the format that handles non-null values
emptyValue - the representation of the empty string
Throws:
java.lang.NullPointerException - if delegate is null

EmptyNumberFormat

public EmptyNumberFormat(java.text.NumberFormat delegate,
                         java.lang.Number emptyValue)
Constructs an EmptyNumberFormat that wraps the given mandatory format to convert the given emptyValue to the empty string and vice versa.

Parameters:
delegate - the format that handles non-null values
emptyValue - the representation of the empty string
Throws:
java.lang.NullPointerException - if delegate is null
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 class java.text.NumberFormat

format

public java.lang.StringBuffer format(double number,
                                     java.lang.StringBuffer toAppendTo,
                                     java.text.FieldPosition pos)
Specified by:
format in class java.text.NumberFormat

format

public java.lang.StringBuffer format(long number,
                                     java.lang.StringBuffer toAppendTo,
                                     java.text.FieldPosition pos)
Specified by:
format in class java.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 via parse(String, ParsePosition).

Overrides:
parseObject in class java.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 via parse(String, ParsePosition).

Overrides:
parse in class java.text.NumberFormat
Throws:
java.text.ParseException

parse

public java.lang.Number parse(java.lang.String source,
                              java.text.ParsePosition pos)
Specified by:
parse in class java.text.NumberFormat


Copyright © 2009-2012 JGoodies Karsten Lentzsch. All Rights Reserved.