commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From <>
Subject Re: Closure, Predicate, and Transformer Examples
Date Sun, 25 Feb 2007 23:37:03 GMT

---- Vishist Mandapaka <> 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.

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

        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.

 Given a list of Person objects, return only those whose country attribute has a specific
value (filtering).

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:
For additional commands, e-mail:

View raw message