cds.util
Class CFormat

java.lang.Object
  extended bycds.util.CFormat

public abstract class CFormat
extends java.lang.Object

Provides methods for formatted printing a-la C printf. This class is the the top-level class of the printf package. All the fields and methods in this class are static. The static fields control the behavior of the methods, and the methods do printing to System.out (printf()), an arbitrary PrintStream (fprintf()), or a String (sprintf()).

Version:
V2.0 Jan 2004 adapted to CDS requirements
- correct the width and 'zero' management (%8d, %08d was incorrect).
- suppress the args.clear() at the end of sprintf(). use instead args.clear().add(...
- modify the decimal symbols. Set DecimalFormat for nf Copyright 1999,2000 Tony Plate
Author:
Tony Plate tplate@attglobal.net, Marc Wenger/CDS

Field Summary
static boolean debug
          Print debugging diagnostics.
static char DECIMAL_POINT
           
(package private) static java.util.Hashtable formatNodesHashTable
          Keep a hash table of recently-encountered format strings.
(package private) static int gThreshold
          The exponent threshold below which %g should print as %f rather than %e.
(package private) static int maxRememberedFormatStrings
          Size of table of cached parsed format strings.
static char MINUS_SIGN
           
static boolean nonStringPercentSIsError
          Is supplying a non-string object for %s an error?
(package private) static DoublyLinkedList recentFormatStrings
          A list of recently-encountered format strings, with the most-recently-used at the head of the list.
static boolean trapErrors
          If trapErrors is true, sprintf catches exceptions internally, prints an error message, prints or returns partial results, and returns to the caller.
static boolean useInternationalizationNaNandInf
          Use the NaN and Infinity symbol strings from java.text.decimalFormatSymbols.
static boolean useNonLocaleChars
          Use non Locale decimal point, zero digit and minus sign, but the common signs used in science
static char ZERO_DIGIT
           
 
Constructor Summary
CFormat()
           
 
Method Summary
static void fprintf(java.io.PrintStream out, java.lang.String format)
          Equivalent to printf() with a format string and zero print arguments.
static void fprintf(java.io.PrintStream out, java.lang.String format, VarArgs args)
          Format the arguments and print the result on the PrintStream out.
static void main(java.lang.String[] args)
           
static void printf(java.lang.String format)
          Equivalent to printf() with a format string and zero print arguments.
static void printf(java.lang.String format, VarArgs args)
          Format the arguments and print the result on System.out.
static java.lang.String sprintf(java.lang.String format)
          Equivalent to sprintf() with a format string and zero print arguments.
static java.lang.String sprintf(java.lang.String format, VarArgs args)
          Format the arguments according to the format string, and return the result as a String.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

trapErrors

public static boolean trapErrors
If trapErrors is true, sprintf catches exceptions internally, prints an error message, prints or returns partial results, and returns to the caller. Otherwise, sprintf throws a CFormatArgException or CFormatSyntaxException without printing or returning partial results (these are subclasses of RuntimeException)


nonStringPercentSIsError

public static boolean nonStringPercentSIsError
Is supplying a non-string object for %s an error? Default: false. If it is not an error, the toString() of the object is used to get a string.


debug

public static boolean debug
Print debugging diagnostics. Default: false


gThreshold

static int gThreshold
The exponent threshold below which %g should print as %f rather than %e. Default: -4 as in the ANSI standard.


maxRememberedFormatStrings

static int maxRememberedFormatStrings
Size of table of cached parsed format strings. Default: 512.


useInternationalizationNaNandInf

public static boolean useInternationalizationNaNandInf
Use the NaN and Infinity symbol strings from java.text.decimalFormatSymbols. These didn't work when I tried. Default: false.


useNonLocaleChars

public static boolean useNonLocaleChars
Use non Locale decimal point, zero digit and minus sign, but the common signs used in science


DECIMAL_POINT

public static char DECIMAL_POINT

ZERO_DIGIT

public static char ZERO_DIGIT

MINUS_SIGN

public static char MINUS_SIGN

formatNodesHashTable

static java.util.Hashtable formatNodesHashTable
Keep a hash table of recently-encountered format strings. Use two data structures for this: (1) A doubly linked list, to keep track of recency (items get moved to the start when used). (2) A hash table of list items, to look up items quickly (ones that fall off the end of the recency list get removed from the table). In the hash table, the format string is the key, and the list node is the value.


recentFormatStrings

static DoublyLinkedList recentFormatStrings
A list of recently-encountered format strings, with the most-recently-used at the head of the list.

Constructor Detail

CFormat

public CFormat()
Method Detail

main

public static void main(java.lang.String[] args)

sprintf

public static java.lang.String sprintf(java.lang.String format,
                                       VarArgs args)
Format the arguments according to the format string, and return the result as a String.

Parameters:
format - the C like formatting string
args - the parameters list
Returns:
the resulting formatted list

sprintf

public static java.lang.String sprintf(java.lang.String format)
Equivalent to sprintf() with a format string and zero print arguments. (It is an error if the format string contains any % codes, except for %%).

Parameters:
format - C like format string
Returns:
the formatted string (without parameter, it should be the same !)

printf

public static void printf(java.lang.String format,
                          VarArgs args)
Format the arguments and print the result on System.out.

Parameters:
format - the C like formatting string
args - the parameters list

printf

public static void printf(java.lang.String format)
Equivalent to printf() with a format string and zero print arguments. (It is an error if the format string contains any % codes, except for %%).

Parameters:
format - the C like formatting string

fprintf

public static void fprintf(java.io.PrintStream out,
                           java.lang.String format,
                           VarArgs args)
Format the arguments and print the result on the PrintStream out.

Parameters:
out - a PrintStreamoutput object
format - the C like formatting string
args - the parameters list

fprintf

public static void fprintf(java.io.PrintStream out,
                           java.lang.String format)
Equivalent to printf() with a format string and zero print arguments. (It is an error if the format string contains any % codes, except for %%).

Parameters:
out - a PrintStreamoutput object
format - the C like formatting string