Using propertyMissing to enhance Date (in Groovy)

In my previous post I had a go at dates and ranges in Groovy. I wasn't to enthusiastic about the fact that it felt a bit verbose and Java-ish. So I took the opportunity to have a go at the new meta-programming stuff in Groovy >= 1.1beta3.

Say I want to be able to do something like this (the properties being Calendar fields):

GROOVY:
  1. d = new Date()
  2.  
  3. println d.dayOfWeek
  4. println d.hourOfDay
  5. println "$d.hourOfDay:$d.minute"
  6.  
  7. etc...

For me, this seemed like a good case to implement using methodMissing and/or propertyMissing. I came up with the following:

GROOVY:
  1. Date.metaClass.propertyMissing = { String name ->
  2.     wrd = name[0].toUpperCase() + name[1..<name.size()]  // dayOfWeek ==> DayOfWeek
  3.     split = wrd =~ /([A-Z][a-z]+)// collect words
  4.     par = split.collect{it}?.join('_').toUpperCase()  // ["Day","Of","Week"] ==> DAY_OF_WEEK
  5.  
  6.     try{
  7.         con = Calendar."$par"  // see if the field exist in calendar, throws MissingPropertyException if not
  8.         Date.metaClass."get$wrd" <<{  // register the 'get' method ==> getDayOfWeek()
  9.             cal = Calendar.instance  // obtain a Calendar instance                           
  10.             cal.time = delegate  // set the time to the current instance
  11.             return  cal.get(con)  // get the field
  12.         }
  13.     }
  14.     catch(MissingPropertyException mpe){
  15.         throw new MissingPropertyException(name, delegate.class) // wrap the exception
  16.     }
  17.  
  18.     delegate."get$wrd"()
  19. }

Performance-wise (tips, anyone?) this might not be the best solution (the documentation is actually a bit scarce in this point) but now I can remove the dreaded calendar object from my previous code:

GROOVY:
  1. sdf = new java.text.SimpleDateFormat("dd-MM-yyyy")
  2. bd_daan = sdf.parse("23-01-1980")   
  3. bd_peter = sdf.parse("18-01-1979")
  4.  
  5. (bd_peter..bd_daan).each{ d ->
  6.     if(d.dayOfWeek == Calendar.SUNDAY)
  7.         println sdf.format(d)
  8. }

Feels better, doesn't it? Now... what about adding a 'parse' and 'format' method to Date... next time ;)


0 Responses to “Using propertyMissing to enhance Date (in Groovy)”

  1. No Comments

Leave a Reply





About

Welcome to the weblog of Peter Maas. Here you'll find various posts related to stuff I like (like my kids and espresso) and stuff I do (like developing software).

JavaOne 2008 Pictures


golden_gate_warning_sign javaone2008 keynote Golden Gate smashmouth Rudie Stage being build in the nearby park javaone 2008 goodybag Greenland nearby hotel Tim Bray introducing the (J)Ruby panel Acme Anvile at CommunityOne Keynote Cable Car line Joshua Bloch at JavaOne2008 Okke en Rudie Scribbled Sun Logo Charles Nutter & Guillaume Laforge pub Moscone Center - JavaOne Hotel room Java + You on a cab
View more photos >

Categories



Meld u aan voor PayPal en begin direct met het accepteren van creditcardbetalingen.