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)"
- The syntax %N (where N is from '0' to '9') specifies that argument
number N is to be formatted with format number N.
- The syntax %A|F specifies that argument number A
is to be formatted with format number F.
- For plain %, use %%.
- Note that arguments are zero-based!
- It is ok to have unused arguments in the pattern.
With missing arguments, a FormatException is thrown.
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
-
MessageFormat(String)
- Sets the pattern.
-
MessageFormat(String, Format[])
- Sets the pattern and the formats for the arguments in that pattern.
-
clone()
- Overrides Cloneable
-
equals(Object)
- Equality comparision between two message format objects
-
format(Object, StringBuffer, FormatStatus)
- Formats an object to produce a string.
-
format(Object[], StringBuffer, FormatStatus)
- Returns pattern with formatted objects substituted for %0..%9
-
format(String, Format[], Object[])
- Convenience routine.
-
format(String, Object[])
- Convenience routine.
-
getFormats()
- Gets formats that were set with setFormats.
-
getPattern()
- Gets the pattern.
-
hashCode()
- Generates a hash code for the message format object.
-
parse(String)
- Parses the string.
-
parse(String, ParseStatus)
- Parses the string.
-
parseObject(String, ParseStatus)
- Parses the string.
-
setFormats(Format[])
- Sets formats to use on parameters.
-
setPattern(String)
- Sets the pattern.
MessageFormat
public MessageFormat(String pattern)
- Sets the pattern.
- See Also:
- setPattern
MessageFormat
public MessageFormat(String pattern,
Format formats[])
- Sets the pattern and the formats for the arguments in that pattern.
- See Also:
- setPattern, setFormats
setPattern
public void setPattern(String pattern)
- Sets the pattern. See the class description.
getPattern
public String getPattern()
- Gets the pattern. See the class description.
setFormats
public void setFormats(Format formats[])
- Sets formats to use on parameters. See the class description.
getFormats
public Format[] getFormats()
- Gets formats that were set with setFormats.
See the class description.
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.
format
public static String format(String pattern,
Object arguments[])
- Convenience routine.
Avoids explicit creation of MessageFormat,
but doesn't allow future optimizations.
format
public static String format(String pattern,
Format formats[],
Object arguments[])
- Convenience routine.
Avoids creation of MessageFormat,
but doesn't allow future optimizations.
format
public final StringBuffer format(Object source,
StringBuffer result,
FormatStatus ignore)
- Formats an object to produce a string.
- Overrides:
- format in class Format
parse
public Object[] parse(String source,
ParseStatus status)
- Parses the string. Does not yet handle recursion (where
the substituted strings contain %n references.)
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.
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
clone
public Object clone()
- Overrides Cloneable
- Overrides:
- clone in class Object
equals
public boolean equals(Object obj)
- Equality comparision between two message format objects
- Overrides:
- equals in class Object
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