Groovy Category Annotation

31 / Jul / 2011 by Uday Pratap Singh 1 comments

Annotations really provides a whole new view of programming things. Groovy also provides some of its own annotations one of them is Category. Lets take an example of using it. We create a IntegerUtil class and annotate it with Category.

[java]
@Category(Integer)
class IntegerUtil {
List<Integer> multiples(Integer upto) {
(1..upto).collect {this * it}
}
}
[/java]

Now the above code made your class any other groovy category class for example Time Category. Now you can use these methods as follows -:
[java]
List<Integer> multiples
use(IntegerUtil) {
multiples = 2.multiples(10)
}
println multiples // Output -: [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
[/java]

It really helps when you want some more helper methods on already existing classes.

Hope it helps
Uday Pratap Singh
uday@intelligrape.com

FOUND THIS USEFUL? SHARE IT

comments (1 “Groovy Category Annotation”)

  1. Icha

    DavidYou may want to look at the other job trends posts, in the Job Trends cogetary. Learning scripting languages is a good idea, but moving completely away from Java may not be a good thing. Do some research on your general area or type of development to determine which language may have the best job prospects. Thanks for reading!

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *