It just takes 15 seconds. Send your
details and we’ll get back to you soon.

Groovy Category Annotation

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.

class IntegerUtil {
    List<Integer> multiples(Integer upto) {
        (1..upto).collect {this * it}

Now the above code made your class any other groovy category class for example Time Category. Now you can use these methods as follows -:

List<Integer> multiples
use(IntegerUtil) {
    multiples = 2.multiples(10)
println multiples  // Output -: [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]

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

Hope it helps
Uday Pratap Singh

One Response to Groovy Category Annotation

  1. Pingback: Using Category in groovy - Grails Info

Leave a Reply

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

+ 84 = 90

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>