Class ListFormat
- All Implemented Interfaces:
Serializable,Cloneable
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 Requirements:
- This class is immutable and thread-safe
- Since:
- 22
- External Specifications
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumstatic enumNested classes/interfaces declared in class java.text.Format
Format.Field -
Method Summary
Modifier and TypeMethodDescriptionbooleanCompares the specified object with thisListFormatfor equality.format(Object obj, StringBuffer toAppendTo, FieldPosition pos) Formats an object and appends the resulting text to a given string buffer.Returns the string that consists of the input strings, concatenated with the patterns of thisListFormat.static Locale[]Returns the available locales that support ListFormat.static ListFormatstatic ListFormatgetInstance(String[] patterns) Returns the ListFormat object for the specified patterns.static ListFormatgetInstance(Locale locale, ListFormat.Type type, ListFormat.Style style) Returns theLocaleof this ListFormat.String[]Returns the patterns used in this ListFormat.Returns the parsed list of strings from thesourcestring.parseObject(String source, ParsePosition parsePos) Parses text from a string to produce a list of strings.toString()Returns a string identifying thisListFormat, for debugging.Methods declared in class java.text.Format
clone, format, formatToCharacterIterator, parseObject
-
Method Details
-
getAvailableLocales
Returns the available locales that support ListFormat.- Returns:
- the available locales that support ListFormat
-
getInstance
- Returns:
- the ListFormat object for the default
FORMAT Locale,STANDARDtype, andFULLstyle
-
getInstance
- Parameters:
locale-Localeto be used, not nulltype- type of the ListFormat. One ofSTANDARD,OR, orUNIT, not nullstyle- style of the ListFormat. One ofFULL,SHORT, orNARROW, not null- Returns:
- the ListFormat object for the specified
Locale,Type, andStyle - Throws:
NullPointerException- if any of the arguments are null
-
getInstance
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
Locale,Type, orStyle.The patterns array should contain five String patterns, each corresponding to the Unicode LDML's
listPatternPart, i.e., "start", "middle", "end", two element, and three element patterns in this order. Each pattern contains "{0}" and "{1}" (and "{2}" for the three element pattern) placeholders that are substituted with the passed input strings on formatting. If the length of the patterns array is not 5, anIllegalArgumentExceptionis thrown.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 tostart := (start_before){0}start_between{1} middle := {0}middle_between{1} end := {0}end_between{1}(end_after) two := (two_before){0}two_between{1}(two_after) three := (three_before){0}three_between1{1}three_between2{2}(three_after)"(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 anIllegalArgumentException.On formatting, the input string list with
nelements substitutes above placeholders based on the number of elements:
As an example, the following table shows a pattern array which is equivalent ton = 1: {0} n = 2: parsed pattern for "two" n = 3: parsed pattern for "three" n > 3: (start_before){0}start_between{1}middle_between{2} ... middle_between{m}end_between{n}(end_after)STANDARDtype,FULLstyle in US English:
Here are the resulting formatted strings with the above pattern array.Pattern Kind Pattern String start "{0}, {1}" middle "{0}, {1}" end "{0}, and {1}" two "{0} and {1}" three "" Input String List Formatted String "Foo", "Bar", "Baz", "Qux" "Foo, Bar, Baz, and Qux" "Foo", "Bar", "Baz" "Foo, Bar, and Baz" "Foo", "Bar" "Foo and Bar" "Foo" "Foo" - Parameters:
patterns- array of patterns, not null- Returns:
- the ListFormat object for the specified patterns
- Throws:
IllegalArgumentException- if the lengthpatternsarray is not 5, or any ofstart,middle,end,two, orthreepatterns cannot be parsed.NullPointerException- ifpatternsis null.
-
getLocale
Returns theLocaleof this ListFormat. Thelocaleis defined bygetInstance(Locale, Type, Style)orgetInstance(String[]).- Returns:
- the
Localeof this ListFormat
-
getPatterns
Returns the patterns used in this ListFormat. Thepatternsare defined bygetInstance(Locale, Type, Style)orgetInstance(String[]).- Returns:
- the patterns used in this ListFormat
-
format
Returns the string that consists of the input strings, concatenated with the patterns of thisListFormat.- API Note:
- Formatting the string from an excessively long list may exceed memory or string sizes.
- Parameters:
input- The list of input strings to format. There should at least one String element in this list, otherwise anIllegalArgumentExceptionis thrown.- Returns:
- the string that consists of the input strings, concatenated with the
patterns of this
ListFormat - Throws:
IllegalArgumentException- if the length ofinputis zero.NullPointerException- ifinputis null.
-
format
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.- Specified by:
formatin classFormat- API Note:
- Formatting the string from an excessively long list or array may exceed memory or string sizes.
- Parameters:
obj- The object to format. Must be a List or an array of Object.toAppendTo- where the text is to be appendedpos- Ignored. Not used in ListFormat. May be null- Returns:
- the string buffer passed in as
toAppendTo, with formatted text appended - Throws:
NullPointerException- ifobjortoAppendTois nullIllegalArgumentException- ifobjis neither aListnor an array ofObjects, or its length is zero.
-
parse
Returns the parsed list of strings from thesourcestring. Note thatformat(List)and this method may not guarantee a round-trip, if the input strings contain ambiguous delimiters. 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".- Parameters:
source- the string to parse, not null.- Returns:
- the parsed list of strings from the
sourcestring - Throws:
ParseException- if parse failedNullPointerException- if source is null
-
parseObject
Parses text from a string to produce a list of strings.The method attempts to parse text starting at the index given by
parsePos. If parsing succeeds, then the index ofparsePosis updated to the index after the last character used (parsing does not necessarily use all characters up to the end of the string), and the parsed object is returned. The updatedparsePoscan be used to indicate the starting point for the next call to parse additional text. If an error occurs, then the index ofparsePosis not changed, the error index ofparsePosis set to the index of the character where the error occurred, and null is returned. See theparse(String)method for more information on list parsing.- Specified by:
parseObjectin classFormat- Parameters:
source- A string, part of which should be parsed.parsePos- AParsePositionobject with index and error index information as described above.- Returns:
- A list of string parsed from the
source. In case of error, returns null. - Throws:
NullPointerException- ifsourceorparsePosis null.IndexOutOfBoundsException- if the starting index given byparsePosis outsidesource.
-
equals
Compares the specified object with thisListFormatfor equality. Returnstrueif the specified object is also aListFormat, andlocaleandpatterns, returned fromgetLocale()andgetPatterns()respectively, are equal. -
toString
-