Class ExtendedMessageFormat

java.lang.Object
java.text.Format
java.text.MessageFormat
org.apache.commons.text.ExtendedMessageFormat
All Implemented Interfaces:
Serializable, Cloneable

public class ExtendedMessageFormat extends MessageFormat
Extends MessageFormat to allow pluggable/additional formatting options for embedded format elements. Client code should specify a registry of FormatFactory instances associated with String format names. This registry will be consulted when the format elements are parsed from the message pattern. In this way custom patterns can be specified, and the formats supported by MessageFormat can be overridden at the format and/or format style level (see MessageFormat). A "format element" embedded in the message pattern is specified (()? signifies optionality):
{}<em>argument-number</em><strong>(</strong>{@code ,}<em>format-name</em><b> (</b>{@code ,}<em>format-style</em><strong>)?)?</strong>{@code }

format-name and format-style values are trimmed of surrounding whitespace in the manner of MessageFormat. If format-name denotes FormatFactory formatFactoryInstance in registry, a Format matching format-name and format-style is requested from formatFactoryInstance. If this is successful, the Format found is used for this format element.

NOTICE: The various subformat mutator methods are considered unnecessary; they exist on the parent class to allow the type of customization which it is the job of this class to provide in a configurable fashion. These methods have thus been disabled and will throw UnsupportedOperationException if called.

Limitations inherited from MessageFormat:

  • When using "choice" subformats, support for nested formatting instructions is limited to that provided by the base class.
  • Thread-safety of Formats, including MessageFormat and thus ExtendedMessageFormat, is not guaranteed.
Since:
1.0
See Also:
  • Field Details

  • Constructor Details

    • ExtendedMessageFormat

      public ExtendedMessageFormat(String pattern)
      Constructs a new ExtendedMessageFormat for the default locale.
      Parameters:
      pattern - the pattern to use, not null.
      Throws:
      IllegalArgumentException - in case of a bad pattern.
    • ExtendedMessageFormat

      public ExtendedMessageFormat(String pattern, Locale locale)
      Constructs a new ExtendedMessageFormat.
      Parameters:
      pattern - the pattern to use, not null.
      locale - the locale to use, not null.
      Throws:
      IllegalArgumentException - in case of a bad pattern.
    • ExtendedMessageFormat

      public ExtendedMessageFormat(String pattern, Locale locale, Map<String, ? extends FormatFactory> registry)
      Constructs a new ExtendedMessageFormat.
      Parameters:
      pattern - the pattern to use, not null.
      locale - the locale to use, not null.
      registry - the registry of format factories, may be null.
      Throws:
      IllegalArgumentException - in case of a bad pattern.
    • ExtendedMessageFormat

      public ExtendedMessageFormat(String pattern, Map<String, ? extends FormatFactory> registry)
      Constructs a new ExtendedMessageFormat for the default locale.
      Parameters:
      pattern - the pattern to use, not null.
      registry - the registry of format factories, may be null.
      Throws:
      IllegalArgumentException - in case of a bad pattern.
  • Method Details

    • appendQuotedString

      private void appendQuotedString(String pattern, ParsePosition pos, StringBuilder appendTo)
      Consumes a quoted string, adding it to appendTo if specified.
      Parameters:
      pattern - pattern to parse.
      pos - current parse position.
      appendTo - optional StringBuilder to append.
    • applyPattern

      public final void applyPattern(String pattern)
      Applies the specified pattern.
      Overrides:
      applyPattern in class MessageFormat
      Parameters:
      pattern - String.
    • containsElements

      private boolean containsElements(Collection<?> coll)
      Tests whether the specified Collection contains non-null elements.
      Parameters:
      coll - to check.
      Returns:
      true if some Object was found, false otherwise.
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class MessageFormat
    • getFormat

      private Format getFormat(String desc)
      Gets a custom format from a format description.
      Parameters:
      desc - String.
      Returns:
      Format.
    • getQuotedString

      private void getQuotedString(String pattern, ParsePosition pos)
      Consumes quoted string only.
      Parameters:
      pattern - pattern to parse.
      pos - current parse position.
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class MessageFormat
    • insertFormats

      private String insertFormats(String pattern, ArrayList<String> customPatterns)
      Inserts formats back into the pattern for toPattern() support.
      Parameters:
      pattern - source.
      customPatterns - The custom patterns to re-insert, if any.
      Returns:
      full pattern.
    • next

      private ParsePosition next(ParsePosition pos)
      Advances parse position by 1.
      Parameters:
      pos - ParsePosition.
      Returns:
      pos.
    • parseFormatDescription

      private String parseFormatDescription(String pattern, ParsePosition pos)
      Parses the format component of a format element.
      Parameters:
      pattern - string to parse.
      pos - current parse position.
      Returns:
      Format description String.
    • readArgumentIndex

      private int readArgumentIndex(String pattern, ParsePosition pos)
      Reads the argument index from the current format element.
      Parameters:
      pattern - pattern to parse.
      pos - current parse position.
      Returns:
      argument index.
    • seekNonWs

      private void seekNonWs(String pattern, ParsePosition pos)
      Consumes whitespace from the current parse position.
      Parameters:
      pattern - String to read.
      pos - current position.
    • setFormat

      public void setFormat(int formatElementIndex, Format newFormat)
      Throws UnsupportedOperationException, see class Javadoc for details.
      Overrides:
      setFormat in class MessageFormat
      Parameters:
      formatElementIndex - format element index.
      newFormat - the new format.
      Throws:
      UnsupportedOperationException - always thrown since this isn't supported by ExtendedMessageFormat.
    • setFormatByArgumentIndex

      public void setFormatByArgumentIndex(int argumentIndex, Format newFormat)
      Throws UnsupportedOperationException, see class Javadoc for details.
      Overrides:
      setFormatByArgumentIndex in class MessageFormat
      Parameters:
      argumentIndex - argument index.
      newFormat - the new format.
      Throws:
      UnsupportedOperationException - always thrown since this isn't supported by ExtendedMessageFormat.
    • setFormats

      public void setFormats(Format[] newFormats)
      Throws UnsupportedOperationException - see class Javadoc for details.
      Overrides:
      setFormats in class MessageFormat
      Parameters:
      newFormats - new formats.
      Throws:
      UnsupportedOperationException - always thrown since this isn't supported by ExtendedMessageFormat.
    • setFormatsByArgumentIndex

      public void setFormatsByArgumentIndex(Format[] newFormats)
      Throws UnsupportedOperationException - see class Javadoc for details.
      Overrides:
      setFormatsByArgumentIndex in class MessageFormat
      Parameters:
      newFormats - new formats
      Throws:
      UnsupportedOperationException - always thrown since this isn't supported by ExtendedMessageFormat
    • toPattern

      public String toPattern()
      Overrides:
      toPattern in class MessageFormat