Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

com.jgoodies.validation.tutorial.formatted.format
Class EmptyFormat  view EmptyFormat download EmptyFormat.java

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

public final class EmptyFormat
extends java.text.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.4 $

Nested Class Summary
 
Nested classes inherited from class java.text.Format
java.text.Format.Field
 
Field Summary
private  java.lang.Object emptyValue
          Holds the object that represents the empty value.
private  java.text.Format format
          Refers to the wrapped Format that is used to forward #format and #parseObject.
 
Fields inherited from class java.text.Format
 
Constructor Summary
EmptyFormat(java.text.Format format)
          Constructs an EmptyFormat that wraps the given format to convert null to the empty string and vice versa.
EmptyFormat(java.text.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(java.text.Format format, java.lang.Object emptyValue)
          Constructs an EmptyFormat that wraps the given format to convert the given emptyValue to the empty string and vice versa.
 
Method Summary
private  boolean equals(java.lang.Object o1, java.lang.Object o2)
          Checks and answers if the two objects are both null or equal.
 java.lang.StringBuffer format(java.lang.Object obj, java.lang.StringBuffer toAppendTo, java.text.FieldPosition pos)
          Formats an object and appends the resulting text to a given string buffer.
 java.lang.Object parseObject(java.lang.String source)
          Parses text from the beginning of the given string to produce an object.
 java.lang.Object parseObject(java.lang.String source, java.text.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
 

Field Detail

format

private final java.text.Format format
Refers to the wrapped Format that is used to forward #format and #parseObject.


emptyValue

private final java.lang.Object emptyValue
Holds the object that represents the empty value. The result of formatting this value is the empty string; the result of parsing an empty string is this object.

Constructor Detail

EmptyFormat

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


EmptyFormat

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


EmptyFormat

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

Method Detail

format

public java.lang.StringBuffer format(java.lang.Object obj,
                                     java.lang.StringBuffer toAppendTo,
                                     java.text.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.


parseObject

public java.lang.Object parseObject(java.lang.String source)
                             throws java.text.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.


parseObject

public java.lang.Object parseObject(java.lang.String source,
                                    java.text.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.


equals

private boolean equals(java.lang.Object o1,
                       java.lang.Object o2)
Checks and answers if the two objects are both null or equal.