From 590e9ffb4cb6f1b065ef943030a01e11ddce6f66 Mon Sep 17 00:00:00 2001 From: Winson Date: Mon, 11 Jan 2021 17:06:18 -0800 Subject: Add SparseArray#set for Kotlin index= operator Just an alias for put to allow `array[index] = value` syntax in Kotlin. Bug: 163565712 Test: manual, used in tests for separate feature Change-Id: Ie9e17b8f4d16a52f4dc213e1ada8d61fce9eedaf --- core/java/android/util/SparseArray.java | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'core/java/android/util/SparseArray.java') diff --git a/core/java/android/util/SparseArray.java b/core/java/android/util/SparseArray.java index dae760f989f6..86120d1e650c 100644 --- a/core/java/android/util/SparseArray.java +++ b/core/java/android/util/SparseArray.java @@ -240,6 +240,14 @@ public class SparseArray implements Cloneable { // Log.e("SparseArray", "gc end with " + mSize); } + /** + * Alias for {@link #put(int, Object)} to support Kotlin [index]= operator. + * @see #put(int, Object) + */ + public void set(int key, E value) { + put(key, value); + } + /** * Adds a mapping from the specified key to the specified value, * replacing the previous mapping from the specified key if there -- cgit v1.2.3 From e803a1bd33a866fff132c2dfdb39e35d609a611e Mon Sep 17 00:00:00 2001 From: Winson Date: Tue, 15 Dec 2020 14:02:11 -0800 Subject: 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 --- core/java/android/util/SparseArray.java | 43 +++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) (limited to 'core/java/android/util/SparseArray.java') 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; + /** * SparseArray maps integers to Objects and, unlike a normal array of Objects, * its indices can contain gaps. SparseArray is intended to be more memory-efficient @@ -505,4 +508,44 @@ public class SparseArray 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 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; + } } -- cgit v1.2.3 From 84bf25d6d3bcb52ed15bd21403dd4d52f3dce34a Mon Sep 17 00:00:00 2001 From: Winson Date: Wed, 24 Feb 2021 11:57:46 -0800 Subject: Accept generic in SparseArray contentEquals Also updates the JavaDoc. Bug: 181100924 Test: atest android.util.cts.SparseArrayTest Change-Id: I3704b2519d10e20d02437037c7f92f5d2ed90dcd --- core/java/android/util/SparseArray.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'core/java/android/util/SparseArray.java') diff --git a/core/java/android/util/SparseArray.java b/core/java/android/util/SparseArray.java index 6718e93f908c..05c8617294da 100644 --- a/core/java/android/util/SparseArray.java +++ b/core/java/android/util/SparseArray.java @@ -510,10 +510,12 @@ public class SparseArray implements Cloneable { } /** + * Compares the contents of this {@link SparseArray} to the specified {@link SparseArray}. + * * For backwards compatibility reasons, {@link Object#equals(Object)} cannot be implemented, * so this serves as a manually invoked alternative. */ - public boolean contentEquals(@Nullable SparseArray other) { + public boolean contentEquals(@Nullable SparseArray other) { if (other == null) { return false; } @@ -534,6 +536,9 @@ public class SparseArray implements Cloneable { } /** + * Returns a hash code value for the contents of this {@link SparseArray}, combining the + * {@link Objects#hashCode(Object)} result of all its keys and values. + * * For backwards compatibility, {@link Object#hashCode()} cannot be implemented, so this serves * as a manually invoked alternative. */ -- cgit v1.2.3