Revision: 10527
Author:   [email protected]
Date:     Tue Aug 16 03:35:30 2011
Log: Fix TreeMap.Node.equals() to allow nodes to be equal to other types of Map.Entry.
While there, eliminate an unnecessary unchecked cast.

Review at http://gwt-code-reviews.appspot.com/1523804

Review by: [email protected]
http://code.google.com/p/google-web-toolkit/source/detail?r=10527

Modified:
 /trunk/user/super/com/google/gwt/emul/java/util/TreeMap.java
 /trunk/user/test/com/google/gwt/emultest/java/util/TreeMapTest.java

=======================================
--- /trunk/user/super/com/google/gwt/emul/java/util/TreeMap.java Fri Oct 8 06:15:38 2010 +++ /trunk/user/super/com/google/gwt/emul/java/util/TreeMap.java Tue Aug 16 03:35:30 2011
@@ -199,16 +199,14 @@
       this.isRed = isRed;
     }

-    @SuppressWarnings("unchecked")
-    // generic cast
     @Override
     public boolean equals(Object o) {
-      if (!(o instanceof Node)) {
+      if (!(o instanceof Map.Entry)) {
         return false;
       }
-      Node<K, V> other = (Node<K, V>) o; // suppress unchecked
-      return Utility.equalsWithNullCheck(key, other.key)
-          && Utility.equalsWithNullCheck(value, other.value);
+      Map.Entry<?, ?> other = (Map.Entry<?, ?>) o;
+      return Utility.equalsWithNullCheck(key, other.getKey())
+          && Utility.equalsWithNullCheck(value, other.getValue());
     }

     public K getKey() {
=======================================
--- /trunk/user/test/com/google/gwt/emultest/java/util/TreeMapTest.java Thu Dec 16 11:33:51 2010 +++ /trunk/user/test/com/google/gwt/emultest/java/util/TreeMapTest.java Tue Aug 16 03:35:30 2011
@@ -46,6 +46,50 @@
  * work.
  */
public abstract class TreeMapTest<K extends Comparable<K>, V> extends TestMap {
+  private static final class SimpleEntry<K, V> implements Entry<K, V> {
+    private static boolean equal(Object a, Object b) {
+      return (a == null) ? (b == null) : a.equals(b);
+    }
+
+    private final K key;
+    private final V value;
+
+    private SimpleEntry(K key, V value) {
+      this.key = key;
+      this.value = value;
+    }
+
+    @Override
+    public boolean equals(Object object) {
+      if (object instanceof Entry) {
+        Entry<?, ?> other = (Entry<?, ?>) object;
+ return equal(key, other.getKey()) && equal(value, other.getValue());
+      }
+      return false;
+    }
+
+    @Override
+    public K getKey() {
+      return key;
+    }
+
+    @Override
+    public V getValue() {
+      return value;
+    }
+
+    @Override
+    public int hashCode() {
+      return ((key == null) ? 0 : key.hashCode())
+          ^ ((value == null) ? 0 : value.hashCode());
+    }
+
+    @Override
+    public final V setValue(V value) {
+      throw new UnsupportedOperationException();
+    }
+  }
+
   /**
    * Verify a Collection is explicitly and implicitly empty.
    *
@@ -515,6 +559,10 @@

     assertEquals(entry.getKey(), getKeys()[0]);
     assertEquals(entry.getValue(), getValues()[0]);
+ // Don't use assertEquals; we want to be clear about which object's equals()
+    // method to test.
+    assertTrue(
+        entry.equals(new SimpleEntry<K, V>(getKeys()[0], getValues()[0])));
   }

   /**

--
http://groups.google.com/group/Google-Web-Toolkit-Contributors

Reply via email to