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

Commit 7a712372 authored by Yohei Yukawa's avatar Yohei Yukawa
Browse files

Replace HashMap with ArrayMap in LocaleUtils

LocaleUtils#filterByLanguage() always perform sort before returning
the result hence enumeration order of HashMap/ArrayMap does not affect
the final result.  Hence we should be able to safely replace HashMap
with ArrayMap.

Also, ArrayMap#valueAt() allows us to copy values into an array
without iterator object.

Bug: 119839847
Test: atest FrameworksServicesTests:com.android.server.inputmethod.LocaleUtilsTest
Change-Id: I898ffcf18110538cbb777e85df21c815e8ed030c
parent 9b89a0bd
Loading
Loading
Loading
Loading
+7 −3
Original line number Original line Diff line number Diff line
@@ -22,10 +22,10 @@ import android.annotation.Nullable;
import android.icu.util.ULocale;
import android.icu.util.ULocale;
import android.os.LocaleList;
import android.os.LocaleList;
import android.text.TextUtils;
import android.text.TextUtils;
import android.util.ArrayMap;


import java.util.ArrayList;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.List;
import java.util.Locale;
import java.util.Locale;


@@ -155,7 +155,7 @@ final class LocaleUtils {
        }
        }


        final int numPreferredLocales = preferredLocales.size();
        final int numPreferredLocales = preferredLocales.size();
        final HashMap<String, ScoreEntry> scoreboard = new HashMap<>();
        final ArrayMap<String, ScoreEntry> scoreboard = new ArrayMap<>();
        final byte[] score = new byte[numPreferredLocales];
        final byte[] score = new byte[numPreferredLocales];
        final ULocale[] preferredULocaleCache = new ULocale[numPreferredLocales];
        final ULocale[] preferredULocaleCache = new ULocale[numPreferredLocales];


@@ -197,7 +197,11 @@ final class LocaleUtils {
            }
            }
        }
        }


        final ScoreEntry[] result = scoreboard.values().toArray(new ScoreEntry[scoreboard.size()]);
        final int numEntries = scoreboard.size();
        final ScoreEntry[] result = new ScoreEntry[numEntries];
        for (int i = 0; i < numEntries; ++i) {
            result[i] = scoreboard.valueAt(i);
        }
        Arrays.sort(result);
        Arrays.sort(result);
        for (final ScoreEntry entry : result) {
        for (final ScoreEntry entry : result) {
            dest.add(sources.get(entry.mIndex));
            dest.add(sources.get(entry.mIndex));