commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Stephen Colebourne <scolebou...@btopenworld.com>
Subject Re: Closure, Predicate, and Transformer Examples
Date Wed, 28 Feb 2007 23:50:33 GMT
Its an example of a class you'd write yourself. Although there are 
similar methods on commons-collections CollectionUtils.

Stephen


Paolo Viappiani wrote:
> In which package is ModelUtils?
> 
> p*
> 
> On 2/26/07, simon.kitching@xtra.co.nz <simon.kitching@xtra.co.nz> wrote:
>>
>>
>> ---- Vishist Mandapaka <vishistm@gmail.com> wrote:
>> > Hi,
>> >   Can anyone guide me with examples for Closure, Predicate, and
>> Transformer
>> > objects. I am unable to map it to any real world scenarios where these
>> > objects may be of use.
>>
>>
>> Closure:
>> ========
>> I have a tree of Attribute objects, and for each one which has one or 
>> more
>> attached errors I want to add it to a list. I therefore have a method 
>> that
>> walks the tree of objects and for each node in the tree invokes a Closure
>> that is passed in as a parameter:
>>
>>     private List findErrors(AttributeGroup model) {
>>         final List errors = new ArrayList(10);
>>
>>         Closure cb = new Closure() {
>>             public void execute(Object input) {
>>                 Attribute attr = (Attribute) input;
>>                 List errList = attr.getErrorList();
>>                 if ((errList != null) && (errList.size() > 0)) {
>>                     errors.add(attr);
>>                 }
>>             }
>>         };
>>
>>         ModelUtils.forEachAttr(model, cb);
>>
>>         return errors;
>>     }
>>
>> The tree-walking code (ModelUtils.forEachAttr) can then be reused with
>> Closure objects that do other types of things.
>>
>>
>> Predicate:
>> Given a list of Person objects, return only those whose country attribute
>> has a specific value (filtering).
>>
>> Transformer:
>> Given a list of strings, return a list of JSF SelectItem objects that 
>> wrap
>> those strings, allowing the list to be displayed in a drop-down list in a
>> JavaServer Faces view. This same Transformer class can then be used to
>> generate wrappers for a list of colours, a list of countries, etc. Yes a
>> simple method can be written to do this, but with the Transformer 
>> approach a
>> Transformer instance can then be passed around as an object without 
>> the code
>> needing to know exactly *what* the Transformer will do.
>>
>> ---------------------------------------------------------------------
>> 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


Mime
View raw message