summaryrefslogtreecommitdiff
path: root/core/java/android/util/LongSparseArray.java
diff options
context:
space:
mode:
authorTejas Khorana <khorana@google.com>2016-07-18 11:18:27 -0700
committerTejas Khorana <khorana@google.com>2016-07-18 12:03:14 -0700
commitfb5166dafb599de0e2d9ffb9684fd1f90e5e1830 (patch)
treec14e41ecb55941f255d239500698c94cdb7ace2c /core/java/android/util/LongSparseArray.java
parentde8ca1697a4036c82b89b45adb258a7cf365677b (diff)
indexOfValue now uses .equals (cont.)
Addresses same issue addressed in SparseArray in LongSparseArray in the method: indexByValue. Made a new method indexByValueByValue that compares objects using .equals instead of ==. Change-Id: I55735fe7ca364d0a9caab2a6909c2eaede845619
Diffstat (limited to 'core/java/android/util/LongSparseArray.java')
-rw-r--r--core/java/android/util/LongSparseArray.java33
1 files changed, 31 insertions, 2 deletions
diff --git a/core/java/android/util/LongSparseArray.java b/core/java/android/util/LongSparseArray.java
index 6b45ff484e67..58ad2fd0c586 100644
--- a/core/java/android/util/LongSparseArray.java
+++ b/core/java/android/util/LongSparseArray.java
@@ -299,10 +299,39 @@ public class LongSparseArray<E> implements Cloneable {
gc();
}
- for (int i = 0; i < mSize; i++)
- if (mValues[i] == value)
+ for (int i = 0; i < mSize; i++) {
+ if (mValues[i] == value) {
return i;
+ }
+ }
+ return -1;
+ }
+
+ /**
+ * Returns an index for which {@link #valueAt} would return the
+ * specified key, or a negative number if no keys map to the
+ * specified value.
+ * <p>Beware that this is a linear search, unlike lookups by key,
+ * and that multiple keys can map to the same value and this will
+ * find only one of them.
+ * <p>Note also that this method uses {@code equals} unlike {@code indexOfValue}.
+ */
+ public int indexOfValueByValue(E value) {
+ if (mGarbage) {
+ gc();
+ }
+ for (int i = 0; i < mSize; i++) {
+ if (value == null) {
+ if (mValues[i] == null) {
+ return i;
+ }
+ } else {
+ if (value.equals(mValues[i])) {
+ return i;
+ }
+ }
+ }
return -1;
}