Groovy Goodness: Closure as a Class
When we write Groovy code there is a big chance we also write some closures. If we are working with collections for example and use the each
, collect
or find
methods we use closures as arguments for these methods. We can assign closures to variables and use the variable name to reference to closure. But we can also create a subclass of the Closure
class to implement a closure. Then we use an instance of the new closure class wherever a closure can be used.
To write a closure as a class we must subclass Closure
and implement a method with the name doCall
. The method can accept arbitrary arguments and the return type can be defined by us. So we are not overriding a method doCall
from the superclass Closure
. But Groovy will look for a method with the name doCall
to execute the closure logic and internally use methods from the Closure
superclass.
In the following sample we write a very simple closure as a class to check if an object is a number. Then we use an instance of the class with the findAll
method for a collection of objects:
class IsNumber extends Closure /* return type for closure as generic type */ {
IsNumber() {
super(null)
}
/**
* Implementation of closure.
*/
Boolean doCall(final Object value) {
// Check if value is a number, if so
// return true, otherwise false.
value in Number
}
}
def list = ['a', 100, 'Groovy', 1, 8, 42.0, true]
def numbers = list.findAll(new IsNumber())
assert numbers == [100, 1, 8, 42.0]
Code written with Groovy 2.3.7.