ListFormat formats or parses a list of strings in a locale-sensitive way.
Use ListFormat to construct a list of strings displayed for end users.
For example, displaying a list of 3 weekdays, e.g. "Monday", "Wednesday", "Friday"
as "Monday, Wednesday, and Friday" in an inclusive list type. This class provides
the functionality defined in Unicode Consortium's LDML specification for
List Patterns.
Three formatting types are provided: STANDARD, OR,
and UNIT, which determines the punctuation
between the strings and the connecting words if any. Also, three formatting styles for each
type are provided: FULL, SHORT, and
NARROW, suitable for how the strings are abbreviated (or not).
The following snippet is an example of formatting
the list of Strings "Foo", "Bar", "Baz" in US English with
STANDARD type and FULL style:
ListFormat.getInstance(Locale.US, ListFormat.Type.STANDARD, ListFormat.Style.FULL) .format(List.of("Foo", "Bar", "Baz"))
| FULL | SHORT | NARROW | |
|---|---|---|---|
| STANDARD | Foo, Bar, and Baz | Foo, Bar, & Baz | Foo, Bar, Baz |
| OR | Foo, Bar, or Baz | Foo, Bar, or Baz | Foo, Bar, or Baz |
| UNIT | Foo, Bar, Baz | Foo, Bar, Baz | Foo Bar Baz |
Alternatively, Locale, Type, and/or Style independent instances
can be created with getInstance(String[]). The String array to the
method specifies the delimiting patterns for the start/middle/end portion of
the formatted string, as well as optional specialized patterns for two or three
elements. Refer to the method description for more detail.
On parsing, if some ambiguity is found in the input string, such as delimiting sequences in the input string, the result, when formatted with the same formatting, does not re-produce the input string. For example, a two element String list "a, b,", "c" will be formatted as "a, b, and c", but may be parsed as three elements "a", "b", "c".
Implementation Specification
This class is immutable and thread-safe
| Modifier and Type | Class and Description |
|---|---|
| public static enum | |
| public static enum |
| Modifier and Type | Field and Description |
|---|---|
| private static final int | |
| private transient String | |
| private transient String | |
| private final Locale | locale
The locale to use for formatting list patterns. |
| private static final int | |
| private transient String | |
| private static final int | |
| private final String[] | patterns
The array of five pattern Strings. |
| private static final int | |
| private static final long | |
| private static final int | |
| private transient String | |
| private transient String | |
| private static final int | |
| private static final int |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| private String | |
| public boolean | Returns: true if the specified object is equal to this ListFormatthe object to be compared for equality. obj)Overrides java. Compares the specified object with this |
| private static int[] | Returns: the start and end positions of the first pattern found in the givensource starting at pos, or null if no such
pattern existsstring to search source, int position at which to start the search pos, String starting string within the pattern prefix, String ending string within the pattern suffix)Returns the start and end positions of the first pattern found in
the given |
| private static int[] | Returns: the positions of the "{0}", "{1}", and "{2}" placeholders in the given pattern string, or null if the pattern is invalidpattern string to parse pattern)Returns the positions of the "{0}", "{1}", and "{2}" placeholders in the given pattern string, or null if the pattern is invalid. |
| public String | Returns: the string that consists of the input strings, concatenated with the patterns of thisListFormatThe list of input strings to format. There should at least
one String element in this list, otherwise an input)IllegalArgumentException
is thrown.Returns the string that consists of the input strings, concatenated with the
patterns of this |
| public StringBuffer | Returns: the string buffer passed in astoAppendTo,
with formatted text appendedThe object to format. Must be a List or an array
of Object. obj, StringBuffer where the text is to be appended toAppendTo, FieldPosition Ignored. Not used in ListFormat. May be null pos)Implements abstract java. Formats an object and appends the resulting text to a given string buffer. |
| pack-priv Format. | format(Object obj, Format.
Overrides java.
|
| private Format. | |
| public AttributedCharacterIterator | formatToCharacterIterator(Object arguments)
Overrides java. Formats an Object producing an |
| private MessageFormat | |
| public static Locale[] | Returns: the available locales that support ListFormatReturns the available locales that support ListFormat. |
| public static ListFormat | getInstance()
Returns the ListFormat object for the default
|
| public static ListFormat | getInstance(Locale
Locale to be used, not nulltype of the ListFormat. One of type,STANDARD, OR,
or UNIT, not nullstyle of the ListFormat. One of styleFULL, SHORT,
or NARROW, not nullReturns the ListFormat object for the specified |
| public static ListFormat | Returns: the ListFormat object for the specified patternsarray of patterns, not null patterns)Returns the ListFormat object for the specified patterns. |
| public Locale | |
| public String[] | Returns: the patterns used in this ListFormatReturns the patterns used in this ListFormat. |
| public int | |
| private void | |
| public List | |
| public Object | Returns: A list of string parsed from thesource.
In case of error, returns null.A string, part of which should be parsed. source, ParsePosition A parsePos)ParsePosition object with index and error
index information as described above.Implements abstract java. Parses text from a string to produce a list of strings. |
| private void | |
| public String | Returns: a string identifying thisListFormat, for debuggingOverrides java. Returns a string identifying this |
| END | back to summary |
|---|---|
| private static final int END | |
| endAfter | back to summary |
|---|---|
| private transient String endAfter | |
| endBetween | back to summary |
|---|---|
| private transient String endBetween | |
| locale | back to summary |
|---|---|
| private final Locale locale The locale to use for formatting list patterns. | |
| MIDDLE | back to summary |
|---|---|
| private static final int MIDDLE | |
| middleBetween | back to summary |
|---|---|
| private transient String middleBetween | |
| PATTERN_ARRAY_LENGTH | back to summary |
|---|---|
| private static final int PATTERN_ARRAY_LENGTH | |
| patterns | back to summary |
|---|---|
| private final String[] patterns The array of five pattern Strings. Each element corresponds to the Unicode LDML's `listPatternsPart` type, i.e, start/middle/end/two/three. | |
| PLACEHOLDER_LENGTH | back to summary |
|---|---|
| private static final int PLACEHOLDER_LENGTH | |
| serialVersionUID | back to summary |
|---|---|
| private static final long serialVersionUID Hides java. | |
| START | back to summary |
|---|---|
| private static final int START | |
| startBefore | back to summary |
|---|---|
| private transient String startBefore | |
| startBetween | back to summary |
|---|---|
| private transient String startBetween | |
| THREE | back to summary |
|---|---|
| private static final int THREE | |
| TWO | back to summary |
|---|---|
| private static final int TWO | |
| ListFormat | back to summary |
|---|---|
| private ListFormat(Locale l, String[] patterns) | |
| createMessageFormatString | back to summary |
|---|---|
| private String createMessageFormatString(int count) | |
| equals | back to summary |
|---|---|
| public boolean equals(Object obj) Overrides java. Compares the specified object with this | |
| findPattern | back to summary |
|---|---|
| private static int[] findPattern(String source, int pos, String prefix, String suffix) Returns the start and end positions of the first pattern found in
the given
| |
| findPlaceholders | back to summary |
|---|---|
| private static int[] findPlaceholders(String pattern) Returns the positions of the "{0}", "{1}", and "{2}" placeholders in the given pattern string, or null if the pattern is invalid. The returned array contains -1 for "{2}" if that placeholder is absent.
| |
| format | back to summary |
|---|---|
| public String format(List<String> input) Returns the string that consists of the input strings, concatenated with the
patterns of this API Note Formatting the string from an excessively long list may exceed memory or string sizes.
| |
| format | back to summary |
|---|---|
| public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) Implements abstract java. Formats an object and appends the resulting text to a given string buffer. The object should either be a List or an array of Objects. API Note Formatting the string from an excessively long list or array may exceed memory or string sizes.
| |
| format | back to summary |
|---|---|
| pack-priv Format. Overrides java.
| |
| format | back to summary |
|---|---|
| private Format. | |
| formatToCharacterIterator | back to summary |
|---|---|
| public AttributedCharacterIterator formatToCharacterIterator(Object arguments) Overrides java. Doc from java. Formats an Object producing an
Each attribute key of the AttributedCharacterIterator will be of type
| |
| generateMessageFormat | back to summary |
|---|---|
| private MessageFormat generateMessageFormat(Object[] input) | |
| getAvailableLocales | back to summary |
|---|---|
| public static Locale[] getAvailableLocales() Returns the available locales that support ListFormat.
| |
| getInstance | back to summary |
|---|---|
| public static ListFormat getInstance() Returns the ListFormat object for the default
| |
| getInstance | back to summary |
|---|---|
| public static ListFormat getInstance(Locale locale, ListFormat. Returns the ListFormat object for the specified
| |
| getInstance | back to summary | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| public static ListFormat getInstance(String[] patterns) Returns the ListFormat object for the specified patterns.
This factory returns an instance based on the customized patterns array,
instead of letting the runtime provide appropriate patterns for the
The patterns array should contain five String patterns, each corresponding to the Unicode LDML's
Each pattern string is first parsed as follows. Literals in parentheses, such as "start_before", are optional:
If two or three pattern string is empty, it falls back to
"(start_before){0}end_between{1}(end_after)",
"(start_before){0}start_between{1}end_between{2}(end_after)" respectively.
If parsing of any pattern string for start, middle, end, two, or three fails,
it throws an IllegalArgumentException.
On formatting, the input string list with
As an example, the following table shows a pattern array which is equivalent to
STANDARD type, FULL style in US English:
| |||||||||||||||||||||||
| getLocale | back to summary |
|---|---|
| public Locale getLocale() Returns the
| |
| getPatterns | back to summary |
|---|---|
| public String[] getPatterns() Returns the patterns used in this ListFormat.
The
| |
| hashCode | back to summary |
|---|---|
| public int hashCode() Overrides java. Doc from java. Returns a hash code value for this object. This method is
supported for the benefit of hash tables such as those provided by
The general contract of
| |
| init | back to summary |
|---|---|
| private void init() | |
| parse | back to summary |
|---|---|
| public List Returns the parsed list of strings from the
| |
| parseObject | back to summary |
|---|---|
| public Object parseObject(String source, ParsePosition parsePos) Implements abstract java. Parses text from a string to produce a list of strings.
The method attempts to parse text starting at the index given by
| |
| readObject | back to summary |
|---|---|
private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException
| |
| toString | back to summary |
|---|---|
| public String toString() Overrides java. Returns a string identifying this | |
FULL, SHORT,
and NARROW.
Style is an enum which represents the style for formatting
a list within a given ListFormat instance.
| Modifier and Type | Field and Description |
|---|---|
| public static final ListFormat. | FULL
The |
| public static final ListFormat. | NARROW
The |
| public static final ListFormat. | SHORT
The |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public static ListFormat. | |
| public static ListFormat. |
| FULL | back to summary |
|---|---|
| public static final ListFormat. The | |
| NARROW | back to summary |
|---|---|
| public static final ListFormat. The | |
| SHORT | back to summary |
|---|---|
| public static final ListFormat. The | |
| Style | back to summary |
|---|---|
| private Style() | |
| valueOf | back to summary |
|---|---|
| public static ListFormat. | |
| values | back to summary |
|---|---|
| public static ListFormat. | |
STANDARD, OR, and
UNIT.
Type is an enum which represents the type for formatting
a list within a given ListFormat instance. It determines
the punctuation and the connecting words in the formatted text.
| Modifier and Type | Field and Description |
|---|---|
| public static final ListFormat. | OR
The |
| public static final ListFormat. | STANDARD
The |
| public static final ListFormat. | UNIT
The |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public static ListFormat. | |
| public static ListFormat. |
| OR | back to summary |
|---|---|
| public static final ListFormat. The | |
| STANDARD | back to summary |
|---|---|
| public static final ListFormat. The | |
| UNIT | back to summary |
|---|---|
| public static final ListFormat. The | |
| Type | back to summary |
|---|---|
| private Type() | |
| valueOf | back to summary |
|---|---|
| public static ListFormat. | |
| values | back to summary |
|---|---|
| public static ListFormat. | |