org.jruby.util
Class PrintfFormat

java.lang.Object
  extended byorg.jruby.util.PrintfFormat

public class PrintfFormat
extends java.lang.Object

PrintfFormat is a Java implementation of the sprintf utility. To use it, you pass the string containing the format specifier(s) to the constructor, then call the appropriate form of the sprintf method. To pass more than one argument, convert any primitives to their wrapper types and place all the arguments in an array of type Object.

PrintfFormat supports all the standard flags ([-+ #0]), as well as the ' (apostrophe) flag, which causes decimal integers to be grouped in a locale-dependent way (e.g, in English locales, groups of three digits separated by commas). The integer modifiers ([hlL]) are also accepted, but the "L" modifier is ignored as superfluous. Finally, all the standard conversion-type characters ([idfgGoxXeEcs]) are supported, except for "p" and "n".

In addition, PrintfFormat supports positional syntax for arguments, field widths, and precisions. A format specifier that starts with a "%n$" sequence will be applied to the nth argument. A field width or precision of the form "*m$" will take its value from the mth argument. The standard variable specifier for field width and precision (*), which takes its value from the next argument, is also supported. However, if a variable field width or precision is encountered in a format spec that starts with a positional argument spec, the "*" will be ignored and the default value will be used instead.

Version:
$Revision: 1.4 $ Release 1: Initial release. Release 2: Asterisk field widths and precisions %n$ and *m$ Bug fixes g format fix (2 digits in e form corrupt) rounding in f format implemented round up when digit not printed is 5 formatting of -0.0f round up/down when last digits are 50000...
Author:
Allan Jacobs

Constructor Summary
PrintfFormat(java.util.Locale locale, java.lang.String fmtArg)
          Constructs an array of control specifications possibly preceded, separated, or followed by ordinary strings.
PrintfFormat(java.lang.String fmtArg)
          Constructs an array of control specifications possibly preceded, separated, or followed by ordinary strings.
 
Method Summary
 java.lang.String sprintf()
          Format nothing.
 java.lang.String sprintf(double x)
          Format a double.
 java.lang.String sprintf(int x)
          Format an int.
 java.lang.String sprintf(long x)
          Format a long.
 java.lang.String sprintf(java.lang.Object x)
          Format an Object.
 java.lang.String sprintf(java.lang.Object[] o)
          Format an array of objects.
 java.lang.String sprintf(java.lang.String x)
          Format a String.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

PrintfFormat

public PrintfFormat(java.lang.String fmtArg)
             throws java.lang.IllegalArgumentException
Constructs an array of control specifications possibly preceded, separated, or followed by ordinary strings.

Parameters:
fmtArg - Control string.
Throws:
java.lang.IllegalArgumentException - if the control string is null, zero length, or otherwise malformed.

PrintfFormat

public PrintfFormat(java.util.Locale locale,
                    java.lang.String fmtArg)
             throws java.lang.IllegalArgumentException
Constructs an array of control specifications possibly preceded, separated, or followed by ordinary strings.

Parameters:
fmtArg - Control string.
Throws:
java.lang.IllegalArgumentException - if the control string is null, zero length, or otherwise malformed.
Method Detail

sprintf

public java.lang.String sprintf(java.lang.Object[] o)
Format an array of objects. Byte, Short, Integer, Long, Float, Double, and Character arguments are treated as wrappers for primitive types.

Parameters:
o - The array of objects to format.
Returns:
The formatted String.

sprintf

public java.lang.String sprintf()
Format nothing. Just use the control string.

Returns:
the formatted String.

sprintf

public java.lang.String sprintf(int x)
                         throws java.lang.IllegalArgumentException
Format an int.

Parameters:
x - The int to format.
Returns:
The formatted String.
Throws:
java.lang.IllegalArgumentException - if the conversion character is f, e, E, g, G, s, or S.

sprintf

public java.lang.String sprintf(long x)
                         throws java.lang.IllegalArgumentException
Format a long.

Parameters:
x - The long to format.
Returns:
The formatted String.
Throws:
java.lang.IllegalArgumentException - if the conversion character is f, e, E, g, G, s, or S.

sprintf

public java.lang.String sprintf(double x)
                         throws java.lang.IllegalArgumentException
Format a double.

Parameters:
x - The double to format.
Returns:
The formatted String.
Throws:
java.lang.IllegalArgumentException - if the conversion character is c, C, s, S, d, d, x, X, or o.

sprintf

public java.lang.String sprintf(java.lang.String x)
                         throws java.lang.IllegalArgumentException
Format a String.

Parameters:
x - The String to format.
Returns:
The formatted String.
Throws:
java.lang.IllegalArgumentException - if the conversion character is neither s nor S.

sprintf

public java.lang.String sprintf(java.lang.Object x)
                         throws java.lang.IllegalArgumentException
Format an Object. Convert wrapper types to their primitive equivalents and call the appropriate internal formatting method. Convert Strings using an internal formatting method for Strings. Otherwise use the default formatter (use toString).

Parameters:
x - the Object to format.
Returns:
the formatted String.
Throws:
java.lang.IllegalArgumentException - if the conversion character is inappropriate for formatting an unwrapped value.


Copyright © 2002 Jan Arne Petersen. All Rights Reserved.