Build 1.0_r1(from source)

java.text
Class MessageFormat

java.lang.Object
  extended by java.text.Format
      extended by java.text.MessageFormat
All Implemented Interfaces:
Serializable, Cloneable

public class MessageFormat
extends Format

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.

See Also:
Serialized Form

Nested Class Summary
static class MessageFormat.Field
          The instances of this inner class are used as attribute keys in AttributedCharacterIterator that MessageFormat.formatToCharacterIterator() method returns.
 
Constructor Summary
MessageFormat(String template)
          Constructs a new MessageFormat using the specified pattern and the default Locale for Formats.
MessageFormat(String template, Locale locale)
          Constructs a new MessageFormat using the specified pattern and the specified Locale for Formats.
 
Method Summary
 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.
 StringBuffer format(Object[] objects, StringBuffer buffer, FieldPosition field)
          Formats the Object arguments into the specified StringBuffer using the pattern of this MessageFormat.
 StringBuffer format(Object object, StringBuffer buffer, FieldPosition field)
          Formats the specified object into the specified StringBuffer using the pattern of this MessageFormat.
static String format(String template, Object... objects)
          Formats the Object arguments using the specified MessageFormat pattern.
 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)
          Parse the message arguments from the specified String using the rules of this MessageFormat.
 Object[] parse(String string, ParsePosition position)
          Parse the message argument from the specified String starting at the index specified by the ParsePosition.
 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
convertPattern, format, getBundle, parseObject, upTo, upToWithQuotes
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MessageFormat

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

MessageFormat

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
Method Detail

applyPattern

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

clone

public Object clone()
Returns a new instance of MessageFormat with the same pattern and Formats as this MessageFormat.

Overrides:
clone in class Format
Returns:
a shallow copy of this MessageFormat
See Also:
Cloneable

equals

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.

Overrides:
equals in class Object
Parameters:
object - the object to compare with this object
Returns:
true if the specified object is equal to this MessageFormat, false otherwise
See Also:
hashCode()

formatToCharacterIterator

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.

Overrides:
formatToCharacterIterator in class Format
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

format

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

format

public final StringBuffer format(Object object,
                                 StringBuffer buffer,
                                 FieldPosition field)
Formats the specified object into the specified StringBuffer using the pattern of this MessageFormat.

Specified by:
format in class Format
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

format

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

getFormats

public Format[] getFormats()
Returns the Formats of this MessageFormat.

Returns:
an array of Format

getFormatsByArgumentIndex

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

setFormatByArgumentIndex

public void setFormatByArgumentIndex(int argIndex,
                                     Format format)
Sets the format used for argument at index argIndexto format

Parameters:
argIndex -
format -

setFormatsByArgumentIndex

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.

Parameters:
formats -

getLocale

public Locale getLocale()
Returns the Locale used when creating Formats.

Returns:
the Locale used to create Formats

hashCode

public int hashCode()
Returns an integer hash code for the receiver. Objects which are equal answer the same value for this method.

Overrides:
hashCode in class Object
Returns:
the receiver's hash
See Also:
equals(java.lang.Object)

parse

public Object[] parse(String string)
               throws ParseException
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

parse

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

parseObject

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.

Specified by:
parseObject in class Format
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

setFormat

public void setFormat(int offset,
                      Format format)
Sets the specified Format used by this MessageFormat.

Parameters:
offset - the format to change
format - the Format

setFormats

public void setFormats(Format[] formats)
Sets the Formats used by this MessageFormat.

Parameters:
formats - an array of Format

setLocale

public void setLocale(Locale locale)
Sets the Locale to use when creating Formats.

Parameters:
locale - the Locale

toPattern

public String toPattern()
Returns the pattern of this MessageFormat.

Returns:
the pattern

Build 1.0_r1(from source)

Please submit a feedback, bug or feature