Top Description Fields Constructors Methods
org.apache.commons.text

public Class ExtendedMessageFormat

extends MessageFormat
Class Inheritance
Imports
java.text.Format, .MessageFormat, .ParsePosition, java.util.ArrayList, .Collection, .Collections, .HashMap, .Locale, .Locale.Category, .Map, .Objects, org.apache.commons.lang3.StringUtils, org.apache.commons.text.matcher.StringMatcherFactory

Extends java.text.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 java.text.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 java.text.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.

Note

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 java.text.MessageFormat:

Since
1.0

Field Summary

Modifier and TypeField and Description
private static final String
EMPTY_PATTERN

The empty string.

private static final char
END_FE

A right curly bracket.

private static final char
QUOTE

A properly escaped character representing a single quote.

private final Map<String, ? extends FormatFactory>
registry

Our registry of FormatFactory.

private static final long
private static final char
START_FE

A left curly bracket.

private static final char
START_FMT

A comma.

private String
toPattern

To pattern string.

Constructor Summary

AccessConstructor and Description
public
ExtendedMessageFormat(final String
the pattern to use, not null.
pattern
)

Constructs a new ExtendedMessageFormat for the default locale.

public
ExtendedMessageFormat(final String
the pattern to use, not null.
pattern
,
final Locale
the locale to use, not null.
locale
)

Constructs a new ExtendedMessageFormat.

public
ExtendedMessageFormat(final String
the pattern to use, not null.
pattern
,
final Locale
the locale to use, not null.
locale
,
final Map<String, ? extends FormatFactory>
the registry of format factories, may be null.
registry
)

Constructs a new ExtendedMessageFormat.

public
ExtendedMessageFormat(final String
the pattern to use, not null.
pattern
,
final Map<String, ? extends FormatFactory>
the registry of format factories, may be null.
registry
)

Constructs a new ExtendedMessageFormat for the default locale.

Method Summary

Modifier and TypeMethod and Description
private void
appendQuotedString(final String
pattern to parse.
pattern
,
final ParsePosition
current parse position.
pos
,
final StringBuilder
optional StringBuilder to append.
appendTo
)

Consumes a quoted string, adding it to appendTo if specified.

public final void
applyPattern(final String
String.
pattern
)

Overrides java.text.MessageFormat.applyPattern.

Applies the specified pattern.

private boolean

Returns:

true if some Object was found, false otherwise.
containsElements
(final Collection<?>
to check.
coll
)

Tests whether the specified Collection contains non-null elements.

public boolean
equals(final Object obj)

Overrides java.text.MessageFormat.equals.

Compares the specified object with this MessageFormat for equality.

private Format

Returns:

Format.
getFormat
(final String
String.
desc
)

Gets a custom format from a format description.

private void
getQuotedString(final String
pattern to parse.
pattern
,
final ParsePosition
current parse position.
pos
)

Consumes quoted string only.

public int
hashCode()

Overrides java.text.MessageFormat.hashCode.

Returns the hash code value for this MessageFormat.

private String

Returns:

full pattern.
insertFormats
(final String
source.
pattern
,
final ArrayList<String>
The custom patterns to re-insert, if any.
customPatterns
)

Inserts formats back into the pattern for toPattern() support.

private ParsePosition

Returns:

pos.
next
(final ParsePosition
ParsePosition.
pos
)

Advances parse position by 1.

private String

Returns:

Format description String.
parseFormatDescription
(final String
string to parse.
pattern
,
final ParsePosition
current parse position.
pos
)

Parses the format component of a format element.

private int

Returns:

argument index.
readArgumentIndex
(final String
pattern to parse.
pattern
,
final ParsePosition
current parse position.
pos
)

Reads the argument index from the current format element.

private void
seekNonWs(final String
String to read.
pattern
,
final ParsePosition
current position.
pos
)

Consumes whitespace from the current parse position.

public void
setFormat(final int
format element index.
formatElementIndex
,
final Format
the new format.
newFormat
)

Overrides java.text.MessageFormat.setFormat.

Throws UnsupportedOperationException, see class Javadoc for details.

