Understanding Groovy Categories
Have you ever used Groovy Categories? Do you even know what its stands for?
Groovy Categories is a great feature from Groovy (inherted from Objective-C) that allows you to have some kind of “extended” control on a class. The category just “add” some more funcionality to your class when you want. It’s different from creating another class that extends from the first one, this approach holds a little more functionality-oriented talk…
Categories is nothing more than a Groovy class that you’ve implemented before that effectly adds your functionality. The syntax is very simple and all you have to do, is tell to the runtime environment you’re now using the category.
use (the category class you want to use) {
//all your code here can access the category funcionality
}
//back to normal code
That’s it. Groovy Core comes with some Categories (ServletCategory, DOMCategory and TimeCategory). The one we use more is the TimeCategory, that adds some calculation methods for us.
Let’s imagine we want to add some days in our current date, using Java you remember how you should do, don’t you?
Get a Calendar Instance, retrieve this calendar’s date, add the ammount of days using the strangest and creepiest method ever and then get the calendar’s date again (actually, get only its time and create a date with it):
//creepiest method myCalendarInstance.add(Calendar.DAY_OF_MONTH, 10);
You can see in the TimeCategory’s javadoc (link above) that it defines some methods like “getHours(), getDays(), getMinutes()”. This methods we don’t have in some usual Date class, but when we use the TimeCategory object, they just get available for us. The example below show us what should happen when we use the TimeCategory:
Date now = new Date() println now //just show when am Iuse(org.codehaus.groovy.runtime.TimeCategory) { now = now + 15.days } println now //shows us when we'll be in 15 days!
This will be our output after running this small script:
Sat May 02 02:25:57 BRT 2009 Sun May 17 02:25:57 BRT 2009
That’s it, try the code above and the others methods the TimeCategory gives to you: years, months, hours, minutes and seconds.
Thanks! Be the first to know when I publish some interesting article signing up my feed and following me on twitter!


































































Comments(5)
Hi, blog.lucastex.com – da best. Keep it going!
That’s so similar to ‘with’ that got me confused for a little while.
Groovy is awesome. Thanks for the tip LT.
In the past categories in groovy could only be static methods, which causes threading issues.
Do you know if anything’s changed? What are the implications of using categories in a muti-threaded environment?
@Raphael
with modifier only uses the proper class methods, in the “with” post, all method used inside the { } are available in the class, and with categories, the method show are not in the Date class, but in the Category one… Got it?
@Dan,
You’re right, you’ll only get Categories working with static methods. Remember, it applies new methods to your class, but ‘helper’, ‘transformation’, or even light methods, not ‘core’ methods (imo, the ones that should care on your instance objects).