org.jcoderz.commons.logging
Class FixLengthFormat

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

public class FixLengthFormat
extends Format

This is used to format a String to a fixed length String. Strings, which are are longer than the configured length, are split after the appropriate number of chars; Strings which are shorter, are left padded with the configured padding char. When parsing a String, the configured number of chars is read from the source String and the padding char is left trimmed from the resulting String.

See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class java.text.Format
Format.Field
 
Field Summary
static org.jcoderz.commons.logging.FixLengthFormat.Padding LEFT_CUT_RIGHT_PADDING
          The string is right padded with the padding char to get the full sized string or cut on the left side if it dues not fit the length.
static org.jcoderz.commons.logging.FixLengthFormat.Padding LEFT_PADDING
          The string is left padded with the padding char to get the full sized string.
static org.jcoderz.commons.logging.FixLengthFormat.Padding RIGHT_PADDING
          The string is right padded with the padding char to get the full sized string.
 
Constructor Summary
FixLengthFormat(int fixedLength, org.jcoderz.commons.logging.FixLengthFormat.Padding type)
          Generates an instance of this with the default padding character, which is a space.
FixLengthFormat(int fixedLength, org.jcoderz.commons.logging.FixLengthFormat.Padding type, char paddingChar)
          Generates an instance of this with paddingChar as padding character.
 
Method Summary
 StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos)
          Formats a string to be of a certain length.
 Object parseObject(String source, ParsePosition pos)
          Parses the source string for a padded string from the position given by pos.
 
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
 

Field Detail

LEFT_PADDING

public static final org.jcoderz.commons.logging.FixLengthFormat.Padding LEFT_PADDING
The string is left padded with the padding char to get the full sized string.


RIGHT_PADDING

public static final org.jcoderz.commons.logging.FixLengthFormat.Padding RIGHT_PADDING
The string is right padded with the padding char to get the full sized string.


LEFT_CUT_RIGHT_PADDING

public static final org.jcoderz.commons.logging.FixLengthFormat.Padding LEFT_CUT_RIGHT_PADDING
The string is right padded with the padding char to get the full sized string or cut on the left side if it dues not fit the length.

Constructor Detail

FixLengthFormat

public FixLengthFormat(int fixedLength,
                       org.jcoderz.commons.logging.FixLengthFormat.Padding type)
Generates an instance of this with the default padding character, which is a space.

Parameters:
fixedLength - all formatted strings will have this length, either they are cut or padded with spaces.
type - the type of padding, either LEFT_PADDING or RIGHT_PADDING
Throws:
IllegalArgumentException - if fixedLength <= 0

FixLengthFormat

public FixLengthFormat(int fixedLength,
                       org.jcoderz.commons.logging.FixLengthFormat.Padding type,
                       char paddingChar)
Generates an instance of this with paddingChar as padding character.

Parameters:
fixedLength - all formatted strings will have this length, either they are cut or padded with paddingChar
type - the type of padding, either LEFT_PADDING or RIGHT_PADDING
paddingChar - the character to use for padding.
Throws:
IllegalArgumentException - if fixedLength <= 0
Method Detail

parseObject

public Object parseObject(String source,
                          ParsePosition pos)
Parses the source string for a padded string from the position given by pos. It reads the configured number of chars and trims all chars equal to the padding character, either from left or from right, according to the configured type of this. If the given source string is not long enough, the index of pos is not modified, but the error index is set to its current index and null is returned.

Specified by:
parseObject in class Format
Parameters:
source - the string to parse
pos - the parse position where to start parsing within source. This will be updated after parsing.
Returns:
null if not enough chars to read from source; else parsed String, one-side trimmed from padding char.
See Also:
Format.parseObject(java.lang.String, java.text.ParsePosition)

format

public StringBuffer format(Object obj,
                           StringBuffer toAppendTo,
                           FieldPosition pos)
Formats a string to be of a certain length. If the string is longer than the configured length; it is cut and the first part being appended to the StringBuffer, if it is shorter, this call pads it with the configured char.

Specified by:
format in class Format
Parameters:
obj - the String to be formatted, must not be null.
toAppendTo - the StringBuffer to which to append the formatted String
pos - field position
Returns:
StringBuffer where the formatted object has been appended to.
See Also:
Format.format(java.lang.Object, java.lang.StringBuffer, java.text.FieldPosition)


Copyright 2007 The jCoderZ Project.