|
24 | 24 |
|
25 | 25 | import com.android.internal.annotations.GuardedBy; |
26 | 26 |
|
| 27 | +import java.util.ArrayList; |
27 | 28 | import java.util.Arrays; |
28 | 29 | import java.util.Collection; |
29 | 30 | import java.util.HashSet; |
@@ -150,37 +151,37 @@ public String toLanguageTags() { |
150 | 151 | /** |
151 | 152 | * Creates a new {@link LocaleList}. |
152 | 153 | * |
| 154 | + * If two or more same locales are passed, the repeated locales will be dropped. |
153 | 155 | * <p>For empty lists of {@link Locale} items it is better to use {@link #getEmptyLocaleList()}, |
154 | 156 | * which returns a pre-constructed empty list.</p> |
155 | 157 | * |
156 | 158 | * @throws NullPointerException if any of the input locales is <code>null</code>. |
157 | | - * @throws IllegalArgumentException if any of the input locales repeat. |
158 | 159 | */ |
159 | 160 | public LocaleList(@NonNull Locale... list) { |
160 | 161 | if (list.length == 0) { |
161 | 162 | mList = sEmptyList; |
162 | 163 | mStringRepresentation = ""; |
163 | 164 | } else { |
164 | | - final Locale[] localeList = new Locale[list.length]; |
| 165 | + final ArrayList<Locale> localeList = new ArrayList<>(); |
165 | 166 | final HashSet<Locale> seenLocales = new HashSet<Locale>(); |
166 | 167 | final StringBuilder sb = new StringBuilder(); |
167 | 168 | for (int i = 0; i < list.length; i++) { |
168 | 169 | final Locale l = list[i]; |
169 | 170 | if (l == null) { |
170 | 171 | throw new NullPointerException("list[" + i + "] is null"); |
171 | 172 | } else if (seenLocales.contains(l)) { |
172 | | - throw new IllegalArgumentException("list[" + i + "] is a repetition"); |
| 173 | + // Dropping duplicated locale entries. |
173 | 174 | } else { |
174 | 175 | final Locale localeClone = (Locale) l.clone(); |
175 | | - localeList[i] = localeClone; |
| 176 | + localeList.add(localeClone); |
176 | 177 | sb.append(localeClone.toLanguageTag()); |
177 | 178 | if (i < list.length - 1) { |
178 | 179 | sb.append(','); |
179 | 180 | } |
180 | 181 | seenLocales.add(localeClone); |
181 | 182 | } |
182 | 183 | } |
183 | | - mList = localeList; |
| 184 | + mList = localeList.toArray(new Locale[localeList.size()]); |
184 | 185 | mStringRepresentation = sb.toString(); |
185 | 186 | } |
186 | 187 | } |
|
0 commit comments