commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Rafael U. C. Afonso" <r.u.c.afo...@uol.com.br>
Subject Re: [Collections] deep cloning
Date Thu, 02 Dec 2004 00:34:05 GMT
Hello:

It could be done simply using
InvokerTransformer
(http://jakarta.apache.org/commons/collections/apidocs-COLLECTIONS_3_1/org/apache/commons/collections/functors/InvokerTransformer.html)
and CollectionUtils.collect()
(http://jakarta.apache.org/commons/collections/apidocs-COLLECTIONS_3_1/org/apache/commons/collections/CollectionUtils.html#collect(java.util.Collection,%20org.apache.commons.collections.Transformer)):

Collection clonedCollection =
CollectionUtils.collect(originalCollection,
new
InvokerTransformer.getInstance("clone"));


Thanks,


---------- Início da mensagem original
-----------

      De: "Edgar Poce"
epoce@fibertel.com.ar
    Para: "Jakarta Commons Users List"
commons-user@jakarta.apache.org
      Cc: 
    Data: Wed, 01 Dec 2004 20:35:36 -0300
 Assunto: Re: [Collections] deep cloning

> 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
> 
> 

Rafael Ubiratam Clemente Afonso
r.u.c.afonso@uol.com.br
---------------------------------
Where is Debug?
Debug is on the Table!
 
__________________________________________________________________________
Acabe com aquelas janelinhas que pulam na sua tela.
AntiPop-up UOL - É grátis!
http://antipopup.uol.com.br/



---------------------------------------------------------------------
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