Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making a Java library "Groovy"

Tags:

groovy

I fell in love with Groovy and try to use it more and more. Now I have to work with Oracle Forms Jdapi library. When working with this library, you write a lot of code like this:

JdapiIterator progIterator = getWorkForm().getProgramUnits();
while(progIterator.hasNext()) {
    ProgramUnit currProgUnit = (ProgramUnit) progIterator.next();
    ...
}

and of cource I would like to write

getWorkForm().programUnits.each {
    ...
}

However, I never wrote a Groovy interface to an existing Java library and need some assistance. I know about Groovy 2.0's extension methods, but in that case I am thinking about a class with the same name in a different namespace which delegates only to the functions I would like to keep.

What is the best approach for providing the each functionality, but also all other closures applicable for collections? I would appreciate if you point me in the right direction!

like image 273
ChrLipp Avatar asked Feb 21 '13 15:02

ChrLipp


1 Answers

The only method you need to provide is the iterator() method. You then get all of the Groovy Object iteration methods (each(), find(), any(), every(), collect(), ...) for free!

like image 152
Dierk König Avatar answered Oct 19 '22 21:10

Dierk König