You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by sc...@apache.org on 2004/05/15 00:33:31 UTC
cvs commit: jakarta-commons/collections/src/java/org/apache/commons/collections MultiHashMap.java
scolebourne 2004/05/14 15:33:31
Modified: collections/src/java/org/apache/commons/collections
MultiHashMap.java
Log:
Ensure constructor and clone method correctly clone multimaps
bug 28972
Revision Changes Path
1.18 +23 -7 jakarta-commons/collections/src/java/org/apache/commons/collections/MultiHashMap.java
Index: MultiHashMap.java
===================================================================
RCS file: /home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/MultiHashMap.java,v
retrieving revision 1.17
retrieving revision 1.18
diff -u -r1.17 -r1.18
--- MultiHashMap.java 14 Mar 2004 17:05:24 -0000 1.17
+++ MultiHashMap.java 14 May 2004 22:33:31 -0000 1.18
@@ -94,12 +94,28 @@
}
/**
- * Constructor.
+ * Constructor that copies the input map creating an independent copy.
+ * <p>
+ * This method performs different behaviour depending on whether the map
+ * specified is a MultiMap or not. If a MultiMap is specified, each internal
+ * collection is also cloned. If the specified map only implements Map, then
+ * the values are not cloned.
+ * <p>
+ * NOTE: From Commons Collections 3.1 this method correctly copies a MultiMap
+ * to form a truly independent new map.
*
* @param mapToCopy a Map to copy
*/
public MultiHashMap(Map mapToCopy) {
super(mapToCopy);
+ if (mapToCopy instanceof MultiMap) {
+ for (Iterator it = entrySet().iterator(); it.hasNext();) {
+ Map.Entry entry = (Map.Entry) it.next();
+ Collection coll = (Collection) entry.getValue();
+ Collection newColl = createCollection(coll);
+ entry.setValue(newColl);
+ }
+ }
}
/**
@@ -401,25 +417,25 @@
//-----------------------------------------------------------------------
/**
- * Clone the map.
+ * Clones the map creating an independent copy.
* <p>
* The clone will shallow clone the collections as well as the map.
*
* @return the cloned map
*/
public Object clone() {
- MultiHashMap obj = (MultiHashMap) super.clone();
+ MultiHashMap cloned = (MultiHashMap) super.clone();
// clone each Collection container
- for (Iterator it = entrySet().iterator(); it.hasNext();) {
+ for (Iterator it = cloned.entrySet().iterator(); it.hasNext();) {
Map.Entry entry = (Map.Entry) it.next();
Collection coll = (Collection) entry.getValue();
Collection newColl = createCollection(coll);
entry.setValue(newColl);
}
- return obj;
+ return cloned;
}
-
+
/**
* Creates a new instance of the map value Collection container.
* <p>
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org