summaryrefslogtreecommitdiff
path: root/core/java/android/util/SparseArray.java
diff options
context:
space:
mode:
authorWinson <chiuwinson@google.com>2020-12-15 14:02:11 -0800
committerWinson <chiuwinson@google.com>2021-02-04 10:26:57 -0800
commite803a1bd33a866fff132c2dfdb39e35d609a611e (patch)
treeae0901f2624442465a42b0fc8775e8ad67622153 /core/java/android/util/SparseArray.java
parent2641e601cc0bebc09336a83974f121b4bfc08ae2 (diff)
Add internal domain verification data classes
Eventually will be used to store state that's included as part of com.android.server.pm.Settings. Also adds equality and Kotlin index operator mutation support to SparseArray, to improve ease of use. Exempt-From-Owner-Approval: Already approved by owners on main branch Bug: 163565712 CTS-Coverage-Bug: 179382047 Test: none, will be tested as part of follow up change Change-Id: Ie4eca3a99633465337758ee165e07f35c8db87c8
Diffstat (limited to 'core/java/android/util/SparseArray.java')
-rw-r--r--core/java/android/util/SparseArray.java43
1 files changed, 43 insertions, 0 deletions
diff --git a/core/java/android/util/SparseArray.java b/core/java/android/util/SparseArray.java
index 86120d1e650c..6718e93f908c 100644
--- a/core/java/android/util/SparseArray.java
+++ b/core/java/android/util/SparseArray.java
@@ -16,6 +16,7 @@
package android.util;
+import android.annotation.Nullable;
import android.compat.annotation.UnsupportedAppUsage;
import com.android.internal.util.ArrayUtils;
@@ -23,6 +24,8 @@ import com.android.internal.util.GrowingArrayUtils;
import libcore.util.EmptyArray;
+import java.util.Objects;
+
/**
* <code>SparseArray</code> maps integers to Objects and, unlike a normal array of Objects,
* its indices can contain gaps. <code>SparseArray</code> is intended to be more memory-efficient
@@ -505,4 +508,44 @@ public class SparseArray<E> implements Cloneable {
buffer.append('}');
return buffer.toString();
}
+
+ /**
+ * For backwards compatibility reasons, {@link Object#equals(Object)} cannot be implemented,
+ * so this serves as a manually invoked alternative.
+ */
+ public boolean contentEquals(@Nullable SparseArray<E> other) {
+ if (other == null) {
+ return false;
+ }
+
+ int size = size();
+ if (size != other.size()) {
+ return false;
+ }
+
+ for (int index = 0; index < size; index++) {
+ int key = keyAt(index);
+ if (!Objects.equals(valueAt(index), other.get(key))) {
+ return false;
+ }
+ }
+
+ return true;
+ }
+
+ /**
+ * For backwards compatibility, {@link Object#hashCode()} cannot be implemented, so this serves
+ * as a manually invoked alternative.
+ */
+ public int contentHashCode() {
+ int hash = 0;
+ int size = size();
+ for (int index = 0; index < size; index++) {
+ int key = keyAt(index);
+ E value = valueAt(index);
+ hash = 31 * hash + Objects.hashCode(key);
+ hash = 31 * hash + Objects.hashCode(value);
+ }
+ return hash;
+ }
}