Class EmptyFormat

  • All Implemented Interfaces:
    java.io.Serializable, java.lang.Cloneable

    public 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 (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());
     
    See Also:
    Serialized Form
    • Nested Class Summary

      • Nested classes/interfaces inherited from class java.text.Format

        java.text.Format.Field
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.text.Format delegate
      Refers to the wrapped Format that is used to forward #format and #parseObject.
      private java.lang.Object emptyValue
      Holds the object that represents the empty value.
    • Constructor Summary

      Constructors 
      Constructor Description
      EmptyFormat​(java.text.Format delegate)
      Constructs an EmptyFormat that wraps the given mandatory format to convert null to the empty string and vice versa.
      EmptyFormat​(java.text.Format delegate, java.lang.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

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.lang.StringBuffer format​(java.lang.Object obj, java.lang.StringBuffer toAppendTo, java.text.FieldPosition pos)
      java.text.AttributedCharacterIterator formatToCharacterIterator​(java.lang.Object obj)  
      java.lang.Object parseObject​(java.lang.String source)
      java.lang.Object parseObject​(java.lang.String source, java.text.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
    • Field Detail

      • delegate

        private final java.text.Format delegate
        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 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:
        java.lang.NullPointerException - if delegate is null
      • EmptyFormat

        public EmptyFormat​(java.text.Format delegate,
                           java.lang.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:
        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.

        Specified by:
        format in class java.text.Format
      • 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.

        Overrides:
        parseObject in class java.text.Format
        Throws:
        java.text.ParseException
      • parseObject

        public final java.lang.Object parseObject​(java.lang.String source,
                                                  java.text.ParsePosition pos)
        Specified by:
        parseObject in class java.text.Format
      • formatToCharacterIterator

        public final java.text.AttributedCharacterIterator formatToCharacterIterator​(java.lang.Object obj)
        Overrides:
        formatToCharacterIterator in class java.text.Format