Monday, November 5, 2012

Groovy Enums

Found a neat trick for matching a String back to its enum. For instance if we have the following enum

public enum Status {
INBOX { public String toString() {"Inbox"}},
NEXT { public String toString() {"Next Action"}},
SOMEDAY { public String toString() {"Someday"}} }
 So if you had a String and wanted to match the enum it is as easy as

def a = "Inbox" as Status

Wednesday, August 29, 2012

Groovy vs Java

Performance Test: Groovy 2.0 vs. Java

Groovy is catching up performance wise. Though speed has never really been it's goal.
I still maintain pure speed is not that important, and you can always drop down to pure Java.

Tuesday, July 17, 2012

Vecor Clock

Was reading a new Guillame Laforge post about Algorithms for collaborative editing. Got me reading about vector clocks which I knew nothing about. It is a really nice idea.

I would like to write a simple groovy implementation.

Of course reading more lead me to great articles further explaining:
http://basho.com/blog/technical/2010/01/29/why-vector-clocks-are-easy/
http://basho.com/blog/technical/2010/04/05/why-vector-clocks-are-hard/

Which led me to an interesting open source distributed database. A Grails plugin exists, not sure how mature it is

http://wiki.basho.com/Riak.html




Tuesday, November 23, 2010

Case Insensitive withCriteria

I found this easy way to do a case insensitive query on the DB using with Criteria. Unfortunately Grails documentation does not mention this, but remember you are using Hibernate. So using .ignoreCase() on the eq works perfectly.


 personList = Person.withCriteria {
  if (params.firstName)
   eq('firstName', params.firstName).ignoreCase()
  if (params.lastName)
   eq('lastName', params.lastName).ignoreCase()
  if (params.login)
   eq('login', params.login).ignoreCase()
  if (params.personHashId)
   like('personHashId', params.personHashId)
  maxResults(Constants.SEARCH_MAX_RESULTS)
 }

Wednesday, November 3, 2010

Elvis Operator

Is really neat and not used enough. Basically it is a modified ternary.
Here is and example for returning a sensible value if an expressions resolves to null (or false).
    def a = b ?: "No Value"
In that example a should equal b unless b is null or false, then it would be assigned "No Value"

Monday, October 18, 2010

Service and DB connection pools

(From Kwang Ho-Lee's investigations)

When using a Grails service that is transactional (default) and it uses the DB, the service will try and get a connection to the DB from the pool each time it is called.
So it is a bad idea to have code in a controller that calls a service in a loop. The loop should really be in the service, this will prevent multiple connections being made, plus it makes better design sense.

Wednesday, October 13, 2010

Time durations

Some code that shows durations and works nicely as a timer
    import groovy.time.*

    def timeStart = new Date()
    // Some code you want to time
    def timeStop = new Date()
    TimeDuration duration = TimeCategory.minus(timeStop, timeStart)
    println duration
This will give you the time in hours, minutes, seconds