Android
java.text
public class

java.text.MessageFormat

java.lang.Object
java.text.Format Serializable Cloneable
java.text.MessageFormat

MessageFormat is used to format and parse arguments based on a pattern. The pattern specifies how each argument will be formatted and concatenated with other text to produce the formatted output.

Nested Classes
MessageFormat.Field The instances of this inner class are used as attribute keys in AttributedCharacterIterator that MessageFormat.formatToCharacterIterator() method returns. 

Summary

Public Constructors

            MessageFormat(String template, Locale locale)
Constructs a new MessageFormat using the specified pattern and the specified Locale for Formats.
            MessageFormat(String template)
Constructs a new MessageFormat using the specified pattern and the default Locale for Formats.

Public Methods

          void  applyPattern(String template)
Changes this MessageFormat to use the specified pattern.
          Object  clone()
Returns a new instance of MessageFormat with the same pattern and Formats as this MessageFormat.
          boolean  equals(Object object)
Compares the specified object to this MessageFormat and answer if they are equal.
      static    String  format(String template, Object[] objects)
Formats the Object arguments using the specified MessageFormat pattern.
    final      StringBuffer  format(Object[] objects, StringBuffer buffer, FieldPosition field)
Formats the Object arguments into the specified StringBuffer using the pattern of this MessageFormat.
    final      StringBuffer  format(Object object, StringBuffer buffer, FieldPosition field)
Formats the specified object into the specified StringBuffer using the pattern of this MessageFormat.
          AttributedCharacterIterator  formatToCharacterIterator(Object object)
Formats the specified object using the rules of this MessageFormat and returns an AttributedCharacterIterator with the formatted message and attributes.
          Format[]  getFormats()
Returns the Formats of this MessageFormat.
          Format[]  getFormatsByArgumentIndex()
Returns the formats used for each argument index.
          Locale  getLocale()
Returns the Locale used when creating Formats.
          int  hashCode()
Returns an integer hash code for the receiver.
          Object[]  parse(String string, ParsePosition position)
Parse the message argument from the specified String starting at the index specified by the ParsePosition.
          Object[]  parse(String string)
Parse the message arguments from the specified String using the rules of this MessageFormat.
          Object  parseObject(String string, ParsePosition position)
Parse the message argument from the specified String starting at the index specified by the ParsePosition.
          void  setFormat(int offset, Format format)
Sets the specified Format used by this MessageFormat.
          void  setFormatByArgumentIndex(int argIndex, Format format)
Sets the format used for argument at index argIndexto format
          void  setFormats(Format[] formats)
Sets the Formats used by this MessageFormat.
          void  setFormatsByArgumentIndex(Format[] formats)
Sets the formats used for each argument The formats array elements should be in the order of the argument indices.
          void  setLocale(Locale locale)
Sets the Locale to use when creating Formats.
          String  toPattern()
Returns the pattern of this MessageFormat.
Methods inherited from class java.text.Format
Methods inherited from class java.lang.Object

Details

Public Constructors

public MessageFormat(String template, Locale locale)

Constructs a new MessageFormat using the specified pattern and the specified Locale for Formats.

Parameters

template the pattern
locale the locale

Throws

IllegalArgumentException when the pattern cannot be parsed

public MessageFormat(String template)

Constructs a new MessageFormat using the specified pattern and the default Locale for Formats.

Parameters

template the pattern

Throws

IllegalArgumentException when the pattern cannot be parsed

Public Methods

public void applyPattern(String template)

Changes this MessageFormat to use the specified pattern.

Parameters

template the pattern

Throws

IllegalArgumentException when the pattern cannot be parsed

public Object clone()

Returns a new instance of MessageFormat with the same pattern and Formats as this MessageFormat.

Returns

  • a shallow copy of this MessageFormat

See Also

public boolean equals(Object object)

Compares the specified object to this MessageFormat and answer if they are equal. The object must be an instance of MessageFormat and have the same pattern.

Parameters

object the object to compare with this object

Returns

  • true if the specified object is equal to this MessageFormat, false otherwise

See Also

