You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by te...@apache.org on 2010/03/26 13:27:05 UTC
svn commit: r927796 - in /harmony/enhanced/trunk/classlib/modules/luni/src:
main/java/java/util/HashMap.java
test/api/common/org/apache/harmony/luni/tests/java/util/HashMapTest.java
Author: tellison
Date: Fri Mar 26 12:27:05 2010
New Revision: 927796
URL: http://svn.apache.org/viewvc?rev=927796&view=rev
Log:
Improve compatibility with the RI by adding new elements before rehashing.
Set rehashing bucket to null to deter threads seeing the linked list being rehashed.
Modified:
harmony/enhanced/trunk/classlib/modules/luni/src/main/java/java/util/HashMap.java
harmony/enhanced/trunk/classlib/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/HashMapTest.java
Modified: harmony/enhanced/trunk/classlib/modules/luni/src/main/java/java/util/HashMap.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/trunk/classlib/modules/luni/src/main/java/java/util/HashMap.java?rev=927796&r1=927795&r2=927796&view=diff
==============================================================================
--- harmony/enhanced/trunk/classlib/modules/luni/src/main/java/java/util/HashMap.java (original)
+++ harmony/enhanced/trunk/classlib/modules/luni/src/main/java/java/util/HashMap.java Fri Mar 26 12:27:05 2010
@@ -568,10 +568,10 @@ public class HashMap<K, V> extends Abstr
entry = findNullKeyEntry();
if (entry == null) {
modCount++;
+ entry = createHashedEntry(null, 0, 0);
if (++elementCount > threshold) {
rehash();
}
- entry = createHashedEntry(null, 0, 0);
}
} else {
int hash = computeHashCode(key);
@@ -579,11 +579,10 @@ public class HashMap<K, V> extends Abstr
entry = findNonNullKeyEntry(key, index, hash);
if (entry == null) {
modCount++;
+ entry = createHashedEntry(key, index, hash);
if (++elementCount > threshold) {
rehash();
- index = hash & (elementData.length - 1);
}
- entry = createHashedEntry(key, index, hash);
}
}
@@ -639,6 +638,7 @@ public class HashMap<K, V> extends Abstr
Entry<K, V>[] newData = newElementArray(length);
for (int i = 0; i < elementData.length; i++) {
Entry<K, V> entry = elementData[i];
+ elementData[i] = null;
while (entry != null) {
int index = entry.origKeyHash & (length - 1);
Entry<K, V> next = entry.next;
Modified: harmony/enhanced/trunk/classlib/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/HashMapTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/trunk/classlib/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/HashMapTest.java?rev=927796&r1=927795&r2=927796&view=diff
==============================================================================
--- harmony/enhanced/trunk/classlib/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/HashMapTest.java (original)
+++ harmony/enhanced/trunk/classlib/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/HashMapTest.java Fri Mar 26 12:27:05 2010
@@ -593,6 +593,39 @@ public class HashMapTest extends junit.f
}
/**
+ * Compatibility test to ensure we rehash the same way as the RI.
+ * Not required by the spec, but some apps seem sensitive to it.
+ */
+ public void test_rehash() {
+ // This map should rehash on adding the ninth element.
+ HashMap<MyKey, Integer> hm = new HashMap<MyKey, Integer>(10, 0.5f);
+
+ // Ordered set of keys.
+ MyKey[] keyOrder = new MyKey[9];
+ for (int i = 0; i < keyOrder.length; i++) {
+ keyOrder[i] = new MyKey();
+ }
+
+ // Store eight elements
+ for (int i = 0; i < 8; i++) {
+ hm.put(keyOrder[i], i);
+ }
+ // Check expected ordering (inverse of adding order)
+ MyKey[] returnedKeys = hm.keySet().toArray(new MyKey[8]);
+ for (int i = 0; i < 8; i++) {
+ assertSame(keyOrder[i], returnedKeys[7 - i]);
+ }
+
+ // The next put causes a rehash
+ hm.put(keyOrder[8], 8);
+ // Check expected new ordering (adding order)
+ returnedKeys = hm.keySet().toArray(new MyKey[8]);
+ for (int i = 0; i < 9; i++) {
+ assertSame(keyOrder[i], returnedKeys[i]);
+ }
+ }
+
+ /**
* @tests java.util.HashMap#size()
*/
public void test_size() {