Fix copy constructor to work on JDK1.3

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/trunk@131784 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stephen Colebourne 2004-06-09 22:11:54 +00:00
parent 196a8151b7
commit 730d3e5569
2 changed files with 36 additions and 6 deletions

View File

@ -52,7 +52,7 @@ import org.apache.commons.collections.iterators.EmptyIterator;
* <code>list</code> will be a list containing "A", "B", "C".
*
* @since Commons Collections 2.0
* @version $Revision: 1.19 $ $Date: 2004/05/26 21:56:05 $
* @version $Revision: 1.20 $ $Date: 2004/06/09 22:11:54 $
*
* @author Christopher Berry
* @author James Strachan
@ -109,14 +109,17 @@ public class MultiHashMap extends HashMap implements MultiMap {
* @param mapToCopy a Map to copy
*/
public MultiHashMap(Map mapToCopy) {
super(mapToCopy);
// be careful of JDK 1.3 vs 1.4 differences
super((int) (mapToCopy.size() * 1.4f));
if (mapToCopy instanceof MultiMap) {
for (Iterator it = entrySet().iterator(); it.hasNext();) {
for (Iterator it = mapToCopy.entrySet().iterator(); it.hasNext();) {
Map.Entry entry = (Map.Entry) it.next();
Collection coll = (Collection) entry.getValue();
Collection newColl = createCollection(coll);
entry.setValue(newColl);
super.put(entry.getKey(), newColl);
}
} else {
putAll(mapToCopy);
}
}

View File

@ -18,6 +18,7 @@ package org.apache.commons.collections;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
@ -29,7 +30,7 @@ import org.apache.commons.collections.map.AbstractTestMap;
/**
* Unit Tests for <code>MultiHashMap</code>.
*
* @version $Revision: 1.19 $ $Date: 2004/05/14 22:33:58 $
* @version $Revision: 1.20 $ $Date: 2004/06/09 22:11:54 $
*
* @author Unknown
*/
@ -368,7 +369,7 @@ public class TestMultiHashMap extends AbstractTestMap {
assertEquals(2, clonedColl.size());
}
public void testConstructor() {
public void testConstructorCopy1() {
MultiHashMap map = new MultiHashMap();
map.put("A", "1");
map.put("A", "2");
@ -384,10 +385,36 @@ public class TestMultiHashMap extends AbstractTestMap {
assertEquals(2, coll.size());
assertEquals(1, newMap.size());
assertEquals(2, newColl.size());
map.put("A", "3");
assertEquals(1, map.size());
assertEquals(3, coll.size());
assertEquals(1, newMap.size());
assertEquals(2, newColl.size());
}
public void testConstructorCopy2() {
Map map = new HashMap();
map.put("A", "1");
map.put("B", "2");
assertEquals(2, map.size());
MultiHashMap newMap = new MultiHashMap(map);
Collection newColl = (Collection) newMap.get("A");
assertNotSame(map, newMap);
assertEquals(2, map.size());
assertEquals(2, newMap.size());
assertEquals(1, newColl.size());
map.put("A", "3");
assertEquals(2, map.size());
assertEquals(2, newMap.size());
assertEquals(1, newColl.size());
map.put("C", "4");
assertEquals(3, map.size());
assertEquals(2, newMap.size());
assertEquals(1, newColl.size());
}
}