Sharkysoft home

lava.text
Class StringJustifier

java.lang.Object
  |
  +--lava.text.StringJustifier
Direct Known Subclasses:
NumberFormatter

public class StringJustifier
extends java.lang.Object

A StringJustifier formats strings according to criteria selected by the user. The user can control various formatting parameters, such as field width, justification style (left-align, right-align, center, etc.), padding characters, etc.

Since applications which must format text frequently format more than one piece of text in the same style, this class is designed to "remember" the selected formatting parameters so that the settings can be reused with each format call. The reduces the amount of parameters which must be passed and results in more efficient execution.

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

String[] names = {"apple", "banana", "cherry"}; StringJustifier justifier = new StringJustifier (); justifier . field_width = 50; justifier . cropping = JUSTIFY.RIGHT; justifier . pad_char = '_'; for (int i = 0; i < names . length; ++ i) System.out . println (justifier . format (names [i]));

The output of this code is:

_____________________________________________apple ____________________________________________banana ____________________________________________cherry

The formatting parameters can be updated at any time, and doing so will change the way text is formatted afterwards. For a detailed description of user-controllable parameters, see the documentation for each parameter below.


Field Summary
 int cropping
          Direction of cropping.
 int field_width
          Width of the field in which the text should be justified.
 int justification
          Direction of justification.
 char pad_char
          Character to use for field padding.
 
Constructor Summary
StringJustifier()
          Initializes a new StringJustifier with default settings.
 
Method Summary
 java.lang.String format(java.lang.String string)
          Formats a string according to currently selected criteria.
 java.lang.String format(java.lang.String prefix, java.lang.String suffix)
          Formats a split string according to selected criteria.
 java.lang.String toString()
          Generates a string representation this StringJustifier.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

field_width

public int field_width
Width of the field in which the text should be justified. Default value: 0.

justification

public int justification
Direction of justification. Set to one of the JUSTIFY constants. Default value: JUSTIFY.RIGHT.

pad_char

public char pad_char
Character to use for field padding. (Default value: ' ' (the space character).)

cropping

public int cropping
Direction of cropping. Used only when the text being justified is larger than the field. Set to one of the CROP constants. (Default value: CROP.NONE.)
Constructor Detail

StringJustifier

public StringJustifier()
Initializes a new StringJustifier with default settings.
Method Detail

format

public final java.lang.String format(java.lang.String string)
Formats a string according to currently selected criteria. If justification is set to Justification.FULL, then the split-string format method (format(String,String)) must be used (for now).
Parameters:
string - the string to justify

format

public final java.lang.String format(java.lang.String prefix,
                                     java.lang.String suffix)
Formats a split string according to selected criteria. If justification is set to Justification.FULL, then the prefix is left-aligned and the suffix is right-aligned. Otherwise, the strings are concatenated together and the resulting string is justified according to selected criteria.
Parameters:
prefix - the left portion of the string
suffix - the right portion of the string

toString

public java.lang.String toString()
Generates a string representation this StringJustifier. Used primarily for debugging.
Overrides:
toString in class java.lang.Object

Sharkysoft home