public static String format(String template, Object[] objects)

Formats the Object arguments using the specified MessageFormat pattern.

Parameters

template the pattern
objects the array of Objects to format

Returns

  • the formatted result

Throws

IllegalArgumentException when the pattern cannot be parsed

public final StringBuffer format(Object[] objects, StringBuffer buffer, FieldPosition field)

Formats the Object arguments into the specified StringBuffer using the pattern of this MessageFormat.

If Field Attribute of the FieldPosition supplied is MessageFormat.Field.ARGUMENT, then begin and end index of this field position is set to the location of the first occurrence of a message format argument. Otherwise the FieldPosition is ignored

Parameters

objects the array of Objects to format
buffer the StringBuffer
field a FieldPosition.

Returns

  • the StringBuffer parameter buffer

public final StringBuffer format(Object object, StringBuffer buffer, FieldPosition field)

Formats the specified object into the specified StringBuffer using the pattern of this MessageFormat.

Parameters

object the object to format, must be an array of Object
buffer the StringBuffer
field a FieldPosition which is ignored

Returns

  • the StringBuffer parameter buffer

Throws

ClassCastException when object is not an array of Object

public AttributedCharacterIterator formatToCharacterIterator(Object object)

Formats the specified object using the rules of this MessageFormat and returns an AttributedCharacterIterator with the formatted message and attributes. The AttributedCharacterIterator returned also includes the attributes from the formats of this MessageFormat.

Parameters

object the object to format

Returns

  • an AttributedCharacterIterator with the formatted message and attributes

Throws

IllegalArgumentException when the arguments in the object array cannot be formatted by this Format

public Format[] getFormats()

Returns the Formats of this MessageFormat.

Returns

  • an array of Format

public Format[] getFormatsByArgumentIndex()

Returns the formats used for each argument index. If an argument is placed more than once in the pattern string, than returns the format of the last one.

Returns

  • an array of formats, ordered by argument index

public Locale getLocale()

Returns the Locale used when creating Formats.

Returns

  • the Locale used to create Formats

public int hashCode()

Returns an integer hash code for the receiver. Objects which are equal answer the same value for this method.

Returns

  • the receiver's hash

See Also

public Object[] parse(String string, ParsePosition position)

Parse the message argument from the specified String starting at the index specified by the ParsePosition. If the string is successfully parsed, the index of the ParsePosition is updated to the index following the parsed text.

Parameters

string the String to parse
position the ParsePosition, updated on return with the index following the parsed text, or on error the index is unchanged and the error index is set to the index where the error occurred

Returns

  • the array of Object arguments resulting from the parse, or null if there is an error

public Object[] parse(String string)

Parse the message arguments from the specified String using the rules of this MessageFormat.

Parameters

string the String to parse

Returns

  • the array of Object arguments resulting from the parse

Throws

ParseException when an error occurs during parsing

public Object parseObject(String string, ParsePosition position)

Parse the message argument from the specified String starting at the index specified by the ParsePosition. If the string is successfully parsed, the index of the ParsePosition is updated to the index following the parsed text.

Parameters

string the String to parse
position the ParsePosition, updated on return with the index following the parsed text, or on error the index is unchanged and the error index is set to the index where the error occurred

Returns

  • the array of Object arguments resulting from the parse, or null if there is an error

public void setFormat(int offset, Format format)

Sets the specified Format used by this MessageFormat.

Parameters

offset the format to change
format the Format

public void setFormatByArgumentIndex(int argIndex, Format format)

Sets the format used for argument at index argIndexto format

public void setFormats(Format[] formats)

Sets the Formats used by this MessageFormat.

Parameters

formats an array of Format

public void setFormatsByArgumentIndex(Format[] formats)

Sets the formats used for each argument The formats array elements should be in the order of the argument indices.

public void setLocale(Locale locale)

Sets the Locale to use when creating Formats.

Parameters

locale the Locale

public String toPattern()

Returns the pattern of this MessageFormat.

Returns

  • the pattern
Copyright 2007 Google Inc. Build 0.9_r1-98467 - 14 Aug 2008 18:56