You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by nd...@apache.org on 2006/07/04 22:29:06 UTC
svn commit: r419085 - in
/incubator/harmony/enhanced/classlib/trunk/modules/luni/src:
main/java/java/util/HashMap.java main/java/java/util/MapEntry.java
test/java/tests/api/java/util/HashMapTest.java
Author: ndbeyer
Date: Tue Jul 4 13:29:05 2006
New Revision: 419085
URL: http://svn.apache.org/viewvc?rev=419085&view=rev
Log:
Fixes related to HARMONY-403 [HashMap hashcode ignores values in entries] and HARMONY-206 [HashMap's behavior is non-compliant with RI when key is reused].
Modified:
incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/HashMap.java
incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/MapEntry.java
incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/HashMapTest.java
Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/HashMap.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/HashMap.java?rev=419085&r1=419084&r2=419085&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/HashMap.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/HashMap.java Tue Jul 4 13:29:05 2006
@@ -41,13 +41,12 @@
private static final int DEFAULT_SIZE = 16;
static class Entry<K, V> extends MapEntry<K, V> {
- final int hash;
-
+ final int origKeyHash;
Entry<K, V> next;
Entry(K theKey, V theValue) {
super(theKey, theValue);
- this.hash = (theKey == null) ? 0 : theKey.hashCode();
+ origKeyHash = (theKey == null ? 0 : theKey.hashCode());
}
@Override
@@ -59,16 +58,6 @@
}
return entry;
}
-
- @Override
- public String toString() {
- return key + "=" + value;
- }
-
- @Override
- public int hashCode() {
- return hash;
- }
}
static class HashMapIterator<E,KT,VT> implements Iterator<E> {
@@ -349,7 +338,15 @@
* @return true if the keys are considered equal
*/
boolean keysEqual(Object k1, Entry<K, V> entry) {
- return entry.hashCode() == k1.hashCode() && k1.equals(entry.key);
+ int k1Hash = k1 == null ? 0 : k1.hashCode();
+ if (k1Hash != entry.origKeyHash) {
+ return false;
+ }
+ if (k1 == null && entry.key == null) {
+ return true;
+ }
+ assert k1 != null;
+ return k1.equals(entry.key);
}
/**
@@ -523,8 +520,10 @@
index = key == null ? 0 : (key.hashCode() & 0x7FFFFFFF)
% elementData.length;
}
- entry = createEntry(key, index, null);
+ entry = createEntry(key, index, value);
+ return null;
}
+
V result = entry.value;
entry.value = value;
return result;
Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/MapEntry.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/MapEntry.java?rev=419085&r1=419084&r2=419085&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/MapEntry.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/MapEntry.java Tue Jul 4 13:29:05 2006
@@ -76,4 +76,8 @@
value = object;
return result;
}
+
+ public String toString() {
+ return key + "=" + value;
+ }
}
Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/HashMapTest.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/HashMapTest.java?rev=419085&r1=419084&r2=419085&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/HashMapTest.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/HashMapTest.java Tue Jul 4 13:29:05 2006
@@ -215,7 +215,7 @@
assertNull("Get returned non-null for non existent key",
hm.get("T"));
hm.put("T", "HELLO");
- assertEquals("Get returned incorecct value for existing key", "HELLO", hm.get("T")
+ assertEquals("Get returned incorrect value for existing key", "HELLO", hm.get("T")
);
HashMap m = new HashMap();
@@ -409,6 +409,21 @@
return key == ((ReusableKey) o).key;
}
}
+
+ public void test_Map_Entry_hashCode() {
+ //Related to HARMONY-403
+ HashMap<Integer, Integer> map = new HashMap<Integer, Integer>(10);
+ Integer key = new Integer(1);
+ Integer val = new Integer(2);
+ map.put(key, val);
+ int expected = key.hashCode() ^ val.hashCode();
+ assertEquals(expected, map.hashCode());
+ key = new Integer(4);
+ val = new Integer(8);
+ map.put(key, val);
+ expected += key.hashCode() ^ val.hashCode();
+ assertEquals(expected, map.hashCode());
+ }
/**
* Sets up the fixture, for example, open a network connection. This method