com.jgoodies.validation.tutorial.formatted.format
Class EmptyFormat

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

public final class EmptyFormat
extends Format

Wraps a given Format and adds behavior to convert to/from the empty string. Therefore it holds an empty value that is the counterpart of 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.

Often the empty value is null. For example you can wrap a DateFormat to format null to an empty string and parse the empty string to null. Another example is the mapping of the -1 to an empty string using a wrapped NumberFormat. Examples:

 new EmptyFormat(new NumberFormat());
 new EmptyFormat(new NumberFormat(), -1);

 new EmptyFormat(DateFormat.getDateInstance());
 new EmptyFormat(DateFormat.getDateInstance(DateFormat.SHORT));
 

Version:
$Revision: 1.12 $
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 format)
          Constructs an EmptyFormat that wraps the given format to convert null to the empty string and vice versa.
EmptyFormat(Format format, int emptyValue)
          Constructs an EmptyFormat that wraps the given format to convert the given emptyValue to the empty string and vice versa.
EmptyFormat(Format format, Object emptyValue)
          Constructs an EmptyFormat that wraps the given format to convert the given emptyValue to the empty string and vice versa.
 
Method Summary
 StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos)
          Formats an object and appends the resulting text to a given string buffer.
 Object parseObject(String source)
          Parses text from the beginning of the given string to produce an object.
 Object parseObject(String source, ParsePosition pos)
          Parses text from a string to produce an object.
 
Methods inherited from class java.text.Format
clone, format, formatToCharacterIterator
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

EmptyFormat

public EmptyFormat(Format format)
Constructs an EmptyFormat that wraps the given format to convert null to the empty string and vice versa.

Parameters:
format - the format that handles the standard cases

EmptyFormat

public EmptyFormat(Format format,
                   Object emptyValue)
Constructs an EmptyFormat that wraps the given format to convert the given emptyValue to the empty string and vice versa.

Parameters:
format - the format that handles non-null values
emptyValue - the representation of the empty string

EmptyFormat

public EmptyFormat(Format format,
                   int emptyValue)
Constructs an EmptyFormat that wraps the given format to convert the given emptyValue to the empty string and vice versa.

Parameters:
format - the format that handles non-null values
emptyValue - the representation of the empty string
Method Detail

format

public StringBuffer format(Object obj,
                           StringBuffer toAppendTo,
                           FieldPosition pos)
Formats an object and appends the resulting text to a given string buffer. If the pos argument identifies a field used by the format, then its indices are set to the beginning and end of the first such field encountered.

Specified by:
format in class Format
Parameters:
obj - The object to format
toAppendTo - where the text is to be appended
pos - A FieldPosition identifying a field in the formatted text
Returns:
the string buffer passed in as toAppendTo, with formatted text appended
Throws:
NullPointerException - if toAppendTo or pos is null
IllegalArgumentException - if the Format cannot format the given object

parseObject

public Object parseObject(String source)
                   throws ParseException
Parses text from the beginning of the given string to produce an object. The method may not use the entire text of the given string.

Unlike super this method returns the emptyValue if the source string is empty and does not throw a ParseException.

Overrides:
parseObject in class Format
Parameters:
source - A String whose beginning should be parsed.
Returns:
An Object parsed from the string.
Throws:
ParseException - if the beginning of the specified string cannot be parsed.
See Also:
Format.parseObject(java.lang.String), JFormattedTextField.setValue(java.lang.Object)

parseObject

public Object parseObject(String source,
                          ParsePosition pos)
Parses text from a string to produce an object.

The method attempts to parse text starting at the index given by pos. If parsing succeeds, then the index of pos is updated to the index after the last character used (parsing does not necessarily use all characters up to the end of the string), and the parsed object is returned. The updated pos can be used to indicate the starting point for the next call to this method. If an error occurs, then the index of pos is not changed, the error index of pos is set to the index of the character where the error occurred, and null is returned.

Specified by:
parseObject in class Format
Parameters:
source - A String, part of which should be parsed.
pos - A ParsePosition object with index and error index information as described above.
Returns:
An Object parsed from the string. In case of error, returns null.
Throws:
NullPointerException - if pos is null.


Copyright © 2003-2008 JGoodies Karsten Lentzsch. All Rights Reserved.