Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit bf83e0dd authored by Dianne Hackborn's avatar Dianne Hackborn Committed by Android (Google) Code Review
Browse files

Merge "Fix issue #10115327: MapCollections need to implement equals"

parents c8f464c8 8b7bc13e
Loading
Loading
Loading
Loading
+68 −5
Original line number Diff line number Diff line
@@ -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();
+59 −0
Original line number Diff line number Diff line
@@ -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;
    }