Skip to content

Commit 142ce41

Browse files
committed
Accept repeated locale as an input of LocaleList construction.
Repeated locale has not been accepted and IllegalArgumentException is thrown. Instead of throwing exception, dropping repeated locale instead. Bug: 152410253 Test: atest LocaleListTest Change-Id: I80f243678ac3024eaeb0349f770cff897df7f332
1 parent 043a322 commit 142ce41

1 file changed

Lines changed: 6 additions & 5 deletions

File tree

core/java/android/os/LocaleList.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
import com.android.internal.annotations.GuardedBy;
2626

27+
import java.util.ArrayList;
2728
import java.util.Arrays;
2829
import java.util.Collection;
2930
import java.util.HashSet;
@@ -150,37 +151,37 @@ public String toLanguageTags() {
150151
/**
151152
* Creates a new {@link LocaleList}.
152153
*
154+
* If two or more same locales are passed, the repeated locales will be dropped.
153155
* <p>For empty lists of {@link Locale} items it is better to use {@link #getEmptyLocaleList()},
154156
* which returns a pre-constructed empty list.</p>
155157
*
156158
* @throws NullPointerException if any of the input locales is <code>null</code>.
157-
* @throws IllegalArgumentException if any of the input locales repeat.
158159
*/
159160
public LocaleList(@NonNull Locale... list) {
160161
if (list.length == 0) {
161162
mList = sEmptyList;
162163
mStringRepresentation = "";
163164
} else {
164-
final Locale[] localeList = new Locale[list.length];
165+
final ArrayList<Locale> localeList = new ArrayList<>();
165166
final HashSet<Locale> seenLocales = new HashSet<Locale>();
166167
final StringBuilder sb = new StringBuilder();
167168
for (int i = 0; i < list.length; i++) {
168169
final Locale l = list[i];
169170
if (l == null) {
170171
throw new NullPointerException("list[" + i + "] is null");
171172
} else if (seenLocales.contains(l)) {
172-
throw new IllegalArgumentException("list[" + i + "] is a repetition");
173+
// Dropping duplicated locale entries.
173174
} else {
174175
final Locale localeClone = (Locale) l.clone();
175-
localeList[i] = localeClone;
176+
localeList.add(localeClone);
176177
sb.append(localeClone.toLanguageTag());
177178
if (i < list.length - 1) {
178179
sb.append(',');
179180
}
180181
seenLocales.add(localeClone);
181182
}
182183
}
183-
mList = localeList;
184+
mList = localeList.toArray(new Locale[localeList.size()]);
184185
mStringRepresentation = sb.toString();
185186
}
186187
}

0 commit comments

Comments
 (0)