8318569: Add getter methods for Locale and Patterns in ListFormat

Reviewed-by: joehw, rriggs, iris, mli
This commit is contained in:
Naoto Sato 2023-10-24 16:54:57 +00:00
parent 6f352740cb
commit 116503754c
2 changed files with 60 additions and 1 deletions

View File

@ -322,6 +322,26 @@ public final class ListFormat extends Format {
return new ListFormat(Locale.ROOT, Arrays.copyOf(patterns, PATTERN_ARRAY_LENGTH)); return new ListFormat(Locale.ROOT, Arrays.copyOf(patterns, PATTERN_ARRAY_LENGTH));
} }
/**
* {@return the {@code Locale} of this ListFormat}
*
* The {@code locale} is defined by {@link #getInstance(Locale, Type, Style)} or
* {@link #getInstance(String[])}.
*/
public Locale getLocale() {
return locale;
}
/**
* {@return the patterns used in this ListFormat}
*
* The {@code patterns} are defined by {@link #getInstance(Locale, Type, Style)} or
* {@link #getInstance(String[])}.
*/
public String[] getPatterns() {
return Arrays.copyOf(patterns, patterns.length);
}
/** /**
* {@return the string that consists of the input strings, concatenated with the * {@return the string that consists of the input strings, concatenated with the
* patterns of this {@code ListFormat}} * patterns of this {@code ListFormat}}

View File

@ -23,7 +23,7 @@
/* /*
* @test * @test
* @bug 8041488 8316974 * @bug 8041488 8316974 8318569
* @summary Tests for ListFormat class * @summary Tests for ListFormat class
* @run junit TestListFormat * @run junit TestListFormat
*/ */
@ -200,6 +200,7 @@ public class TestListFormat {
arguments(CUSTOM_PATTERNS_MINIMAL, SAMPLE4), arguments(CUSTOM_PATTERNS_MINIMAL, SAMPLE4),
}; };
} }
static Arguments[] getInstance_3Arg_InheritPatterns() { static Arguments[] getInstance_3Arg_InheritPatterns() {
return new Arguments[] { return new Arguments[] {
arguments(ListFormat.Type.STANDARD, ListFormat.Style.FULL), arguments(ListFormat.Type.STANDARD, ListFormat.Style.FULL),
@ -213,6 +214,17 @@ public class TestListFormat {
arguments(ListFormat.Type.UNIT, ListFormat.Style.NARROW), arguments(ListFormat.Type.UNIT, ListFormat.Style.NARROW),
}; };
} }
static Arguments[] getLocale_localeDependent() {
return new Arguments[] {
arguments(Locale.ROOT),
arguments(Locale.US),
arguments(Locale.GERMANY),
arguments(Locale.JAPAN),
arguments(Locale.SIMPLIFIED_CHINESE),
};
}
@ParameterizedTest @ParameterizedTest
@MethodSource @MethodSource
void getInstance_1Arg(String[] patterns, List<String> input, String expected) throws ParseException { void getInstance_1Arg(String[] patterns, List<String> input, String expected) throws ParseException {
@ -235,6 +247,33 @@ public class TestListFormat {
compareResult(f, SAMPLE3, expected, roundTrip); compareResult(f, SAMPLE3, expected, roundTrip);
} }
@Test
void getLocale_invariant() {
var f = ListFormat.getInstance(CUSTOM_PATTERNS_FULL);
assertEquals(Locale.ROOT, f.getLocale());
}
@Test
void getLocale_default() {
var f = ListFormat.getInstance();
assertEquals(Locale.getDefault(Locale.Category.FORMAT), f.getLocale());
}
@ParameterizedTest
@MethodSource
void getLocale_localeDependent(Locale l) {
var f = ListFormat.getInstance(l, ListFormat.Type.STANDARD, ListFormat.Style.FULL);
assertEquals(l, f.getLocale());
}
@Test
void getPatterns_immutability() {
var f = ListFormat.getInstance(CUSTOM_PATTERNS_FULL);
var p = f.getPatterns();
p[0] = null;
assertArrayEquals(CUSTOM_PATTERNS_FULL, f.getPatterns());
}
@Test @Test
void format_3Arg() { void format_3Arg() {
var f = ListFormat.getInstance(); var f = ListFormat.getInstance();