You are viewing a plain text version of this content. The canonical link for it is here.
Posted to common-commits@hadoop.apache.org by to...@apache.org on 2011/02/24 18:59:04 UTC

svn commit: r1074239 - in /hadoop/common/trunk: CHANGES.txt src/java/org/apache/hadoop/io/MapWritable.java src/test/core/org/apache/hadoop/io/TestMapWritable.java

Author: todd
Date: Thu Feb 24 17:59:04 2011
New Revision: 1074239

URL: http://svn.apache.org/viewvc?rev=1074239&view=rev
Log:
HADOOP-7153. MapWritable violates contract of Map interface for equals() and hashCode(). Contributed by Nicholas Telford.

Modified:
    hadoop/common/trunk/CHANGES.txt
    hadoop/common/trunk/src/java/org/apache/hadoop/io/MapWritable.java
    hadoop/common/trunk/src/test/core/org/apache/hadoop/io/TestMapWritable.java

Modified: hadoop/common/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/hadoop/common/trunk/CHANGES.txt?rev=1074239&r1=1074238&r2=1074239&view=diff
==============================================================================
--- hadoop/common/trunk/CHANGES.txt (original)
+++ hadoop/common/trunk/CHANGES.txt Thu Feb 24 17:59:04 2011
@@ -76,6 +76,9 @@ Trunk (unreleased changes)
     HADOOP-7048.  Wrong description of Block-Compressed SequenceFile Format in
     SequenceFile's javadoc.  (Jingguo Yao via tomwhite)
 
+    HADOOP-7153. MapWritable violates contract of Map interface for equals()
+    and hashCode(). (Nicholas Telford via todd)
+
 Release 0.22.0 - Unreleased
 
   INCOMPATIBLE CHANGES

Modified: hadoop/common/trunk/src/java/org/apache/hadoop/io/MapWritable.java
URL: http://svn.apache.org/viewvc/hadoop/common/trunk/src/java/org/apache/hadoop/io/MapWritable.java?rev=1074239&r1=1074238&r2=1074239&view=diff
==============================================================================
--- hadoop/common/trunk/src/java/org/apache/hadoop/io/MapWritable.java (original)
+++ hadoop/common/trunk/src/java/org/apache/hadoop/io/MapWritable.java Thu Feb 24 17:59:04 2011
@@ -76,11 +76,34 @@ public class MapWritable extends Abstrac
   }
 
   /** {@inheritDoc} */
+  public boolean equals(Object obj) {
+    if (this == obj) {
+      return true;
+    }
+
+    if (obj instanceof MapWritable) {
+      Map map = (Map) obj;
+      if (size() != map.size()) {
+        return false;
+      }
+
+      return entrySet().equals(map.entrySet());
+    }
+
+    return false;
+  }
+
+  /** {@inheritDoc} */
   public Writable get(Object key) {
     return instance.get(key);
   }
   
   /** {@inheritDoc} */
+  public int hashCode() {
+    return 1 + this.instance.hashCode();
+  }
+
+  /** {@inheritDoc} */
   public boolean isEmpty() {
     return instance.isEmpty();
   }

Modified: hadoop/common/trunk/src/test/core/org/apache/hadoop/io/TestMapWritable.java
URL: http://svn.apache.org/viewvc/hadoop/common/trunk/src/test/core/org/apache/hadoop/io/TestMapWritable.java?rev=1074239&r1=1074238&r2=1074239&view=diff
==============================================================================
--- hadoop/common/trunk/src/test/core/org/apache/hadoop/io/TestMapWritable.java (original)
+++ hadoop/common/trunk/src/test/core/org/apache/hadoop/io/TestMapWritable.java Thu Feb 24 17:59:04 2011
@@ -129,4 +129,24 @@ public class TestMapWritable extends Tes
     assertTrue(m.get(t).equals(t));
     dis.close();
   }
+
+  public void testEquality() {
+    MapWritable map1 = new MapWritable();
+    MapWritable map2 = new MapWritable();
+    MapWritable map3 = new MapWritable();
+    final IntWritable k1 = new IntWritable(5);
+    final IntWritable k2 = new IntWritable(10);
+    final Text value = new Text("value");
+
+    map1.put(k1, value); // equal
+    map2.put(k1, value); // equal
+    map3.put(k2, value); // not equal
+
+    assertTrue(map1.equals(map2));
+    assertTrue(map2.equals(map1));
+    assertFalse(map1.equals(map3));
+
+    assertEquals(map1.hashCode(), map2.hashCode());
+    assertFalse(map1.hashCode() == map3.hashCode());
+}
 }