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
This commit is contained in:
Kweku Adams
2020-09-16 10:07:55 -07:00
parent 609abbbd5c
commit 0c22b1da5e
5 changed files with 45 additions and 41 deletions

View File

@@ -26,16 +26,17 @@ import java.util.function.Consumer;
* A sparse array of ArrayMaps, which is suitable for holding (userId, packageName)->object
* associations.
*
* @param <T> Any class
* @param <K> Any class
* @param <V> Any class
* @hide
*/
@TestApi
public class SparseArrayMap<T> {
private final SparseArray<ArrayMap<String, T>> mData = new SparseArray<>();
public class SparseArrayMap<K, V> {
private final SparseArray<ArrayMap<K, V>> 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<String, T> 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<K, V> data = mData.get(key);
if (data == null) {
data = new ArrayMap<>();
mData.put(key, data);
@@ -50,8 +51,8 @@ public class SparseArrayMap<T> {
}
}
/** 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<T> {
* @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<String, T> data = mData.get(key);
public V delete(int key, @NonNull K mapKey) {
ArrayMap<K, V> data = mData.get(key);
if (data != null) {
return data.remove(mapKey);
}
@@ -75,11 +76,11 @@ public class SparseArrayMap<T> {
}
/**
* 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<String, T> data = mData.get(key);
public V get(int key, @NonNull K mapKey) {
ArrayMap<K, V> data = mData.get(key);
if (data != null) {
return data.get(mapKey);
}
@@ -91,9 +92,9 @@ public class SparseArrayMap<T> {
* 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<String, T> data = mData.get(key);
ArrayMap<K, V> data = mData.get(key);
if (data != null && data.containsKey(mapKey)) {
return data.get(mapKey);
}
@@ -111,8 +112,8 @@ public class SparseArrayMap<T> {
*
* @see SparseArray#indexOfKey
*/
public int indexOfKey(int key, @NonNull String mapKey) {
ArrayMap<String, T> data = mData.get(key);
public int indexOfKey(int key, @NonNull K mapKey) {
ArrayMap<K, V> data = mData.get(key);
if (data != null) {
return data.indexOfKey(mapKey);
}
@@ -126,7 +127,7 @@ public class SparseArrayMap<T> {
/** 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<T> {
/** Returns the number of elements in the map of the given key. */
public int numElementsForKey(int key) {
ArrayMap<String, T> data = mData.get(key);
ArrayMap<K, V> 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<T> consumer) {
/** Iterate through all int-K pairs and operate on all of the values. */
public void forEach(@NonNull Consumer<V> consumer) {
for (int i = numMaps() - 1; i >= 0; --i) {
ArrayMap<String, T> data = mData.valueAt(i);
ArrayMap<K, V> data = mData.valueAt(i);
for (int j = data.size() - 1; j >= 0; --j) {
consumer.accept(data.valueAt(j));
}