commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From José Antonio Pérez Testa <japte...@indra.es>
Subject Re: Tranformer collection processor
Date Tue, 17 Aug 2004 06:55:26 GMT
As Kishore said

org.apache.commons.collections.CollectionUtils

can do what you need. You must use 
#CollectionUtils.collect(Collection collection, Transformer transformer)#

See below ...

Rafael U. C. Afonso wrote:

>Hello again:
>
>I Read CollectionTranfomer API. It
>says: "The add methods are affected by
>this class.". Reading the source code
>I could see that only added elements
>are transformed. But If I have a
>Collection (List, Set, Bag, etc) that
>has elements before? I want that ALL
>it elements be transfomed, not only
>when I add something. For example I
>create this program test that uses
>ListTranformer:
>
>import org.apache.commons.collections.*;
>import java.util.*;
>
>public class TestaListTransformer {
>	static Object[] vector = {"a", "b", "c"};
>	
>	private static class UpperTransformer
>implements Transformer {
>		public Object transform(Object input) {
>			return input.toString().toUpperCase();
>		}
>	}
>	
>	public static void main(String args[]) {
>		List list1 = Arrays.asList(vector);
>		System.out.println("Original List: "
>+ list1);
>		
>		List list2 =
>// ListUtils.transformedList(list1, new UpperTransformer());
>  
>
CollectionUtils.collect(list1,new UpperTransformer());

>		System.out.println("New List: " +
>list2);
>		
>		System.out.print("Iteracting over
>New List: ");
>		for(Iterator it = list2.iterator();
>it.hasNext(); ) {
>		
>System.out.print(it.next().toString()
>+ ", ");
>		}
>	}
>	
>}
>
>My results were:
>
>Original List: [a, b, c]
>New List: [a, b, c]
>Iteracting over New List: a, b, c, 
>
>What I wanted is when I do a
>toString() or iterator() over newList
>I got all its elements tranformed. Im
>this case to get [A, B, C]. So I
>maintain again my proposal to create a
>method that returns a Collection
>(List, Set, Bag, etc) with all
>elements transformed.
>
>Thanks,
>
>Rafael Ubiratam Clemente Afonso
>r.u.c.afonso@uol.com.br
>---------------------------------
>Where is Debug?
>Debug is on the Table!
>
>---------- Início da mensagem original
>-----------
>
>      De: "Kishore Senji" ksenji@gmail.com
>    Para: "Jakarta Commons Users List"
>commons-user@jakarta.apache.org
>      Cc: 
>    Data: Tue, 3 Aug 2004 22:23:20 -0400
> Assunto: Re: Tranformer collection
>processor
>
>  
>
>>CollectionUtils.transform(Collection
>>    
>>
>collection, Transformer
>  
>
>>transformer); does what you have
>>    
>>
>proposed
>  
>
>>On Tue,  3 Aug 2004 23:09:30 -0300,
>>    
>>
>Rafael U. C. Afonso
>  
>
>><r.u.c.afonso@uol.com.br> wrote:
>>    
>>
>>>Hello:
>>>
>>>Is there something in Collections
>>>      
>>>
>that, given a collection
>  
>
>>>and a Transformer, return a
>>>      
>>>
>collection of transformed
>  
>
>>>Objects?
>>>It would be like this:
>>>
>>>class TransformeProcessorCollection {
>>>   private Transformer transformer;
>>>
>>>   public
>>>      
>>>
>TransformeProcessorCollection(Transformer
>trans) {
>  
>
>>>       transformer = trans;
>>>   }
>>>
>>>   public Collection
>>>      
>>>
>process(Colletcion col) {
>  
>
>>>       Collection newCol = new
>>>      
>>>
>ArrayList(col.size); // Or
>  
>
>>>some other type of Collection
>>>       Iterator it = new
>>>      
>>>
>TransformIterator(col.iterator(),
>  
>
>>>transformer);
>>>       while(it.hasNext()) {
>>>           newCol.add(it.next());
>>>       }
>>>       return newCol;
>>>   }
>>>
>>>}
>>>
>>>If it does not exist, I would like
>>>      
>>>
>suggest it for Commons
>  
>
>>>Collections. Something similar
>>>      
>>>
>would be done with Closures
>  
>
>>>and Predicates.
>>>
>>>Thanks,
>>>
>>>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
>  
>
>>>      
>>>
>>    
>>
>----------------------------------------------
>  
>
>>To unsubscribe, e-mail:
>>    
>>
>commons-user-unsubscribe@jakarta.apache.org
>  
>
>>For additional commands, e-mail:
>>    
>>
>commons-user-help@jakarta.apache.org
>  
>
>>    
>>
>
>
> 
>__________________________________________________________________________
>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
>
>  
>
-------------------------------------------------------------------------------------------------------------------
Este correo electrónico y, en su caso, cualquier fichero anexo al mismo, contiene información
de carácter confidencial exclusivamente dirigida a su destinatario o destinatarios. Queda
prohibida su divulgación, copia o distribución a terceros sin la previa autorización escrita
de Indra. En el caso de haber recibido este correo electrónico por error, se ruega notificar
inmediatamente esta circunstancia mediante reenvío a la dirección electrónica del remitente.

The information in this e-mail and in any attachments is confidential and solely for the attention
and use of the named addressee(s). You are hereby notified that any dissemination, distribution
or copy of this communication is prohibited without the prior written consent of Indra. If
you have received this communication in error, please, notify the sender by reply e-mail

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