You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Edgar Poce <ep...@fibertel.com.ar> on 2004/11/30 21:42:05 UTC

[Collections] deep cloning

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


Re: [Collections] deep cloning

Posted by Stephen Colebourne <sc...@btopenworld.com>.
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" <ep...@fibertel.com.ar>
> 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
>


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


Re: [Collections] deep cloning

Posted by Edgar Poce <ep...@fibertel.com.ar>.
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