summaryrefslogtreecommitdiffstats
path: root/core/java/android/util
diff options
context:
space:
mode:
authorChet Haase <chet@google.com>2013-06-06 16:34:33 -0700
committerChet Haase <chet@google.com>2013-06-06 16:43:41 -0700
commitf4130cf35fa128e36f96e55955d4f5db86197e4a (patch)
tree0ae8a2c211bebc906d67402e3aa7e0c5b801babe /core/java/android/util
parentae8f82f2015612a659b95007c355d12c83fbcefc (diff)
downloadframeworks_base-f4130cf35fa128e36f96e55955d4f5db86197e4a.zip
frameworks_base-f4130cf35fa128e36f96e55955d4f5db86197e4a.tar.gz
frameworks_base-f4130cf35fa128e36f96e55955d4f5db86197e4a.tar.bz2
Additional functionality and performance for ArrayMap
Added equals() and hashCode() to ArrayMap to allow equals() tests of maps with the same key/value pairs to return true. Changed putAll() to handle the case of an empty map faster, just copying the arrays instead of adding elements one by one. Added to ArrayMapTests to test new equals() and copy constructor functionality. Issue #9299310 Optimize ArrayMap copy constructor Change-Id: I1186a0eddd1fd53a0f380c2f3972fc1942cdf879
Diffstat (limited to 'core/java/android/util')
-rw-r--r--core/java/android/util/ArrayMap.java76
1 files changed, 72 insertions, 4 deletions
diff --git a/core/java/android/util/ArrayMap.java b/core/java/android/util/ArrayMap.java
index edda779..bb0a6e1 100644
--- a/core/java/android/util/ArrayMap.java
+++ b/core/java/android/util/ArrayMap.java
@@ -400,8 +400,16 @@ public final class ArrayMap<K, V> implements Map<K, V> {
public void putAll(ArrayMap<? extends K, ? extends V> array) {
final int N = array.mSize;
ensureCapacity(mSize + N);
- for (int i=0; i<N; i++) {
- put(array.keyAt(i), array.valueAt(i));
+ if (mSize == 0) {
+ if (N > 0) {
+ System.arraycopy(array.mHashes, 0, mHashes, 0, N);
+ System.arraycopy(array.mArray, 0, mArray, 0, N<<1);
+ mSize = N;
+ }
+ } else {
+ for (int i=0; i<N; i++) {
+ put(array.keyAt(i), array.valueAt(i));
+ }
}
}
@@ -605,7 +613,7 @@ public final class ArrayMap<K, V> implements Map<K, V> {
* Return a {@link java.util.Set} for iterating over and interacting with all keys
* in the array map.
*
- * <p><b>Note:</b> this is a fair inefficient way to access the array contents, it
+ * <p><b>Note:</b> this is a fairly inefficient way to access the array contents, it
* requires generating a number of temporary objects.</p>
*/
@Override
@@ -617,11 +625,71 @@ public final class ArrayMap<K, V> implements Map<K, V> {
* Return a {@link java.util.Collection} for iterating over and interacting with all values
* in the array map.
*
- * <p><b>Note:</b> this is a fair inefficient way to access the array contents, it
+ * <p><b>Note:</b> this is a fairly inefficient way to access the array contents, it
* requires generating a number of temporary objects.</p>
*/
@Override
public Collection<V> values() {
return getCollection().getValues();
}
+
+ /**
+ * {@inheritDoc}
+ *
+ * <p>This implementation returns false if the object is not a map, or
+ * if the maps have different sizes. Otherwise, for each key in this map,
+ * values of both maps are compared. If the values for any key are not
+ * equal, the method returns false, otherwise it returns true.
+ */
+ @Override
+ public boolean equals(Object object) {
+ if (this == object) {
+ return true;
+ }
+ if (object instanceof Map) {
+ Map<?, ?> map = (Map<?, ?>) object;
+ if (size() != map.size()) {
+ return false;
+ }
+
+ try {
+ for (int i=0; i<mSize; i++) {
+ K key = keyAt(i);
+ V mine = valueAt(i);
+ Object theirs = map.get(key);
+ if (mine == null) {
+ if (theirs != null || !map.containsKey(key)) {
+ return false;
+ }
+ } else if (!mine.equals(theirs)) {
+ return false;
+ }
+ }
+ } catch (NullPointerException ignored) {
+ return false;
+ } catch (ClassCastException ignored) {
+ return false;
+ }
+ return true;
+ }
+ return false;
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * <p>This implementation sums a hashcode using all keys and values.
+ */
+ @Override
+ public int hashCode() {
+ int result = 0;
+ for (int i=0; i<mSize; i++) {
+ K key = keyAt(i);
+ V value = valueAt(i);
+ result += (key == null ? 0 : key.hashCode())
+ ^ (value == null ? 0 : value.hashCode());
+ }
+ return result;
+ }
+
}