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.

@Category(Integer)
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
uday@intelligrape.com
https://twitter.com/meudaypratap
http://in.linkedin.com/in/meudaypratap

 

Hope it helps
Uday Pratap Singh
uday@intelligrape.com
http://in.linkedin.com/in/meudaypratap

2 Responses to Groovy Category Annotation

  1. Pingback: Using Category in groovy - Grails Info

  2. It’s amazing to visit tɦis web paɡe and reading tɦe views of all colleagues concerning thiѕ piece ߋf writing, ԝhile I am also keen of getting knowledge.

Leave a Reply

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

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>