public void
setFormatByArgumentIndex(final int
argument index.
argumentIndex
,
final Format
the new format.
newFormat
)

Overrides java.text.MessageFormat.setFormatByArgumentIndex.

Throws UnsupportedOperationException, see class Javadoc for details.

public void
setFormats(final Format[]
new formats.
newFormats
)

Overrides java.text.MessageFormat.setFormats.

Throws UnsupportedOperationException - see class Javadoc for details.

public void
setFormatsByArgumentIndex(final Format[]
new formats
newFormats
)

Overrides java.text.MessageFormat.setFormatsByArgumentIndex.

Throws UnsupportedOperationException - see class Javadoc for details.

public String
toPattern()

Overrides java.text.MessageFormat.toPattern.

Returns a String pattern adhering to the patterns section that represents the current state of this MessageFormat.

Inherited from java.text.MessageFormat:
cloneformatformatformatformatToCharacterIteratorgetFormatsgetFormatsByArgumentIndexgetLocaleparseparseparseObjectsetLocaletoString

Field Detail

EMPTY_PATTERNback to summary
private static final String EMPTY_PATTERN

The empty string.

END_FEback to summary
private static final char END_FE

A right curly bracket.

QUOTEback to summary
private static final char QUOTE

A properly escaped character representing a single quote.

registryback to summary
private final Map<String, ? extends FormatFactory> registry

Our registry of FormatFactory.

serialVersionUIDback to summary
private static final long serialVersionUID

Hides java.text.MessageFormat.serialVersionUID.

Serializable Object.

START_FEback to summary
private static final char START_FE

A left curly bracket.

START_FMTback to summary
private static final char START_FMT

A comma.

toPatternback to summary
private String toPattern

To pattern string.

Constructor Detail

ExtendedMessageFormatback to summary
public ExtendedMessageFormat(final String pattern)

Constructs a new ExtendedMessageFormat for the default locale.

Parameters
pattern:String

the pattern to use, not null.

Exceptions
IllegalArgumentException:
in case of a bad pattern.
ExtendedMessageFormatback to summary
public ExtendedMessageFormat(final String pattern, final Locale locale)

Constructs a new ExtendedMessageFormat.

Parameters
pattern:String

the pattern to use, not null.

locale:Locale

the locale to use, not null.

Exceptions
IllegalArgumentException:
in case of a bad pattern.
ExtendedMessageFormatback to summary
public ExtendedMessageFormat(final String pattern, final Locale locale, final Map<String, ? extends FormatFactory> registry)

Constructs a new ExtendedMessageFormat.

Parameters
pattern:String

the pattern to use, not null.

locale:Locale

the locale to use, not null.

registry:Map<String, ? extends FormatFactory>

the registry of format factories, may be null.

Exceptions
IllegalArgumentException:
in case of a bad pattern.
ExtendedMessageFormatback to summary
public ExtendedMessageFormat(final String pattern, final Map<String, ? extends FormatFactory> registry)

Constructs a new ExtendedMessageFormat for the default locale.

Parameters
pattern:String

the pattern to use, not null.

registry:Map<String, ? extends FormatFactory>

the registry of format factories, may be null.

Exceptions
IllegalArgumentException:
in case of a bad pattern.

Method Detail

appendQuotedStringback to summary
private void appendQuotedString(final String pattern, final ParsePosition pos, final StringBuilder appendTo)

Consumes a quoted string, adding it to appendTo if specified.

Parameters
pattern:String

pattern to parse.

pos:ParsePosition

current parse position.

appendTo:StringBuilder

optional StringBuilder to append.

applyPatternback to summary
public final void applyPattern(final String pattern)

Overrides java.text.MessageFormat.applyPattern.

Applies the specified pattern.

Parameters
pattern:String

String.

Annotations
@Override
containsElementsback to summary
private boolean containsElements(final Collection<?> coll)

Tests whether the specified Collection contains non-null elements.

Parameters
coll:Collection<?>

to check.

Returns:boolean

true if some Object was found, false otherwise.

equalsback to summary
public boolean equals(final Object obj)

Overrides java.text.MessageFormat.equals.

Doc from java.text.MessageFormat.equals.

