Return to Snippet

Revision: 12367
at March 12, 2009 10:59 by narkisr


Initial Code
def car = "Patriot"
	
def manufacturer = match(car) {
  when "Focus",     "Ford"
  when "Navigator", "Lincoln"
  when "Camry",     "Toyota"
  when "Civic",     "Honda"
  when "Patriot",   "Jeep"
  when "Jetta",     "VW"
  when "Ceyene",    "Porsche"
  when "Outback",   "Subaru"
  when "520i",      "BMW"
  when "Tundra",    "Nissan"
  otherwise         "Unknown"
}
	
println "The $car is made by $manufacturer"
	
def match(obj, closure) {
    closure.subject = obj
    closure.when = { value, result ->
        if (value == subject)
            throw new MatchResultException(result: result)
    }
    closure.otherwise = { return it }
    closure.resolveStrategy = Closure.DELEGATE_FIRST
    try {
        closure()
        closure.otherwise()
    } catch (MatchResultException r) {
        r.result
    }
}
	
class MatchResultException extends RuntimeException {
    def result
}

Initial URL

                                

Initial Description
A nice example of utilizing Groovy closures in order to create a when statement in Groovy (http://www.transentia.com.au/flatpress/?x=entry:entry090311-222726).

Initial Title
Creating when construct in Groovy using closures

Initial Tags
groovy

Initial Language
Groovy