sharkysoft.com

com.sharkysoft.printf
Class PrintfData

java.lang.Object
  extended bycom.sharkysoft.printf.PrintfData

public class PrintfData
extends java.lang.Object

Syntactic sugar for variable argument lists.

Details: PrintfData offers an alternative method for defining the data vector in Printf For Java. If you don't like the idea of wrapping primitive types in their object counterparts and referencing them in an Object array, then perhaps you'll like the syntactic sugar afforded by this helper class. Use of this class is entirely optional.

To build a printf data vector using this class, first instantiate an instance, and then chain together calls to its overloaded add method, furnishing the printf input data one element at a time. (Each add call returns the same instance of this class, to facilitate chaining.) After the last element has been added, the result can then be supplied directly to most Printf methods.

Example: The two Printf in this code are equivalent:

String format = "My dog %s is %d years old.";
String name = "Spot";
int age = 3;
// With Object array:
Printf.out(format, new Object[]{name, new Integer(age)});
// With PrintfData:
Printf.out(format, new PrintfData().add(name).add(age));

In the first Printf call above, an Object array is explicitely created, and the primitive type is explicitely wrapped. In the second call, no Object array is created, and no primitive type is wrapped. Printf accepts the data vector returned by the last add call.

Behind the scenes, Printf simply converts the PrintfData to an Object[]. Therefore, straight Object arrays are always more efficient. The choice is yours.

Since:
before 1998.02.19
Version:
1998.12.23
Author:
Sharky

Constructor Summary
PrintfData()
          Initializes empty vector with undetermined length.
PrintfData(int inLength)
          Initializes empty vector with final length.
 
Method Summary
 PrintfData add(boolean izB)
          Appends boolean data.
 PrintfData add(byte ibB)
          Appends byte data.
 PrintfData add(char icC)
          Appends char data.
 PrintfData add(double idD)
          Appends double data.
 PrintfData add(float ifF)
          Appends float data.
 PrintfData add(int inI)
          Appends int data.
 PrintfData add(long ilL)
          Appends long data.
 PrintfData add(java.lang.Object ipO)
          Appends Object data.
 PrintfData add(short iwS)
          Appends short data.
 java.lang.Object[] done()
          Converts to Object array.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

PrintfData

public PrintfData()
Initializes empty vector with undetermined length.

Details: This constructor initializes an empty data vector. Use this constructor when the final length of the data vector is unknown at compile time.


PrintfData

public PrintfData(int inLength)
Initializes empty vector with final length.

Details: This constructor initializes an empty data vector whose final length has already been determined. Use this constructor whenever possible to create more efficient code. If the predicted final length of this data vector ultimately differs from the value passed into this constructor, no harm is done.

Parameters:
inLength - the estimated length
Method Detail

add

public PrintfData add(java.lang.Object ipO)
Appends Object data.

Details: Use this method to append any type of object, including Strings, to this data vector. The argument will not be wrapped.

Parameters:
ipO - the object
Returns:
this instance

add

public PrintfData add(char icC)
Appends char data.

Details: Use this method to append a char to this data vector. The argument will be wrapped in a Character.

Parameters:
icC - the char
Returns:
this instance

add

public PrintfData add(short iwS)
Appends short data.

Details: Use this method to append a short to this data vector. The argument will be wrapped in a Short.

Parameters:
iwS - the short
Returns:
this instance

add

public PrintfData add(int inI)
Appends int data.

Details: Use this method to append an int to this data vector. The argument will be wrapped in a Integer.

Parameters:
inI - the int
Returns:
this instance

add

public PrintfData add(long ilL)
Appends long data.

Details: Use this method to append a long to this data vector. The argument will be wrapped in a Long.

Parameters:
ilL - the long
Returns:
this instance

add

public PrintfData add(float ifF)
Appends float data.

Details: Use this method to append a float to this data vector. The argument will be wrapped in a Float.

Parameters:
ifF - the float
Returns:
this instance

add

public PrintfData add(double idD)
Appends double data.

Details: Use this method to append a double to this data vector. The argument will be wrapped in a Double.

Parameters:
idD - the double
Returns:
this instance

add

public PrintfData add(byte ibB)
Appends byte data.

Details: Use this method to append a byte to this data vector. The argument will be wrapped in a Byte.

Parameters:
ibB - the byte
Returns:
this instance

add

public PrintfData add(boolean izB)
Appends boolean data.

Details: Use this method to append a boolean to this data vector. The argument will be wrapped in a Boolean.

Parameters:
izB - the boolean
Returns:
this instance

done

public java.lang.Object[] done()
Converts to Object array.

Details: Although it is not necessary in most cases, this method can be used to convert this data vector into an Object array.

Returns:
the Object array

sharkysoft.com

Copyright © 1997-2004 Sharkysoft (sharkysoft.com). All rights reserved.