All Packages  Class Hierarchy  This Package  Previous  Next  Index

Class java.text.Format

java.lang.Object
   |
   +----java.text.Format

public class Format
extends Object
implements Serializable, Cloneable
Base class for all formats.

If formatting is unsuccessful, ClassCastException is returned when the Format cannot format the type of object, otherwise if there is something illformed about the the Unicode replacement character \\uFFFD is returned.

If there is no match when parsing, FormatException is thrown for the method that takes no ParseStatus. For the method that takes a ParseStatus, the startAt parameter is left unchanged.

Subclassing. All base classes that provide static functions that create objects for Locales must implement three statics:

 public static Locale[] getAvailableLocales()
 public static String getDisplayName(Locale objectLocale,
                                     Locale displayLocale)
 public static final String getDisplayName(Locale objectLocale)
 

See Also:
ParseStatus, FormatStatus, NumberFormat, DateFormat, MessageFormat

Constructor Index

 o Format()

Method Index

 o format(Object)
Formats an object to produce a string.
 o format(Object, StringBuffer, FormatStatus)
Formats an object to produce a string.
 o parseObject(String)
Parses a string to produce an object.
 o parseObject(String, ParseStatus)
Parses a string to produce an object.

Constructors

 o Format
  public Format()

Methods

 o format
  public final String format(Object obj)
Formats an object to produce a string.

Subclasses will override the StringBuffer version of format.

Parameters:
obj - The object to format
Returns:
Formatted string.
Throws: ClassCastException
when the Format cannot format the type of object.
See Also:
ParameterFormat, format
 o format
  public abstract StringBuffer format(Object obj,
                                      StringBuffer toAppendTo,
                                      FormatStatus status)
Formats an object to produce a string. Subclasses will implement for particular object, such as:
 StringBuffer format (Number obj, StringBuffer toAppendTo)
 Number parse (String str)
 
These general routines allow polymorphic parsing and formatting for objects such as the ParameterFormat.

Parameters:
obj - The object to format
toAppendTo - where the text is to be appended
status - On input: an alignment field, if desired. On output: the offsets of the alignment field.
Returns:
the value passed in as toAppendTo (this allows chaining, as with StringBuffer.append())
Throws: ClassCastException
when the Format cannot format the type of object.
See Also:
ParameterFormat, FormatStatus
 o parseObject
  public abstract Object parseObject(String source,
                                     ParseStatus status)
Parses a string to produce an object. Subclasses will typically implement for particular object, such as:
       String format (Number obj);
       String format (long obj);
       String format (double obj);
       Number parse (String str);
 

Parameters:
ParseStatus - Input-Output parameter.

Before calling, set status.startAt to the offset you want to start parsing at in the source. After calling, status.startAt is the end of the text you parsed. If error occurs, startAt is unchanged.

When parsing, leading whitespace is discarded (with successful parse), while trailing whitespace is left as is.

Example: Parsing "_12_xy" (where _ represents a space) for a number, with startAt == 0 will result in the number 12, with status.startAt updated to 3 (just before the second space). Parsing a second time will result in a FormatException since "xy" is not a number, and leave startAt at 3.

Subclasses will typically supply specific parse methods that return different types of values. Since methods can't overload on return types, these will typically be named "parse", while this polymorphic method will always be called parseObject. Any parse method that does not take a status should throw FormatException when no text in the required format is at the start position.

Returns:
Object parsed from string. In case of error, returns null.
See Also:
ParseStatus
 o parseObject
  public Object parseObject(String source) throws FormatException
Parses a string to produce an object.


All Packages  Class Hierarchy  This Package  Previous  Next  Index