Class MessageFormat
java.lang.Object
java.text.Format
java.text.MessageFormat
- All Implemented Interfaces:
Serializable, Cloneable
Builds a message from a pattern and a list of arguments, and reads the arguments back out of a message.
Pattern syntax
Text is copied as it is, and each {...} element is replaced by an
argument:
{0}: the argument at that index. ANumberis formatted withNumberFormat.getInstance, aDatewith the short date and time format, aStringis copied, anything else contributestoString().{0,number},{0,number,integer},{0,number,percent},{0,number,currency}, or{0,number,#,##0.00}with aDecimalFormatpattern.{0,date}and{0,time}, optionally followed byshort,medium,longorfull, or by aSimpleDateFormatpattern:{0,date,yyyy-MM-dd}. The device's ownjava.text.DateFormatdoes the formatting, so the named styles produce what the device produces.{0,choice,0#none|1#one|1<{0} items}with aChoiceFormatpattern. If the chosen text has elements of its own, they are formatted against the same arguments.
A single quote makes the characters up to the next one literal, which is
how a { is written ('{'), and two quotes in a row stand for one. An
apostrophe in a message must therefore be doubled: it''s.
An argument that is null is written as null, and an element whose
index is past the end of the arguments is written back as it was, {2}.
Differences from the JDK
toPatternreturns the elements as they were written rather than a canonical rewrite of them.- Date and time elements ignore the locale passed to the constructor: the device formats dates for its own locale only.
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidapplyPattern(String pattern) clone()A copy of this format, built by applying its pattern again:Object.clone()answersnullon the device for anything but an array.booleanIndicates whether some other object is "equal to" this one.final StringBufferformat(Object[] arguments, StringBuffer result, FieldPosition pos) final StringBufferformat(Object arguments, StringBuffer result, FieldPosition pos) Formats an array of arguments, whichargumentsmust be.static StringFormat[]The formats of the elements, in the order the elements appear in the pattern;nullfor an element that has none.Format[]The formats of the arguments, indexed by argument number;nullfor an argument no element formats.inthashCode()Returns a hash code value for the object.Object[]Reads the arguments back out of the beginning ofsource.Object[]parse(String source, ParsePosition pos) Reads the arguments back out ofsource, starting atpos: the literal text must match exactly, an element with a format is read by that format, and an element without one takes the text up to the literal that follows it.parseObject(String source, ParsePosition pos) Parses an object fromsourcestarting atpos.voidSets the format of the element atformatElementIndex, counted in the order the elements appear in the pattern.voidsetFormatByArgumentIndex(int argumentIndex, Format newFormat) Sets the format of every element that formats argumentargumentIndex.voidsetFormats(Format[] newFormats) Sets the formats of the elements in pattern order; extra formats are ignored and elements past the end ofnewFormatsare left alone.voidsetFormatsByArgumentIndex(Format[] newFormats) Sets the formats of the arguments, indexed by argument number.voidMethods inherited from class Format
format, parseObject
-
Constructor Details
-
MessageFormat
-
MessageFormat
-
-
Method Details
-
setLocale
-
getLocale
-
applyPattern
-
toPattern
-
format
-
format
Formats an array of arguments, which
argumentsmust be.Throws
IllegalArgumentException: ifargumentsis not anObject[], or an argument is not of the type its element formats
-
format
-
getFormats
The formats of the elements, in the order the elements appear in the pattern;nullfor an element that has none. -
getFormatsByArgumentIndex
The formats of the arguments, indexed by argument number;nullfor an argument no element formats. Where several elements name the same argument, the last one's format is the one returned. -
setFormat
Sets the format of the element at
formatElementIndex, counted in the order the elements appear in the pattern.Throws
ArrayIndexOutOfBoundsException: if there is no such element
-
setFormats
Sets the formats of the elements in pattern order; extra formats are ignored and elements past the end ofnewFormatsare left alone. -
setFormatByArgumentIndex
Sets the format of every element that formats argumentargumentIndex. -
setFormatsByArgumentIndex
Sets the formats of the arguments, indexed by argument number. -
parse
Reads the arguments back out ofsource, starting atpos: the literal text must match exactly, an element with a format is read by that format, and an element without one takes the text up to the literal that follows it. The result is indexed by argument number. On failureposis left alone, its error index is set, andnullis returned. -
parse
Reads the arguments back out of the beginning of
source.Throws
ParseException: ifsourcedoes not match the pattern
- Throws:
ParseException
-
parseObject
Description copied from class:FormatParses an object fromsourcestarting atpos. On success the index ofposis moved past what was read; on failure it is left alone, the error index is set andnullis returned.- Specified by:
parseObjectin classFormat
-
clone
A copy of this format, built by applying its pattern again:Object.clone()answersnullon the device for anything but an array. A format installed withsetFormatthat the pattern cannot spell is carried over as the same object, since it cannot be copied the same way. -
equals
Description copied from class:ObjectIndicates whether some other object is "equal to" this one. The equals method implements an equivalence relation: It is reflexive: for any reference value x, x.equals(x) should return true. It is symmetric: for any reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true. It is transitive: for any reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true. It is consistent: for any reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the object is modified. For any non-null reference value x, x.equals(null) should return false. The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any reference values x and y, this method returns true if and only if x and y refer to the same object (x==y has the value true). -
hashCode
public int hashCode()Description copied from class:ObjectReturns a hash code value for the object. This method is supported for the benefit of hashtables such as those provided by java.util.Hashtable. The general contract of hashCode is: Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application. If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result. It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hashtables. As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language.)
-