org.jcoderz.commons.logging
Class WhitespaceFormat

java.lang.Object
  extended by java.text.Format
      extended by org.jcoderz.commons.logging.WhitespaceFormat
All Implemented Interfaces:
Serializable, Cloneable

public final class WhitespaceFormat
extends Format

This Formatter formats the whitespace of a string, The space char ' ' is left untouched, all other whitespace chars are replaced and compressed by exactly one space char in a row. It extends the Format type in a unsymmetric way: A formatted String cannot be parsed in a way that the result is equal to the source string. If allocated with a sub format, first the sub format is used for formatting, the rewsult will be formatted by this. In addition to the Format implementation it offers static access methods for the format functionality.

See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class java.text.Format
Format.Field
 
Constructor Summary
WhitespaceFormat()
          Creates a new instance of this with no sub format.
WhitespaceFormat(Format subFormat)
          Creates a new instance of this with the supplied sub format.
 
Method Summary
static CharBuffer format(CharBuffer message)
          Replaced and reduces whitespace in the supplied character buffer.
 StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos)
          If a sub format is set, it uses this to format the object and compresses the whitespace within the result.
static String format(String message)
          Replaces and reduces whitespace in the supplied message.
 Object parseObject(String source, ParsePosition pos)
          If a sub format is set, it delegates parsing to the sub format.
 
Methods inherited from class java.text.Format
clone, format, formatToCharacterIterator, parseObject
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

WhitespaceFormat

public WhitespaceFormat()
Creates a new instance of this with no sub format.


WhitespaceFormat

public WhitespaceFormat(Format subFormat)
Creates a new instance of this with the supplied sub format.

Parameters:
subFormat - The sub format to use for first step formatting of an object. This will be used for parsing an object as well. Might be null.
Method Detail

format

public static String format(String message)
Replaces and reduces whitespace in the supplied message. The resulting string will only have ' ' as white space. Any such character in the source string is left untouched, all other whitespace characters are replaced by ' ', but with only one in a row, so, for example, a sequence of 2 line separators will be replaced by one ' '.

Parameters:
message - The message in which to find and replace white space.
Returns:
String with replaced and reduced white space

format

public static CharBuffer format(CharBuffer message)
Replaced and reduces whitespace in the supplied character buffer.

Parameters:
message - The message buffer in which to find and replace white space.
Returns:
CharBuffer with replaced and reduced white space. This might be message if it does not contain whitespace to replace.
See Also:
format(String)

parseObject

public Object parseObject(String source,
                          ParsePosition pos)
If a sub format is set, it delegates parsing to the sub format. If no subformat is set, it takes the source string until the first whitespace char is found, which is not PRESERVED_CHAR.

Specified by:
parseObject in class Format
See Also:
Format.parseObject(java.lang.String, java.text.ParsePosition)

format

public StringBuffer format(Object obj,
                           StringBuffer toAppendTo,
                           FieldPosition pos)
If a sub format is set, it uses this to format the object and compresses the whitespace within the result. If no sub format is set, it expects a String object and compresses the whitespace on that.

Specified by:
format in class Format
Parameters:
obj - The object to format.
toAppendTo - The string buffer where to append to the formatted object.
pos - The field position for formatting.
Returns:
StringBuffer with formatted objects.
See Also:
Format.format(java.lang.Object, java.lang.StringBuffer, java.text.FieldPosition)


Copyright 2007 The jCoderZ Project.