Class ExtendedMessageFormat
java.lang.Object
java.text.Format
java.text.MessageFormat
org.apache.commons.text.ExtendedMessageFormat
- All Implemented Interfaces:
Serializable, Cloneable
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, includingMessageFormatand thusExtendedMessageFormat, is not guaranteed.
- Since:
- 1.0
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from class MessageFormat
MessageFormat.Field -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final StringThe empty string.private static final charA right curly bracket.private static final charA properly escaped character representing a single quote.private final Map<String, ? extends FormatFactory> Our registry of FormatFactory.private static final longSerializable Object.private static final charA left curly bracket.private static final charA comma.private StringTo pattern string. -
Constructor Summary
ConstructorsConstructorDescriptionExtendedMessageFormat(String pattern) Constructs a new ExtendedMessageFormat for the default locale.ExtendedMessageFormat(String pattern, Locale locale) Constructs a new ExtendedMessageFormat.ExtendedMessageFormat(String pattern, Locale locale, Map<String, ? extends FormatFactory> registry) Constructs a new ExtendedMessageFormat.ExtendedMessageFormat(String pattern, Map<String, ? extends FormatFactory> registry) Constructs a new ExtendedMessageFormat for the default locale. -
Method Summary
Modifier and TypeMethodDescriptionprivate voidappendQuotedString(String pattern, ParsePosition pos, StringBuilder appendTo) Consumes a quoted string, adding it toappendToif specified.final voidapplyPattern(String pattern) Applies the specified pattern.private booleancontainsElements(Collection<?> coll) Tests whether the specified Collection contains non-null elements.booleanprivate FormatGets a custom format from a format description.private voidgetQuotedString(String pattern, ParsePosition pos) Consumes quoted string only.inthashCode()private StringinsertFormats(String pattern, ArrayList<String> customPatterns) Inserts formats back into the pattern for toPattern() support.private ParsePositionnext(ParsePosition pos) Advances parse position by 1.private StringparseFormatDescription(String pattern, ParsePosition pos) Parses the format component of a format element.private intreadArgumentIndex(String pattern, ParsePosition pos) Reads the argument index from the current format element.private voidseekNonWs(String pattern, ParsePosition pos) Consumes whitespace from the current parse position.voidThrows UnsupportedOperationException, see class Javadoc for details.voidsetFormatByArgumentIndex(int argumentIndex, Format newFormat) Throws UnsupportedOperationException, see class Javadoc for details.voidsetFormats(Format[] newFormats) Throws UnsupportedOperationException - see class Javadoc for details.voidsetFormatsByArgumentIndex(Format[] newFormats) Throws UnsupportedOperationException - see class Javadoc for details.Methods inherited from class MessageFormat
clone, format, format, format, formatToCharacterIterator, getFormats, getFormatsByArgumentIndex, getLocale, parse, parse, parseObject, setLocaleMethods inherited from class Format
format, parseObject
-
Field Details
-
serialVersionUID
private static final long serialVersionUIDSerializable Object.- See Also:
-
EMPTY_PATTERN
-
START_FMT
private static final char START_FMTA comma.- See Also:
-
END_FE
private static final char END_FEA right curly bracket.- See Also:
-
START_FE
private static final char START_FEA left curly bracket.- See Also:
-
QUOTE
private static final char QUOTEA properly escaped character representing a single quote.- See Also:
-
toPattern
To pattern string. -
registry
Our registry of FormatFactory.
-
-
Constructor Details
-
ExtendedMessageFormat
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
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
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
Consumes a quoted string, adding it toappendToif specified.- Parameters:
pattern- pattern to parse.pos- current parse position.appendTo- optional StringBuilder to append.
-
applyPattern
Applies the specified pattern.- Overrides:
applyPatternin classMessageFormat- Parameters:
pattern- String.
-
containsElements
Tests whether the specified Collection contains non-null elements.- Parameters:
coll- to check.- Returns:
trueif some Object was found,falseotherwise.
-
equals
- Overrides:
equalsin classMessageFormat
-
getFormat
-
getQuotedString
Consumes quoted string only.- Parameters:
pattern- pattern to parse.pos- current parse position.
-
hashCode
public int hashCode()- Overrides:
hashCodein classMessageFormat
-
insertFormats
-
next
Advances parse position by 1.- Parameters:
pos- ParsePosition.- Returns:
pos.
-
parseFormatDescription
Parses the format component of a format element.- Parameters:
pattern- string to parse.pos- current parse position.- Returns:
- Format description String.
-
readArgumentIndex
Reads the argument index from the current format element.- Parameters:
pattern- pattern to parse.pos- current parse position.- Returns:
- argument index.
-
seekNonWs
Consumes whitespace from the current parse position.- Parameters:
pattern- String to read.pos- current position.
-
setFormat
Throws UnsupportedOperationException, see class Javadoc for details.- Overrides:
setFormatin classMessageFormat- Parameters:
formatElementIndex- format element index.newFormat- the new format.- Throws:
UnsupportedOperationException- always thrown since this isn't supported byExtendedMessageFormat.
-
setFormatByArgumentIndex
Throws UnsupportedOperationException, see class Javadoc for details.- Overrides:
setFormatByArgumentIndexin classMessageFormat- Parameters:
argumentIndex- argument index.newFormat- the new format.- Throws:
UnsupportedOperationException- always thrown since this isn't supported byExtendedMessageFormat.
-
setFormats
Throws UnsupportedOperationException - see class Javadoc for details.- Overrides:
setFormatsin classMessageFormat- Parameters:
newFormats- new formats.- Throws:
UnsupportedOperationException- always thrown since this isn't supported byExtendedMessageFormat.
-
setFormatsByArgumentIndex
Throws UnsupportedOperationException - see class Javadoc for details.- Overrides:
setFormatsByArgumentIndexin classMessageFormat- Parameters:
newFormats- new formats- Throws:
UnsupportedOperationException- always thrown since this isn't supported byExtendedMessageFormat
-
toPattern
- Overrides:
toPatternin classMessageFormat
-