info.informatica.text
Class ObjectFormat

java.lang.Object
  extended by info.informatica.text.ObjectFormat
All Implemented Interfaces:
Alignable
Direct Known Subclasses:
NumberFormat, StringFormat, TemplateDateFormat, TextFormat

public abstract class ObjectFormat
extends Object
implements Alignable

Base class for text formatting objects emulating popular C-Language printf function.

Synopsis:
ObjectFormat of; String formatted; of = ObjectFormat.create("6d"); formatted = of.format(new Integer(3527)); [...] of = ObjectFormat.create("-15s"); formatted = of.format("This is a test"); [...] of = ObjectFormat.create("-5T"); formatted = of.format("This is another test");

Current supported formats are:

The T text format is an extension that right- or left- justifies a given text. The D format uses a SimpleDateFormat format for a Date object. The usual flags and modifiers can be used, though full compatibility with typical C behavior is not guaranteed.

Subclasses must implement the setFormat method, and can implement its own setFlag method. Only in rare cases will need to override setFlags.

Version:
1.03
Author:
amengual at informatica dot info
See Also:
NumberFormat, OctalFormat, HexFormat, IntegerFormat, FloatFormat, ExponentFormat, StringFormat, TextFormat

Field Summary
 
Fields inherited from interface info.informatica.doc.style.Alignable
CENTER, JUSTIFY, LEFT, RIGHT, UNDETERMINED
 
Method Summary
 void align(int i)
          Sets the alignment of the object.
static ObjectFormat create(Class clase, int width, int precision)
          Creates the proper ObjectFormat object according to parameters, with a different default align for text and anything else.
static ObjectFormat create(Class clase, int width, int precision, boolean right_align)
          Creates the proper ObjectFormat object according to parameters.
static ObjectFormat create(String format)
          Creates the proper ObjectFormat object according to format.
abstract  String format(Object o)
          Formats an object according to the format specification set by setFormat.
 int getAlignment()
          Gets the alignment.
abstract  Object sample()
          Produce a sample of this format.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

create

public static ObjectFormat create(String format)
                           throws ParsingException
Creates the proper ObjectFormat object according to format.

Parameters:
format - the format
Returns:
the ObjectFormat object for format
Throws:
ParsingException

create

public static ObjectFormat create(Class clase,
                                  int width,
                                  int precision)
                           throws ParsingException
Creates the proper ObjectFormat object according to parameters, with a different default align for text and anything else.

Parameters:
clase - the class of the object to be formatted.
Returns:
the ObjectFormat object
Throws:
ParsingException

create

public static ObjectFormat create(Class clase,
                                  int width,
                                  int precision,
                                  boolean right_align)
                           throws ParsingException
Creates the proper ObjectFormat object according to parameters.

Parameters:
clase - the class of the object to be formatted.
Returns:
the ObjectFormat object
Throws:
ParsingException

align

public void align(int i)
Description copied from interface: Alignable
Sets the alignment of the object.

Specified by:
align in interface Alignable
Parameters:
i - one of the public constants specifying the alignment.

getAlignment

public int getAlignment()
Description copied from interface: Alignable
Gets the alignment.

Specified by:
getAlignment in interface Alignable
Returns:
the alignment.

format

public abstract String format(Object o)
Formats an object according to the format specification set by setFormat.

Must be overriden by subclasses.


sample

public abstract Object sample()
Produce a sample of this format.

Returns:
a String with a (more or less) random object appropriate to be formatted according to current format specifications.