diff options
| -rw-r--r-- | core/java/android/util/MapCollections.java | 73 | ||||
| -rw-r--r-- | tests/ActivityTests/src/com/google/android/test/activity/ArrayMapTests.java | 59 |
2 files changed, 127 insertions, 5 deletions
diff --git a/core/java/android/util/MapCollections.java b/core/java/android/util/MapCollections.java index f29fb65..09f1f8e 100644 --- a/core/java/android/util/MapCollections.java +++ b/core/java/android/util/MapCollections.java @@ -183,13 +183,27 @@ abstract class MapCollections<K, V> { } @Override - public boolean contains(Object object) { - throw new UnsupportedOperationException(); + public boolean contains(Object o) { + if (!(o instanceof Map.Entry)) + return false; + Map.Entry<?, ?> e = (Map.Entry<?, ?>) o; + int index = colIndexOfKey(e.getKey()); + if (index < 0) { + return false; + } + Object foundVal = colGetEntry(index, 1); + return Objects.equal(foundVal, e.getValue()); } @Override public boolean containsAll(Collection<?> collection) { - throw new UnsupportedOperationException(); + Iterator<?> it = collection.iterator(); + while (it.hasNext()) { + if (!contains(it.next())) { + return false; + } + } + return true; } @Override @@ -231,6 +245,23 @@ abstract class MapCollections<K, V> { public <T> T[] toArray(T[] array) { throw new UnsupportedOperationException(); } + + @Override + public boolean equals(Object object) { + return equalsSetHelper(this, object); + } + + @Override + public int hashCode() { + int result = 0; + for (int i=colGetSize()-1; i>=0; i--) { + final Object key = colGetEntry(i, 0); + final Object value = colGetEntry(i, 1); + result += ( (key == null ? 0 : key.hashCode()) ^ + (value == null ? 0 : value.hashCode()) ); + } + return result; + } }; final class KeySet implements Set<K> { @@ -257,7 +288,7 @@ abstract class MapCollections<K, V> { @Override public boolean containsAll(Collection<?> collection) { - return removeAllHelper(colGetMap(), collection); + return containsAllHelper(colGetMap(), collection); } @Override @@ -304,6 +335,21 @@ abstract class MapCollections<K, V> { public <T> T[] toArray(T[] array) { return toArrayHelper(array, 1); } + + @Override + public boolean equals(Object object) { + return equalsSetHelper(this, object); + } + + @Override + public int hashCode() { + int result = 0; + for (int i=colGetSize()-1; i>=0; i--) { + Object obj = colGetEntry(i, 0); + result += obj == null ? 0 : obj.hashCode(); + } + return result; + } }; final class ValuesCollection implements Collection<V> { @@ -437,7 +483,6 @@ abstract class MapCollections<K, V> { return oldSize != map.size(); } - public Object[] toArrayHelper(int offset) { final int N = colGetSize(); Object[] result = new Object[N]; @@ -463,6 +508,24 @@ abstract class MapCollections<K, V> { return array; } + public static <T> boolean equalsSetHelper(Set<T> set, Object object) { + if (set == object) { + return true; + } + if (object instanceof Set) { + Set<?> s = (Set<?>) object; + + try { + return set.size() == s.size() && set.containsAll(s); + } catch (NullPointerException ignored) { + return false; + } catch (ClassCastException ignored) { + return false; + } + } + return false; + } + public Set<Map.Entry<K, V>> getEntrySet() { if (mEntrySet == null) { mEntrySet = new EntrySet(); diff --git a/tests/ActivityTests/src/com/google/android/test/activity/ArrayMapTests.java b/tests/ActivityTests/src/com/google/android/test/activity/ArrayMapTests.java index 28e86bf..4ad6dc7 100644 --- a/tests/ActivityTests/src/com/google/android/test/activity/ArrayMapTests.java +++ b/tests/ActivityTests/src/com/google/android/test/activity/ArrayMapTests.java @@ -146,6 +146,65 @@ public class ArrayMapTests { } } + if (map.entrySet().hashCode() != array.entrySet().hashCode()) { + Log.e("test", "Entry set hash codes differ: map=0x" + + Integer.toHexString(map.entrySet().hashCode()) + " array=0x" + + Integer.toHexString(array.entrySet().hashCode())); + return false; + } + + if (!map.entrySet().equals(array.entrySet())) { + Log.e("test", "Failed calling equals on map entry set against array set"); + return false; + } + + if (!array.entrySet().equals(map.entrySet())) { + Log.e("test", "Failed calling equals on array entry set against map set"); + return false; + } + + if (map.keySet().hashCode() != array.keySet().hashCode()) { + Log.e("test", "Key set hash codes differ: map=0x" + + Integer.toHexString(map.keySet().hashCode()) + " array=0x" + + Integer.toHexString(array.keySet().hashCode())); + return false; + } + + if (!map.keySet().equals(array.keySet())) { + Log.e("test", "Failed calling equals on map key set against array set"); + return false; + } + + if (!array.keySet().equals(map.keySet())) { + Log.e("test", "Failed calling equals on array key set against map set"); + return false; + } + + if (!map.keySet().containsAll(array.keySet())) { + Log.e("test", "Failed map key set contains all of array key set"); + return false; + } + + if (!array.keySet().containsAll(map.keySet())) { + Log.e("test", "Failed array key set contains all of map key set"); + return false; + } + + if (!array.containsAll(map.keySet())) { + Log.e("test", "Failed array contains all of map key set"); + return false; + } + + if (!map.entrySet().containsAll(array.entrySet())) { + Log.e("test", "Failed map entry set contains all of array entry set"); + return false; + } + + if (!array.entrySet().containsAll(map.entrySet())) { + Log.e("test", "Failed array entry set contains all of map entry set"); + return false; + } + return true; } |
