[collections] Can Closures return values?

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

[collections] Can Closures return values?

kokaku
I have a loop common to several methods where the behavior of the loop
depends on the boolean result of different method calls.

ex)

do {
   if(x.equals(y)) do some stuff
} while(test)

do {
   if(x.startsWith(y)) do some stuff
} while(test)

etc

The looping and testing are a bit more complicated than that (e.g. big
nested loop). I'd like to use an invokerClosure to handle the test so
that I can have one common method that takes the Closure as an
argument. However, I don't see a way to get a return value from a
Closure. Is this possible? I could make the whole if/then block the
Closure.execute but if possible I'd like to know if the Closure is
able to return values.

thx
andy

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: [collections] Can Closures return values?

Rafael U. C. Afonso
Andy:

Commons Collections API is here: http://jakarta.apache.org/commons/collections/apidocs-COLLECTIONS_3_1/index.html
Based in your case, I think that you should use a InvokerTransformer (http://jakarta.apache.org/commons/collections/apidocs-COLLECTIONS_3_1/org/apache/commons/collections/functors/InvokerTransformer.html) and a EqualPredicate (http://jakarta.apache.org/commons/collections/apidocs-COLLECTIONS_3_1/org/apache/commons/collections/functors/EqualPredicate.html).

Good luck,

Rafael Ubiratam Clemente Afonso
[hidden email]
---------------------------------
Where is Debug?
Debug is on the Table!

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

      De: "Andy Kriger" [hidden email]
    Para: "Rafael U. C. Afonso" [hidden email]
      Cc:
    Data: Thu, 26 May 2005 09:54:23 -0400
 Assunto: Re: [collections] Can Closures return values?

> Thx - I'm not that familiar with the collections API and wasn't aware
> of that. Is there documentation on the Closure/Transformer/Predicate
> aspect of the collections API?
>
> On 5/25/05, Rafael U. C. Afonso <[hidden email]> wrote:
> > Hello:
> >
> > Why don't you use a Transformer? It returns a Object.
> >
> > Thanks,
> >
> > Rafael U. C. Afonso
> >
> > ---------- Início da mensagem original -----------
> >
> >       De: "Andy Kriger" [hidden email]
> >     Para: [hidden email]
> >       Cc:
> >     Data: Wed, 25 May 2005 17:06:09 -0400
> >  Assunto: [collections] Can Closures return values?
> >
> > > I have a loop common to several methods where the behavior of the loop
> > > depends on the boolean result of different method calls.
> > >
> > > ex)
> > >
> > > do {
> > >    if(x.equals(y)) do some stuff
> > > } while(test)
> > >
> > > do {
> > >    if(x.startsWith(y)) do some stuff
> > > } while(test)
> > >
> > > etc
> > >
> > > The looping and testing are a bit more complicated than that (e.g. big
> > > nested loop). I'd like to use an invokerClosure to handle the test so
> > > that I can have one common method that takes the Closure as an
> > > argument. However, I don't see a way to get a return value from a
> > > Closure. Is this possible? I could make the whole if/then block the
> > > Closure.execute but if possible I'd like to know if the Closure is
> > > able to return values.
> > >
> > > thx
> > > andy
> > >
> > > ---------------------------------------------------------------------
> > > To unsubscribe, e-mail: [hidden email]
> > > For additional commands, e-mail: [hidden email]
> > >
> > >
> >
> > Rafael Ubiratam Clemente Afonso
> > [hidden email]
> > ---------------------------------
> > Where is Debug?
> > Debug is on the Table!
> >
> > __________________________________________________________________________
> > UOL Fone: Fale com o Brasil e o Mundo com até 90% de economia.
> > http://www.uol.com.br/fone
> >
> >
> >
>


 
__________________________________________________________________________
UOL Fone: Fale com o Brasil e o Mundo com até 90% de economia.
http://www.uol.com.br/fone



---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]