commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Emmanuel Bourg <>
Subject [collections] InvokerClosure
Date Thu, 02 Dec 2004 17:16:38 GMT
Hi all, I decided to play a bit with closures today and tried replacing 
a trivial loop in [configuration] :

for (Iterator i = configList.iterator(); i.hasNext();)
     Configuration config = (Configuration);

I thought a closure would help reducing the code, but actually I ended 
with something much longer :

CollectionUtils.forAllDo(configList, new TransformerClosure(new 
InvokerTransformer("clearProperty", new Class[] { String.class }, new 
Object[] { key })));

Then I found the invokerClosure method in ClosureUtils, the code turned 
into :

ClosureUtils.invokerClosure("clearProperty", new Class[] { String.class 
}, new Object[] { key }));

Slightly better but still quite long. Why is there no InvokerClosure 
class ? This is the only closure in ClosureUtils that has no 
corresponding class, this could make the code a bit shorter:

CollectionUtils.forAllDo(configList, new InvokerClosure("clearProperty", 
new Class[] { String.class }, new Object[] { key }));

Also the Class[] and Object[] are quite cumbersome, that would be nice 
to add methods/constructors to invoke simple methods with only one or 
two parameters. Thus my example could look like this:

CollectionUtils.forAllDo(configList, new InvokerClosure("clearProperty", 
String.class, key));

which is much more readable.

What do you think ?

Emmanuel Bourg

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message