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
-
QuickFormat(String)
- Create a QuickFormat object using the given string
for the TextPicture.
-
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.
-
arg(Double)
- Plug the argument value into the next "slot" in the picture.
-
arg(double)
- Plug the argument value into the next "slot" in the picture.
-
arg(Float)
- Plug the argument value into the next "slot" in the picture.
-
arg(float)
- Plug the argument value into the next "slot" in the picture.
-
arg(Formattable)
- Plug the argument value into the next "slot" in the picture.
-
arg(int)
- Plug the argument value into the next "slot" in the picture.
-
arg(Integer)
- Plug the argument value into the next "slot" in the picture.
-
arg(long)
- Substitute the argument value into the picture.
-
arg(Long)
- Plug the argument value into the next "slot" in the picture.
-
arg(String)
- Substitute the argument value into the picture.
-
arg(TextAlignment)
- Substitute the argument value into the picture.
-
nfmt(int, int)
- Set the width and precision for subsequent numeric arguments.
-
npic(String)
- Change the numeric picture to the one specified by the given string.
-
pre(int)
- Set the precision to be used for subsequent numeric arguments.
-
print(PrintStream)
- Print the result of formatting to the specified PrintStream.
-
println(PrintStream)
- Print the result of formatting, followed by a newline,
to the specified PrintStream.
-
toString()
- Return the result of formatting as a string.
QuickFormat
public QuickFormat(String pictureString)
- Create a QuickFormat object using the given string
for the TextPicture.
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.
arg
public QuickFormat arg(Formattable value)
- Plug the argument value into the next "slot" in the picture.
arg
public QuickFormat arg(int value)
- Plug the argument value into the next "slot" in the picture.
arg
public QuickFormat arg(Integer value)
- Plug the argument value into the next "slot" in the picture.
arg
public QuickFormat arg(long value)
- Substitute the argument value into the picture.
arg
public QuickFormat arg(Long value)
- Plug the argument value into the next "slot" in the picture.
arg
public QuickFormat arg(float value)
- Plug the argument value into the next "slot" in the picture.
arg
public QuickFormat arg(Float value)
- Plug the argument value into the next "slot" in the picture.
arg
public QuickFormat arg(double value)
- Plug the argument value into the next "slot" in the picture.
arg
public QuickFormat arg(Double value)
- Plug the argument value into the next "slot" in the picture.
arg
public QuickFormat arg(String value)
- Substitute the argument value into the picture.
arg
public QuickFormat arg(TextAlignment textalign)
- Substitute the argument value into the picture.
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
nfmt
public QuickFormat nfmt(int width,
int precision)
- Set the width and precision for subsequent numeric arguments.
pre
public QuickFormat pre(int precision)
- Set the precision to be used for subsequent numeric arguments.
print
public void print(PrintStream ps)
- Print the result of formatting to the specified PrintStream.
println
public void println(PrintStream ps)
- Print the result of formatting, followed by a newline,
to the specified PrintStream.
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