Skip to content

Instantly share code, notes, and snippets.

@jonatasemidio
Created May 21, 2015 11:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonatasemidio/6999c427e969b82e9e14 to your computer and use it in GitHub Desktop.
Save jonatasemidio/6999c427e969b82e9e14 to your computer and use it in GitHub Desktop.
O poder do Switch do Groovy
def testSwitch(val) {
def result
switch (val) {
case ~/^Switch.*Groovy$/:
result = 'Pattern match'
break
case BigInteger:
result = 'Class isInstance'
break
case 60..90:
result = 'Range contains'
break
case [21, 'test', 9.12]:
result = 'List contains'
break
case 42.056:
result = 'Object equals'
break
case { it instanceof Integer && it < 50 }:
result = 'Closure boolean'
break
default:
result = 'Default'
break
}
result
}
assert 'Pattern match' == testSwitch("Switch to Groovy")
assert 'Class isInstance' == testSwitch(42G)
assert 'Range contains' == testSwitch(70)
assert 'List contains' == testSwitch('test')
assert 'Object equals' == testSwitch(42.056)
assert 'Closure boolean' == testSwitch(20)
assert 'Default' == testSwitch('default')
@jonatasemidio
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment