sharkysoft.com

com.sharkysoft.printf.engine
Class StringFormatter

java.lang.Object
  extended bycom.sharkysoft.printf.engine.StringFormatter
Direct Known Subclasses:
NumberFormatter

public class StringFormatter
extends java.lang.Object

Justifies text in field.

Details: A StringFormatter formats arbitrary length strings into a fixed-length field according to specifiable formatting criteria.

To justify a string in a field, create an instance of StringFormatter, configure the formatting properties, and then process the string through the justifier using format.

Example: Output the contents of vasNames, an array of strings, with one element per line, right-justified in a 50-character field. For strings shorter than 50 characters, fill in the open space on the left with an underscore character.

String[] vasNames = {"apple", "banana", "cherry"};
StringFormatter vpJustifier = new StringFormatter();
vpJustifier.setFieldWidth(50);
vpJustifier.setAlignment(AlignmentMode.gpRight);
vpJustifier.setPadChar('_');
for (int vnI = 0; vnI < vasNames.length; ++ i)
  System.out.println(vpJustifier.format(vasNames[i]));

The output of this code is:

_____________________________________________apple
____________________________________________banana
____________________________________________cherry

Author:
Sharky

Field Summary
protected  char mcPadChar
          Padding character.
protected  int mnAlignment
          Alignment mode.
protected  int mnCropping
          Cropping mode.
protected  int mnFieldWidth
          Field width.
 
Constructor Summary
StringFormatter()
          Default constructor.
 
Method Summary
 java.lang.String format(java.lang.String isText)
          Justifies text.
 java.lang.String format(java.lang.String isPrefix, java.lang.String isSuffix)
          Justifies text.
 AlignmentMode getAlignment()
          Retrieves Alignment property.
 CroppingMode getCropping()
          Retrieves Cropping property.
 int getFieldWidth()
          Retrieves FieldWidth property.
 char getPadChar()
          Retrieves PadChar property.
 void setAlignment(AlignmentMode ipAlignment)
          Updates Alignment property.
 void setCropping(CroppingMode ipCropping)
          Updates Cropping property.
 void setFieldWidth(int inFieldWidth)
          Updates FieldWidth property.
 void setPadChar(char icPadChar)
          Updates PadChar property.
 java.lang.String toString()
          Generates debug output.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

mnFieldWidth

protected int mnFieldWidth
Field width.

Details:

Property FieldWidth is the width of the field into which text will be formatted.

Default value: 0.

See Also:
getFieldWidth(), setFieldWidth(int)

mnAlignment

protected int mnAlignment
Alignment mode.

Details: Property Alignment controls the positioning of text within the field. Available alignment options include left justification, right justification, full justification, and centering.

Default value: AlignmentMode.gpLeft.

See Also:
getAlignment(), setAlignment(AlignmentMode)

mcPadChar

protected char mcPadChar
Padding character.

Details: Property PadChar is the character used to fill the unused portion of the field when the text is shorter than the field.

Default value: ' ' (space character).

See Also:
getPadChar(), setPadChar(char)

mnCropping

protected int mnCropping
Cropping mode.

Details: Property Cropping controls whether and how text is compressed if it is larger than the field into which it is formatted. The head, tail, or center portion of the text may be cropped to ensure that the formatted text fits within the field. Or, cropping may be prohibited, so that long text actually overflows the field.

Default value: CroppingMode.gpNone.

See Also:
getCropping(), setCropping(CroppingMode)
Constructor Detail

StringFormatter

public StringFormatter()
Default constructor.

Method Detail

getFieldWidth

public int getFieldWidth()
Retrieves FieldWidth property.

Returns:
current value
See Also:
mnFieldWidth

setFieldWidth

public void setFieldWidth(int inFieldWidth)
Updates FieldWidth property.

Parameters:
inFieldWidth - new value
See Also:
mnFieldWidth

getAlignment

public AlignmentMode getAlignment()
Retrieves Alignment property.

Returns:
current value
See Also:
mnAlignment

setAlignment

public void setAlignment(AlignmentMode ipAlignment)
Updates Alignment property.

Parameters:
ipAlignment - new value
See Also:
mnAlignment

getPadChar

public char getPadChar()
Retrieves PadChar property.

Returns:
current value
See Also:
mcPadChar

setPadChar

public void setPadChar(char icPadChar)
Updates PadChar property.

Parameters:
icPadChar - new value
See Also:
mcPadChar

getCropping

public CroppingMode getCropping()
Retrieves Cropping property.

Returns:
current value
See Also:
mnCropping

setCropping

public void setCropping(CroppingMode ipCropping)
Updates Cropping property.

Parameters:
ipCropping - new value
See Also:
mnCropping

format

public final java.lang.String format(java.lang.String isText)
Justifies text.

Details: format formats the supplied string according to currently configured properties.

If Alignment is set to FULL, then the split-string format method (format(String, String)) should be used instead. This is because the format method with only one string parameter does not know where the filler characters should be inserted. Eventually, this method may be upgraded to support automatic expansion of whitespace characters within the string for full justification.

Parameters:
isText - the text to justify
Returns:
the formatted string

format

public final java.lang.String format(java.lang.String isPrefix,
                                     java.lang.String isSuffix)
Justifies text.

Details: format justifies a split string according to the currently configured properties. If Alignment is set to FULL, then the prefix is left-aligned and the suffix is right-aligned, and the portion between them is filled with the padding character. Otherwise, the strings are simply concatenated together and formatted as in format(String).

Parameters:
isPrefix - the left portion of the string
isSuffix - the right portion of the string
Returns:
the formatted string

toString

public java.lang.String toString()
Generates debug output.

Details: toString generates a string representation of this instance and its properties. This output is useful only for debugging.

Returns:
string representation

sharkysoft.com

Copyright © 1997-2004 Sharkysoft (sharkysoft.com). All rights reserved.