Fix AbstractHeashedMap initialization to calculate correct threshold

bug 35012, by Christian Siefkes

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/trunk@171349 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stephen Colebourne 2005-05-22 17:48:56 +00:00
parent 2b68e826ec
commit f9045aedf3
4 changed files with 18 additions and 4 deletions

View File

@ -71,6 +71,7 @@ If this causes major headaches to anyone please contact commons-dev at jakarta.a
<li>AbstractHashedMap deserialization - Fix to prevent doubling of internal data array [34265]</li>
<li>Flat3Map.equals() - Fix to make flat mode comparison actually work [34917]</li>
<li>IteratorChain.remove() - Fix to avoid IllegalStateException when one of the underlying iterators is a FilterIterator [34267]</li>
<li>AbstractHashedMap initialization - Fix to setup threshold correctly, improving performance [35012]</li>
</ul>
<center><h3>JAVADOC</h3></center>

View File

@ -284,6 +284,9 @@
<contributor>
<name>Jon Schewe</name>
</contributor>
<contributor>
<name>Christian Siefkes</name>
</contributor>
<contributor>
<name>Michael Smith</name>
</contributor>

View File

@ -1,5 +1,5 @@
/*
* Copyright 2003-2004 The Apache Software Foundation
* Copyright 2003-2005 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -56,6 +56,7 @@ import org.apache.commons.collections.iterators.EmptyMapIterator;
*
* @author java util HashMap
* @author Stephen Colebourne
* @author Christian Siefkes
*/
public class AbstractHashedMap extends AbstractMap implements IterableMap {
@ -145,8 +146,8 @@ public class AbstractHashedMap extends AbstractMap implements IterableMap {
throw new IllegalArgumentException("Load factor must be greater than 0");
}
this.loadFactor = loadFactor;
this.threshold = calculateThreshold(initialCapacity, loadFactor);
initialCapacity = calculateNewCapacity(initialCapacity);
this.threshold = calculateThreshold(initialCapacity, loadFactor);
this.data = new HashEntry[initialCapacity];
init();
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2001-2004 The Apache Software Foundation
* Copyright 2001-2005 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -58,7 +58,16 @@ public class TestHashedMap extends AbstractTestIterableMap {
assertEquals(map.size(), cloned.size());
assertSame(map.get("1"), cloned.get("1"));
}
public void testInternalState() {
HashedMap map = new HashedMap(42, 0.75f);
assertEquals(0.75f, map.loadFactor, 0.1f);
assertEquals(0, map.size);
assertEquals(64, map.data.length);
assertEquals(48, map.threshold);
assertEquals(0, map.modCount);
}
// public void testCreate() throws Exception {
// resetEmpty();
// writeExternalFormToDisk((java.io.Serializable) map, "D:/dev/collections/data/test/HashedMap.emptyCollection.version3.obj");