Merge "Generalize SparseArrayMap."

This commit is contained in:
Kweku Adams
2020-09-16 20:17:45 +00:00
committed by Android (Google) Code Review
5 changed files with 45 additions and 41 deletions

View File

@@ -296,13 +296,14 @@ public final class QuotaController extends StateController {
}
/** List of all tracked jobs keyed by source package-userId combo. */
private final SparseArrayMap<ArraySet<JobStatus>> mTrackedJobs = new SparseArrayMap<>();
private final SparseArrayMap<String, ArraySet<JobStatus>> mTrackedJobs = new SparseArrayMap<>();
/** Timer for each package-userId combo. */
private final SparseArrayMap<Timer> mPkgTimers = new SparseArrayMap<>();
private final SparseArrayMap<String, Timer> mPkgTimers = new SparseArrayMap<>();
/** List of all timing sessions for a package-userId combo, in chronological order. */
private final SparseArrayMap<List<TimingSession>> mTimingSessions = new SparseArrayMap<>();
private final SparseArrayMap<String, List<TimingSession>> mTimingSessions =
new SparseArrayMap<>();
/**
* Listener to track and manage when each package comes back within quota.
@@ -311,7 +312,8 @@ public final class QuotaController extends StateController {
private final InQuotaAlarmListener mInQuotaAlarmListener = new InQuotaAlarmListener();
/** Cached calculation results for each app, with the standby buckets as the array indices. */
private final SparseArrayMap<ExecutionStats[]> mExecutionStatsCache = new SparseArrayMap<>();
private final SparseArrayMap<String, ExecutionStats[]> mExecutionStatsCache =
new SparseArrayMap<>();
/** List of UIDs currently in the foreground. */
private final SparseBooleanArray mForegroundUids = new SparseBooleanArray();
@@ -1225,7 +1227,8 @@ public final class QuotaController extends StateController {
}
private class UidConstraintUpdater implements Consumer<JobStatus> {
private final SparseArrayMap<Integer> mToScheduleStartAlarms = new SparseArrayMap<>();
private final SparseArrayMap<String, Integer> mToScheduleStartAlarms =
new SparseArrayMap<>();
public boolean wasJobChanged;
@Override

View File

@@ -5089,23 +5089,23 @@ package android.util {
field public static final String SETTINGS_WIFITRACKER2 = "settings_wifitracker2";
}
public class SparseArrayMap<T> {
public class SparseArrayMap<K, V> {
ctor public SparseArrayMap();
method public void add(int, @NonNull String, @Nullable T);
method public void add(int, @NonNull K, @Nullable V);
method public void clear();
method public boolean contains(int, @NonNull String);
method public boolean contains(int, @NonNull K);
method public void delete(int);
method @Nullable public T delete(int, @NonNull String);
method public void forEach(@NonNull java.util.function.Consumer<T>);
method @Nullable public T get(int, @NonNull String);
method @Nullable public T getOrDefault(int, @NonNull String, T);
method @Nullable public V delete(int, @NonNull K);
method public void forEach(@NonNull java.util.function.Consumer<V>);
method @Nullable public V get(int, @NonNull K);
method @Nullable public V getOrDefault(int, @NonNull K, V);
method public int indexOfKey(int);
method public int indexOfKey(int, @NonNull String);
method public int indexOfKey(int, @NonNull K);
method public int keyAt(int);
method @NonNull public String keyAt(int, int);
method @NonNull public K keyAt(int, int);
method public int numElementsForKey(int);
method public int numMaps();
method @Nullable public T valueAt(int, int);
method @Nullable public V valueAt(int, int);
}
public class TimeUtils {

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));
}

View File

@@ -95,7 +95,7 @@ abstract class QuotaTracker {
/** "Free quota status" for apps. */
@GuardedBy("mLock")
private final SparseArrayMap<Boolean> mFreeQuota = new SparseArrayMap<>();
private final SparseArrayMap<String, Boolean> mFreeQuota = new SparseArrayMap<>();
private final AlarmManager mAlarmManager;
protected final Context mContext;

View File

@@ -31,7 +31,7 @@ import java.util.function.Function;
* @see Uptc
*/
class UptcMap<T> {
private final SparseArrayMap<ArrayMap<String, T>> mData = new SparseArrayMap<>();
private final SparseArrayMap<String, ArrayMap<String, T>> mData = new SparseArrayMap<>();
public void add(int userId, @NonNull String packageName, @Nullable String tag,
@Nullable T obj) {