Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit fc8c211b authored by Seigo Nonaka's avatar Seigo Nonaka
Browse files

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
parent 0c0b75fb
Loading
Loading
Loading
Loading
+6 −5
Original line number Original line Diff line number Diff line
@@ -25,6 +25,7 @@ import android.icu.util.ULocale;


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


import java.util.ArrayList;
import java.util.Arrays;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collection;
import java.util.HashSet;
import java.util.HashSet;
@@ -151,18 +152,18 @@ public final class LocaleList implements Parcelable {
    /**
    /**
     * Creates a new {@link LocaleList}.
     * Creates a new {@link LocaleList}.
     *
     *
     * If two or more same locales are passed, the repeated locales will be dropped.
     * <p>For empty lists of {@link Locale} items it is better to use {@link #getEmptyLocaleList()},
     * <p>For empty lists of {@link Locale} items it is better to use {@link #getEmptyLocaleList()},
     * which returns a pre-constructed empty list.</p>
     * which returns a pre-constructed empty list.</p>
     *
     *
     * @throws NullPointerException if any of the input locales is <code>null</code>.
     * @throws NullPointerException if any of the input locales is <code>null</code>.
     * @throws IllegalArgumentException if any of the input locales repeat.
     */
     */
    public LocaleList(@NonNull Locale... list) {
    public LocaleList(@NonNull Locale... list) {
        if (list.length == 0) {
        if (list.length == 0) {
            mList = sEmptyList;
            mList = sEmptyList;
            mStringRepresentation = "";
            mStringRepresentation = "";
        } else {
        } else {
            final Locale[] localeList = new Locale[list.length];
            final ArrayList<Locale> localeList = new ArrayList<>();
            final HashSet<Locale> seenLocales = new HashSet<Locale>();
            final HashSet<Locale> seenLocales = new HashSet<Locale>();
            final StringBuilder sb = new StringBuilder();
            final StringBuilder sb = new StringBuilder();
            for (int i = 0; i < list.length; i++) {
            for (int i = 0; i < list.length; i++) {
@@ -170,10 +171,10 @@ public final class LocaleList implements Parcelable {
                if (l == null) {
                if (l == null) {
                    throw new NullPointerException("list[" + i + "] is null");
                    throw new NullPointerException("list[" + i + "] is null");
                } else if (seenLocales.contains(l)) {
                } else if (seenLocales.contains(l)) {
                    throw new IllegalArgumentException("list[" + i + "] is a repetition");
                    // Dropping duplicated locale entries.
                } else {
                } else {
                    final Locale localeClone = (Locale) l.clone();
                    final Locale localeClone = (Locale) l.clone();
                    localeList[i] = localeClone;
                    localeList.add(localeClone);
                    sb.append(localeClone.toLanguageTag());
                    sb.append(localeClone.toLanguageTag());
                    if (i < list.length - 1) {
                    if (i < list.length - 1) {
                        sb.append(',');
                        sb.append(',');
@@ -181,7 +182,7 @@ public final class LocaleList implements Parcelable {
                    seenLocales.add(localeClone);
                    seenLocales.add(localeClone);
                }
                }
            }
            }
            mList = localeList;
            mList = localeList.toArray(new Locale[localeList.size()]);
            mStringRepresentation = sb.toString();
            mStringRepresentation = sb.toString();
        }
        }
    }
    }