All Packages  Class Hierarchy  This Package  Previous  Next  Index

Class java.text.MessageFormat

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

public class MessageFormat
extends Format
Provides means to produce concatenated messages in language-neutral way. Use this for all concatenations that show up to end users.

Takes a set of objects, formats them, then inserts the formatted strings into the pattern at the appropriate places.

For each argument, the following strategy is used to format it, depending on the type of the Format and Object. If the Format can format the object, then that formatted string is returned. Otherwise (or if the format is null or missing from the formats array), if the object is a Number, then NumberFormat.getDefault() is used; if the object is a Date, then DateFormat.getDateTimeFormat() is used. Otherwise the toString method on the object is used.

The pattern is of form:

"The disk %1 contains %2 file(s)"
For more sophisticated patterns, you can use a ChoiceFormat to get output such as:
 "The disk Foo contains 3 files."
 "The disk Foo contains 1 file."
 "The disk Foo contains no files."
 
Example:
 MessageFormat form = new MessageFormat("There are %0 files on %1");
 Object[] testArgs = {new Long(3), "MyDisk"};
 System.out.println(form.getPattern() + "; " + form.format(testArgs));
 

See Also:
Locale, Format, NumberFormat, DecimalFormat, ChoiceFormat

Constructor Index

 o MessageFormat(String)
Sets the pattern.
 o MessageFormat(String, Format[])
Sets the pattern and the formats for the arguments in that pattern.

Method Index

 o clone()
Overrides Cloneable
 o equals(Object)
Equality comparision between two message format objects
 o format(Object, StringBuffer, FormatStatus)
Formats an object to produce a string.
 o format(Object[], StringBuffer, FormatStatus)
Returns pattern with formatted objects substituted for %0..%9
 o format(String, Format[], Object[])
Convenience routine.
 o format(String, Object[])
Convenience routine.
 o getFormats()
Gets formats that were set with setFormats.
 o getPattern()
Gets the pattern.
 o hashCode()
Generates a hash code for the message format object.
 o parse(String)
Parses the string.
 o parse(String, ParseStatus)
Parses the string.
 o parseObject(String, ParseStatus)
Parses the string.
 o setFormats(Format[])
Sets formats to use on parameters.
 o setPattern(String)
Sets the pattern.

Constructors

 o MessageFormat
  public MessageFormat(String pattern)
Sets the pattern.

See Also:
setPattern
 o MessageFormat
  public MessageFormat(String pattern,
                       Format formats[])
Sets the pattern and the formats for the arguments in that pattern.

See Also:
setPattern, setFormats

Methods

 o setPattern
  public void setPattern(String pattern)
Sets the pattern. See the class description.

 o getPattern
  public String getPattern()
Gets the pattern. See the class description.

 o setFormats
  public void setFormats(Format formats[])
Sets formats to use on parameters. See the class description.

 o getFormats
  public Format[] getFormats()
Gets formats that were set with setFormats. See the class description.

 o format
  public final StringBuffer format(Object source[],
                                   StringBuffer result,
                                   FormatStatus ignore)
Returns pattern with formatted objects substituted for %0..%9

Parameters:
source - an array of objects to be formatted & substituted.
result - where text is appended.
ignore - no useful status is returned.
 o format
  public static String format(String pattern,
                              Object arguments[])
Convenience routine. Avoids explicit creation of MessageFormat, but doesn't allow future optimizations.

 o format
  public static String format(String pattern,
                              Format formats[],
                              Object arguments[])
Convenience routine. Avoids creation of MessageFormat, but doesn't allow future optimizations.

 o format
  public final StringBuffer format(Object source,
                                   StringBuffer result,
                                   FormatStatus ignore)
Formats an object to produce a string.

Overrides:
format in class Format
 o parse
  public Object[] parse(String source,
                        ParseStatus status)
Parses the string. Does not yet handle recursion (where the substituted strings contain %n references.)

 o parse
  public Object parse(String source) throws FormatException
Parses the string. Does not yet handle recursion (where the substituted strings contain %n references.)

Throws: FormatException
if the string can't be parsed.
 o parseObject
  public Object parseObject(String text,
                            ParseStatus status)
Parses the string. Does not yet handle recursion (where the substituted strings contain %n references.)

Overrides:
parseObject in class Format
 o clone
  public Object clone()
Overrides Cloneable

Overrides:
clone in class Object
 o equals
  public boolean equals(Object obj)
Equality comparision between two message format objects

Overrides:
equals in class Object
 o hashCode
  public int hashCode()
Generates a hash code for the message format object.

Overrides:
hashCode in class Object

All Packages  Class Hierarchy  This Package  Previous  Next  Index