Skip to content

Commit

Permalink
8318569: Add getter methods for Locale and Patterns in ListFormat
Browse files Browse the repository at this point in the history
Reviewed-by: joehw, rriggs, iris, mli
  • Loading branch information
naotoj committed Oct 24, 2023
1 parent 6f35274 commit 1165037
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
20 changes: 20 additions & 0 deletions src/java.base/share/classes/java/text/ListFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,26 @@ public static ListFormat getInstance(String[] patterns) {
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
* patterns of this {@code ListFormat}}
Expand Down
41 changes: 40 additions & 1 deletion test/jdk/java/text/Format/ListFormat/TestListFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

/*
* @test
* @bug 8041488 8316974
* @bug 8041488 8316974 8318569
* @summary Tests for ListFormat class
* @run junit TestListFormat
*/
Expand Down Expand Up @@ -200,6 +200,7 @@ static Arguments[] parseObject_parsePos() {
arguments(CUSTOM_PATTERNS_MINIMAL, SAMPLE4),
};
}

static Arguments[] getInstance_3Arg_InheritPatterns() {
return new Arguments[] {
arguments(ListFormat.Type.STANDARD, ListFormat.Style.FULL),
Expand All @@ -213,6 +214,17 @@ static Arguments[] getInstance_3Arg_InheritPatterns() {
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
@MethodSource
void getInstance_1Arg(String[] patterns, List<String> input, String expected) throws ParseException {
Expand All @@ -235,6 +247,33 @@ void getInstance_3Arg(Locale l, ListFormat.Type type, ListFormat.Style style, St
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
void format_3Arg() {
var f = ListFormat.getInstance();
Expand Down

1 comment on commit 1165037

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.