commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Edgar Poce <ep...@fibertel.com.ar>
Subject Re: [Collections] deep cloning
Date Wed, 01 Dec 2004 23:35:36 GMT
But the goal of the Collections component is to extend the JDK 
framework, right?.
I think it would be useful to have a deepClone utility under 
CollectionUtils.

something like

	/**
	 * Deep clone.
	 * It creates a new Collection wich contains a clone of
	 * each element found in the original Collection.
	 * All the elements in the original Collection must support
	 * clone() method invocation. If not CloneNotSupportedException
	 * is thrown
	 *
	 * @param collection
	 * @return
	 * @throws CloneNotSupportedException
	 */
     private static Collection deepClone(Collection collection) throws 
CloneNotSupportedException {
         ArrayList retu = new ArrayList();
         Iterator iter = collection.iterator() ;
         while (iter.hasNext()) {
             Object o = (Object) iter.next() ;
             try {
                 Method method = o.getClass().getDeclaredMethod("clone", 
new Class[]{});
                 Object clone = method.invoke(o, new Object[]{} );
                 retu.add(clone);
             } catch (Exception e) {
                 throw new CloneNotSupportedException("Unable to clone 
element " + o + ". Exception=" + e.getClass().getName() + ". Message= " 
+ e.getMessage() );
             }
         }
         return retu ;
     }	

----- Original Message -----
From: Stephen Colebourne
 > No, because the JDK collections API doesn't feature cloning as part 
of > its
 > standard  functions.
 >
 > You could try commons-lang SerializationUtils clone. That will do 
what > you
 > want, just a little slowly.
 >
 > Stephen
 >
 > ----- Original Message -----
 > From: "Edgar Poce"
 > > hi
 > >   is there any method in the package that clones a Collection and
 > > returns a new Collection wich contains a clone of each element?.
 > >
 > > Thanks in advance
 > > Edgar
 > >
 > > >---------------------------------------------------------------------


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Mime
View raw message