All Packages  Class Hierarchy  This Package  Previous  Next  Index

Class com.roguewave.format.QuickFormat

java.lang.Object
   |
   +----com.roguewave.format.QuickFormat

public class QuickFormat
extends Object
The QuickFormat class provides a convenient interface for the combined functionality of the TextPicture, TextAlignment, NumericPicture, and NumericFormat classes. Typical use would be to start with a picture string and then set formatting parameters and substitute in values:
 QuickFormat format =
   
   new QuickFormat("<tr><td> _ </td><td> _ </td><td> _ </td></tr>\n"); // javadoc bug: -->
 format.nfmt(0,2).arg(1.2).arg("2.4").npic("( N)").arg(-4.8).print(System.out);
 
produces: <tr><td> 1.20 </td><td> 2.4 </td><td> ( 4.80) </td></tr>

Writing code to produce formatted output using the above four classes directly can require a lot of typing; the QuickFormat class is a wrapper for the other classes which provides a terser output syntax:

   QuickFormat format = new QuickFormat("| ________ | ________ | ________ |").pre(2);
   if(d < 0)
     format.arg("").arg(-d).npic("(N)").arg(balance).println(System.out);
   else
     format.arg(d).arg("").npic("(N)").arg(balance).println(System.out);
 
The QuickFormat class interface is a mixture of C and C++ output styles: the use of TextPicture provides the intuitive readability of printf, while the chaining of methods is reminiscent of C++'s overloaded shift operator (each method returns a QuickFormat object on which the subsequent method is invoked).

A QuickFormat object is usually created using a TextPicture picture string; specifics of formatting and the subsitution of values is then done with method calls.

There are a number of arg methods which correspond to those in the TextPicture class with the exception that numeric values are formatted according to a NumericPicture object kept internally in QuickFormat objects.

A NumericFormat object is also held by QuickFormat for determining the width and precision of numeric arguments.

The npic(NumericPicture nl) method can be used to substitute a new NumericPicture object for the one in use. The nfmt(int width, int precision) method changes the width and precision of the current NumericFormat in use, while pre(int precision) changes only the precision.

Note that in order to make use of the most advanced features of NumericFormat, you will have to create your own NumericFormat object separate from QuickFormat and use the NumericFormat.format(n) method to obtain a string. That string can then be use with the QuickFormat.arg(String) method.

See QuickFormatExample2.java in the examples directory for yet another bank statement printer, this one based on the QuickFormat class. In the example, the NumericPicture has been modified to use parentheses for negative balances, giving as output:

 ----------------------------------
 | Credits  |  Debits  | Balance  |
 ----------------------------------
 |    50.00 |          |   50.00  |
 |          |     4.95 |   45.05  |
 |          |    49.99 |   (4.94) |
 |          |     8.50 |  (13.44) |
 |    50.00 |          |   36.56  |
 |          |    37.50 |   (0.94) |
 ----------------------------------
 

See Also:
TextPicture, TextAlignment, NumericPicture, NumericFormat

Constructor Index

 o QuickFormat(String)
Create a QuickFormat object using the given string for the TextPicture.
 o QuickFormat(String, String)
Create a QuickFormat object using the first string for the TextPicture and the second string as the default NumericPicture for inserting numeric values.

Method Index

 o arg(Double)
Plug the argument value into the next "slot" in the picture.
 o arg(double)
Plug the argument value into the next "slot" in the picture.
 o arg(Float)
Plug the argument value into the next "slot" in the picture.
 o arg(float)
Plug the argument value into the next "slot" in the picture.
 o arg(Formattable)
Plug the argument value into the next "slot" in the picture.
 o arg(int)
Plug the argument value into the next "slot" in the picture.
 o arg(Integer)
Plug the argument value into the next "slot" in the picture.
 o arg(long)
Substitute the argument value into the picture.
 o arg(Long)
Plug the argument value into the next "slot" in the picture.
 o arg(String)
Substitute the argument value into the picture.
 o arg(TextAlignment)
Substitute the argument value into the picture.
 o nfmt(int, int)
Set the width and precision for subsequent numeric arguments.
 o npic(String)
Change the numeric picture to the one specified by the given string.
 o pre(int)
Set the precision to be used for subsequent numeric arguments.
 o print(PrintStream)
Print the result of formatting to the specified PrintStream.
 o println(PrintStream)
Print the result of formatting, followed by a newline, to the specified PrintStream.
 o toString()
Return the result of formatting as a string.

Constructors

 o QuickFormat
 public QuickFormat(String pictureString)
Create a QuickFormat object using the given string for the TextPicture.

 o QuickFormat
 public QuickFormat(String textPic,
                    String numericPic)
Create a QuickFormat object using the first string for the TextPicture and the second string as the default NumericPicture for inserting numeric values.

Methods

 o arg
 public QuickFormat arg(Formattable value)
Plug the argument value into the next "slot" in the picture.

 o arg
 public QuickFormat arg(int value)
Plug the argument value into the next "slot" in the picture.

 o arg
 public QuickFormat arg(Integer value)
Plug the argument value into the next "slot" in the picture.

 o arg
 public QuickFormat arg(long value)
Substitute the argument value into the picture.

 o arg
 public QuickFormat arg(Long value)
Plug the argument value into the next "slot" in the picture.

 o arg
 public QuickFormat arg(float value)
Plug the argument value into the next "slot" in the picture.

 o arg
 public QuickFormat arg(Float value)
Plug the argument value into the next "slot" in the picture.

 o arg
 public QuickFormat arg(double value)
Plug the argument value into the next "slot" in the picture.

 o arg
 public QuickFormat arg(Double value)
Plug the argument value into the next "slot" in the picture.

 o arg
 public QuickFormat arg(String value)
Substitute the argument value into the picture.

 o arg
 public QuickFormat arg(TextAlignment textalign)
Substitute the argument value into the picture.

 o npic
 public QuickFormat npic(String numericPic)
Change the numeric picture to the one specified by the given string. Note that this new numeric picture will only be in force until the end of the current statement. Subsequent statements will return to using the original default numeric picture.

See Also:
NumericPicture
 o nfmt
 public QuickFormat nfmt(int width,
                         int precision)
Set the width and precision for subsequent numeric arguments.

 o pre
 public QuickFormat pre(int precision)
Set the precision to be used for subsequent numeric arguments.

 o print
 public void print(PrintStream ps)
Print the result of formatting to the specified PrintStream.

 o println
 public void println(PrintStream ps)
Print the result of formatting, followed by a newline, to the specified PrintStream.

 o toString
 public String toString()
Return the result of formatting as a string.

Overrides:
toString in class Object

All Packages  Class Hierarchy  This Package  Previous  Next  Index