com.jgoodies.common.format
Class EmptyFormat

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

public class EmptyFormat
extends Format

Wraps a given Format and adds behavior to convert to/from the empty string. Therefore it holds an empty value (often null) 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 Format.

If you want to wrap a DateFormat or NumberFormat, you may use EmptyDateFormat or EmptyNumberFormat resp. Examples:

 new EmptyFormat(new WeightFormat());
 

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

Nested Class Summary
 
Nested classes/interfaces inherited from class java.text.Format
Format.Field
 
Constructor Summary
EmptyFormat(Format delegate)
          Constructs an EmptyFormat that wraps the given mandatory format to convert null to the empty string and vice versa.
EmptyFormat(Format delegate, Object emptyValue)
          Constructs an EmptyFormat that wraps the given mandatory format to convert the given emptyValue to the empty string and vice versa.
 
Method Summary
 StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos)
           If obj is equal to the emptyValue, toAppendTo is returned.
 AttributedCharacterIterator formatToCharacterIterator(Object obj)
           
 Object parseObject(String source)
           If source is empty or whitespace, the emptyValue is returned.
 Object parseObject(String source, ParsePosition pos)
           
 
Methods inherited from class java.text.Format
clone, format
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

EmptyFormat

public EmptyFormat(Format delegate)
Constructs an EmptyFormat 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:
NullPointerException - if delegate is null

EmptyFormat

public EmptyFormat(Format delegate,
                   Object emptyValue)
Constructs an EmptyFormat 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:
NullPointerException - if delegate is null
Method Detail

format

public StringBuffer format(Object obj,
                           StringBuffer toAppendTo,
                           FieldPosition pos)

If obj is equal to the emptyValue, toAppendTo is returned. Otherwise the format is forwarded to the delegate.

Specified by:
format in class Format

parseObject

public Object parseObject(String source)
                   throws ParseException

If source is empty or whitespace, the emptyValue is returned. Otherwise parsing is forwarded to the delegate.

Overrides:
parseObject in class Format
Throws:
ParseException

parseObject

public final Object parseObject(String source,
                                ParsePosition pos)
Specified by:
parseObject in class Format

formatToCharacterIterator

public final AttributedCharacterIterator formatToCharacterIterator(Object obj)
Overrides:
formatToCharacterIterator in class Format


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