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

See Also:
Serialized Form

Nested Class Summary
static class MessageFormat.Field
           
 
Constructor Summary
MessageFormat(String pattern)
          Creates a new MessageFormat object with the specified pattern
MessageFormat(String pattern, Locale locale)
          Creates a new MessageFormat object with the specified pattern
 
Method Summary
 void applyPattern(String newPattern)
          Applies the specified pattern to this MessageFormat.
 Object clone()
          Overrides Format.clone()
 boolean equals(Object obj)
          Overrides Format.equals(Object obj)
 StringBuffer format(Object[] arguments, StringBuffer appendBuf, FieldPosition fp)
          Returns the pattern with the formatted objects.
 StringBuffer format(Object objectArray, StringBuffer appendBuf, FieldPosition fpos)
          Returns the pattern with the formatted objects.
static String format(String pattern, Object... arguments)
          A convinience method to format patterns.
 AttributedCharacterIterator formatToCharacterIterator(Object arguments)
          A convinience method to format patterns.
 Format[] getFormats()
          Returns an array with the Formats for the arguments.
 Format[] getFormatsByArgumentIndex()
          Return the formatters used sorted by argument index.
 Locale getLocale()
          Returns the locale.
 int hashCode()
          Overrides Format.hashCode()
 Object[] parse(String sourceStr)
           
 Object[] parse(String sourceStr, ParsePosition pos)
          Parse a string sourceStr against the pattern specified to the MessageFormat constructor.
 Object parseObject(String sourceStr, ParsePosition pos)
          This method parses a String and converts the parsed contents into an Object.
 void setFormat(int variableNum, Format newFormat)
          Sets the format for the argument at an specified index.
 void setFormatByArgumentIndex(int argumentIndex, Format newFormat)
          Set the format to used using the argument index number.
 void setFormats(Format[] newFormats)
          Sets the formats for the arguments.
 void setFormatsByArgumentIndex(Format[] newFormats)
          Set the format for argument using a specified array of formatters which is sorted according to the argument index.
 void setLocale(Locale loc)
          Sets the locale.
 String toPattern()
          Returns the pattern.
 
Methods inherited from class java.text.Format
format, parseObject
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MessageFormat

public MessageFormat(String pattern)
Creates a new MessageFormat object with the specified pattern

Parameters:
pattern - The Pattern

MessageFormat

public MessageFormat(String pattern,
                     Locale locale)
Creates a new MessageFormat object with the specified pattern

Parameters:
pattern - The Pattern
locale - The Locale to use
Since:
1.4
Method Detail

applyPattern

public void applyPattern(String newPattern)
Applies the specified pattern to this MessageFormat.

Parameters:
newPattern - The Pattern

clone

public Object clone()
Overrides Format.clone()

Overrides:
clone in class Format
Returns:
The copied Object.
See Also:
Cloneable

equals

public boolean equals(Object obj)
Overrides Format.equals(Object obj)

Overrides:
equals in class Object
Parameters:
obj - the Object to compare to
Returns:
whether this Object is semantically equal to another
See Also:
Object.hashCode()

formatToCharacterIterator

public AttributedCharacterIterator formatToCharacterIterator(Object arguments)
A convinience method to format patterns.

Overrides:
formatToCharacterIterator in class Format
Parameters:
arguments - The array containing the objects to be formatted.

format

public static String format(String pattern,
                            Object... arguments)
A convinience method to format patterns.

Parameters:
pattern - The pattern used when formatting.
arguments - The array containing the objects to be formatted.

format

public final StringBuffer format(Object[] arguments,
                                 StringBuffer appendBuf,
                                 FieldPosition fp)
Returns the pattern with the formatted objects.

Parameters:
arguments - The array containing the objects to be formatted.
appendBuf - The StringBuffer where the text is appened.
fp - A FieldPosition object (it is ignored).

format

public final StringBuffer format(Object objectArray,
                                 StringBuffer appendBuf,
                                 FieldPosition fpos)
Returns the pattern with the formatted objects. The first argument must be a array of Objects. This is equivalent to format((Object[]) objectArray, appendBuf, fpos)

Specified by:
format in class Format
Parameters:
objectArray - The object array to be formatted.
appendBuf - The StringBuffer where the text is appened.
fpos - A FieldPosition object (it is ignored).
Returns:
The updated StringBuffer.

getFormats

public Format[] getFormats()
Returns an array with the Formats for the arguments.


getLocale

public Locale getLocale()
Returns the locale.


hashCode

public int hashCode()
Overrides Format.hashCode()

Overrides:
hashCode in class Object
Returns:
the hash code for this Object
See Also:
Object.equals(Object), System.identityHashCode(Object)

parse

public Object[] parse(String sourceStr,
                      ParsePosition pos)
Parse a string sourceStr against the pattern specified to the MessageFormat constructor.

Parameters:
sourceStr - the string to be parsed.
pos - the current parse position (and eventually the error position).
Returns:
the array of parsed objects sorted according to their argument number in the pattern.

parse

public Object[] parse(String sourceStr)
               throws ParseException
Throws:
ParseException

parseObject

public Object parseObject(String sourceStr,
                          ParsePosition pos)
Description copied from class: Format
This method parses a String and converts the parsed contents into an Object.

Specified by:
parseObject in class Format
Parameters:
sourceStr - The String to parse.
pos - The starting parse index on input, the ending parse index on output.
Returns:
The parsed Object, or null in case of error.

setFormat

public void setFormat(int variableNum,
                      Format newFormat)
Sets the format for the argument at an specified index.

Parameters:
variableNum - The index.
newFormat - The Format object.

setFormats

public void setFormats(Format[] newFormats)
Sets the formats for the arguments.

Parameters:
newFormats - An array of Format objects.

setLocale

public void setLocale(Locale loc)
Sets the locale.

Parameters:
loc - A Locale

toPattern

public String toPattern()
Returns the pattern.


getFormatsByArgumentIndex

public Format[] getFormatsByArgumentIndex()
Return the formatters used sorted by argument index. It uses the internal table to fill in this array: if a format has been set using setFormat or setFormatByArgumentIndex then it returns it at the right index. If not it uses the detected formatters during a format call. If nothing is known about that argument index it just puts null at that position. To get useful informations you may have to call format at least once.

Returns:
an array of formatters sorted by argument index.

setFormatByArgumentIndex

public void setFormatByArgumentIndex(int argumentIndex,
                                     Format newFormat)
Set the format to used using the argument index number.

Parameters:
argumentIndex - the argument index.
newFormat - the format to use for this argument.

setFormatsByArgumentIndex

public void setFormatsByArgumentIndex(Format[] newFormats)
Set the format for argument using a specified array of formatters which is sorted according to the argument index. If the number of elements in the array is fewer than the number of arguments only the arguments specified by the array are touched.

Parameters:
newFormats - array containing the new formats to set.
Throws:
NullPointerException - if newFormats is null