From 0c22b1da5e27400a30fe0fff4e293cf867120ac0 Mon Sep 17 00:00:00 2001 From: Kweku Adams Date: Wed, 16 Sep 2020 10:07:55 -0700 Subject: Generalize SparseArrayMap. SparseArrayMap required Strings as keys, which restricted its uses. Now it can take any object as a key, which better reflects the underlying ArrayMap generalization. Bug: 138469672 Test: atest com.android.server.job.controllers.QuotaControllerTest Test: atest QuotaTrackerTest Test: atest SparseArrayMapTest Change-Id: If6da7dfdf3cdd9be0fe9006484c78c574ef2028b --- core/java/android/util/SparseArrayMap.java | 49 +++++++++++++++--------------- 1 file changed, 25 insertions(+), 24 deletions(-) (limited to 'core/java/android/util/SparseArrayMap.java') diff --git a/core/java/android/util/SparseArrayMap.java b/core/java/android/util/SparseArrayMap.java index 3ec6b810fda8..3287c279c87f 100644 --- a/core/java/android/util/SparseArrayMap.java +++ b/core/java/android/util/SparseArrayMap.java @@ -26,16 +26,17 @@ import java.util.function.Consumer; * A sparse array of ArrayMaps, which is suitable for holding (userId, packageName)->object * associations. * - * @param Any class + * @param Any class + * @param Any class * @hide */ @TestApi -public class SparseArrayMap { - private final SparseArray> mData = new SparseArray<>(); +public class SparseArrayMap { + private final SparseArray> mData = new SparseArray<>(); - /** Add an entry associating obj with the int-String pair. */ - public void add(int key, @NonNull String mapKey, @Nullable T obj) { - ArrayMap data = mData.get(key); + /** Add an entry associating obj with the int-K pair. */ + public void add(int key, @NonNull K mapKey, @Nullable V obj) { + ArrayMap data = mData.get(key); if (data == null) { data = new ArrayMap<>(); mData.put(key, data); @@ -50,8 +51,8 @@ public class SparseArrayMap { } } - /** Return true if the structure contains an explicit entry for the int-String pair. */ - public boolean contains(int key, @NonNull String mapKey) { + /** Return true if the structure contains an explicit entry for the int-K pair. */ + public boolean contains(int key, @NonNull K mapKey) { return mData.contains(key) && mData.get(key).containsKey(mapKey); } @@ -66,8 +67,8 @@ public class SparseArrayMap { * @return Returns the value that was stored under the keys, or null if there was none. */ @Nullable - public T delete(int key, @NonNull String mapKey) { - ArrayMap data = mData.get(key); + public V delete(int key, @NonNull K mapKey) { + ArrayMap data = mData.get(key); if (data != null) { return data.remove(mapKey); } @@ -75,11 +76,11 @@ public class SparseArrayMap { } /** - * Get the value associated with the int-String pair. + * Get the value associated with the int-K pair. */ @Nullable - public T get(int key, @NonNull String mapKey) { - ArrayMap data = mData.get(key); + public V get(int key, @NonNull K mapKey) { + ArrayMap data = mData.get(key); if (data != null) { return data.get(mapKey); } @@ -91,9 +92,9 @@ public class SparseArrayMap { * map contains no mapping for them. */ @Nullable - public T getOrDefault(int key, @NonNull String mapKey, T defaultValue) { + public V getOrDefault(int key, @NonNull K mapKey, V defaultValue) { if (mData.contains(key)) { - ArrayMap data = mData.get(key); + ArrayMap data = mData.get(key); if (data != null && data.containsKey(mapKey)) { return data.get(mapKey); } @@ -111,8 +112,8 @@ public class SparseArrayMap { * * @see SparseArray#indexOfKey */ - public int indexOfKey(int key, @NonNull String mapKey) { - ArrayMap data = mData.get(key); + public int indexOfKey(int key, @NonNull K mapKey) { + ArrayMap data = mData.get(key); if (data != null) { return data.indexOfKey(mapKey); } @@ -126,7 +127,7 @@ public class SparseArrayMap { /** Returns the map's key at the given mapIndex for the given keyIndex. */ @NonNull - public String keyAt(int keyIndex, int mapIndex) { + public K keyAt(int keyIndex, int mapIndex) { return mData.valueAt(keyIndex).keyAt(mapIndex); } @@ -137,20 +138,20 @@ public class SparseArrayMap { /** Returns the number of elements in the map of the given key. */ public int numElementsForKey(int key) { - ArrayMap data = mData.get(key); + ArrayMap data = mData.get(key); return data == null ? 0 : data.size(); } - /** Returns the value T at the given key and map index. */ + /** Returns the value V at the given key and map index. */ @Nullable - public T valueAt(int keyIndex, int mapIndex) { + public V valueAt(int keyIndex, int mapIndex) { return mData.valueAt(keyIndex).valueAt(mapIndex); } - /** Iterate through all int-String pairs and operate on all of the values. */ - public void forEach(@NonNull Consumer consumer) { + /** Iterate through all int-K pairs and operate on all of the values. */ + public void forEach(@NonNull Consumer consumer) { for (int i = numMaps() - 1; i >= 0; --i) { - ArrayMap data = mData.valueAt(i); + ArrayMap data = mData.valueAt(i); for (int j = data.size() - 1; j >= 0; --j) { consumer.accept(data.valueAt(j)); } -- cgit v1.2.3