Compares the specified object with this MessageFormat for equality. Returns true if the object is also a MessageFormat and the two formats would format any value the same.

Annotations
@Override
getFormatback to summary
private Format getFormat(final String desc)

Gets a custom format from a format description.

Parameters
desc:String

String.

Returns:Format

Format.

getQuotedStringback to summary
private void getQuotedString(final String pattern, final ParsePosition pos)

Consumes quoted string only.

Parameters
pattern:String

pattern to parse.

pos:ParsePosition

current parse position.

hashCodeback to summary
public int hashCode()

Overrides java.text.MessageFormat.hashCode.

Doc from java.text.MessageFormat.hashCode.

Returns the hash code value for this MessageFormat.

Annotations
@Override
insertFormatsback to summary
private String insertFormats(final String pattern, final ArrayList<String> customPatterns)

Inserts formats back into the pattern for toPattern() support.

Parameters
pattern:String

source.

customPatterns:ArrayList<String>

The custom patterns to re-insert, if any.

Returns:String

full pattern.

nextback to summary
private ParsePosition next(final ParsePosition pos)

Advances parse position by 1.

Parameters
pos:ParsePosition

ParsePosition.

Returns:ParsePosition

pos.

parseFormatDescriptionback to summary
private String parseFormatDescription(final String pattern, final ParsePosition pos)

Parses the format component of a format element.

Parameters
pattern:String

string to parse.

pos:ParsePosition

current parse position.

Returns:String

Format description String.

readArgumentIndexback to summary
private int readArgumentIndex(final String pattern, final ParsePosition pos)

Reads the argument index from the current format element.

Parameters
pattern:String

pattern to parse.

pos:ParsePosition

current parse position.

Returns:int

argument index.

seekNonWsback to summary
private void seekNonWs(final String pattern, final ParsePosition pos)

Consumes whitespace from the current parse position.

Parameters
pattern:String

String to read.

pos:ParsePosition

current position.

setFormatback to summary
public void setFormat(final int formatElementIndex, final Format newFormat)

Overrides java.text.MessageFormat.setFormat.

Throws UnsupportedOperationException, see class Javadoc for details.

Parameters
formatElementIndex:int

format element index.

newFormat:Format

the new format.

Annotations
@Override
Exceptions
UnsupportedOperationException:
always thrown since this isn't supported by ExtendedMessageFormat.
setFormatByArgumentIndexback to summary
public void setFormatByArgumentIndex(final int argumentIndex, final Format newFormat)

Overrides java.text.MessageFormat.setFormatByArgumentIndex.

Throws UnsupportedOperationException, see class Javadoc for details.

Parameters
argumentIndex:int

argument index.

newFormat:Format

the new format.

Annotations
@Override
Exceptions
UnsupportedOperationException:
always thrown since this isn't supported by ExtendedMessageFormat.
setFormatsback to summary
public void setFormats(final Format[] newFormats)

Overrides java.text.MessageFormat.setFormats.

Throws UnsupportedOperationException - see class Javadoc for details.

Parameters
newFormats:Format[]

new formats.

Annotations
@Override
Exceptions
UnsupportedOperationException:
always thrown since this isn't supported by ExtendedMessageFormat.
setFormatsByArgumentIndexback to summary
public void setFormatsByArgumentIndex(final Format[] newFormats)

Overrides java.text.MessageFormat.setFormatsByArgumentIndex.

Throws UnsupportedOperationException - see class Javadoc for details.

Parameters
newFormats:Format[]

new formats

Annotations
@Override
Exceptions
UnsupportedOperationException:
always thrown since this isn't supported by ExtendedMessageFormat
toPatternback to summary
public String toPattern()

Overrides java.text.MessageFormat.toPattern.

Doc from java.text.MessageFormat.toPattern.

Returns a String pattern adhering to the patterns section that represents the current state of this MessageFormat. The string is constructed from internal information and therefore does not necessarily equal the previously applied pattern. The order of FormatStyle matching is not guaranteed. That is, a FormatStyle produced may not be equivalent to the corresponding style passed, in the instance that multiple styles are equivalent.

Annotations
@Override