Loading...

December 16, 2010

Groovy Goodness: Use Map in Switch Statement

Since Groovy 1.7.6 we can use a Map as a case in a switch statement. Groovy adds the isCase() method where the switch operand is used to find map elements. Groovy tries to find a key with the given switch operand in the Map and if it is found true is returned.

def testSwitch(val) {
    def result
    switch (val) {
        // New in Groovy 1.7.6: Map support.
        case [groovy: 'Rocks!', version: '1.7.6']:
            result = "Map contains key '$val'"
            break
        case 60..90:
            result = "Range contains $val"
            break
        case [21, 'test', 9.12]:
            result = "List contains '$val'"
            break
        default:
            result = 'Default'
            break
    }    
    result
}

assert testSwitch('groovy') == "Map contains key 'groovy'"
assert testSwitch(70) == 'Range contains 70'
assert testSwitch('test') == "List contains 'test'"
assert testSwitch('default') == 'Default'