HADOOP-7153. MapWritable violates contract of Map interface for equals() and hashCode(). Contributed by Nicholas Telford.

git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/trunk@1074239 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Todd Lipcon 2011-02-24 17:59:04 +00:00
parent 5148cc83e0
commit 27df75e6bb
3 changed files with 46 additions and 0 deletions

View File

@ -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

View File

@ -75,11 +75,34 @@ public Set<Map.Entry<Writable, Writable>> entrySet() {
return instance.entrySet();
}
/** {@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();

View File

@ -129,4 +129,24 @@ public void testMultipleCallsToReadFieldsAreSafe() throws Exception {
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());
}
}