The Groovy truth in some cases is different from Java, so you need to make sure that Groovy gives you what you expect.
| Truth examples | // Boolean
assert true
assert !false
// Matcher
assert ('a' =~ /a/)
assert !('a' =~ /b/)
// Collection
assert [1]
assert ![] // empty List = false
// Map
assert [1:'one']
assert ![:] // empty Map = false
// String
assert 'a'
assert !'' // empty String = false
// Number
assert 1
assert 3.5
assert !0 // 0 = false
// None of the above
assert new Object()
assert !null // null = false |
I have already covered a number of these back in my basics section.
| Conditional examples | if( true ) {
println "true"
}
if( true )
println true
def age = 35
if( age >= 35 ){
println "can run for president"
}
if( false ) {
println "true"
} else {
println "false"
}
def yourage = 18
if( yourage >= 21 ) {
println "buy beer"
} else {
println "no beer for you"
}
def someage = 37
if( someage >= 21 && someage < 35 ) {
println "buy some beer"
} else if( someage >= 35 ) {
println "run for president"
} else {
println "under 21..."
}
// -------------------------------------------------------
// ternary operator (expression) ? true : false
def name = 'Paul'
def isit = (name.toLowerCase() == 'PAUL') ? 'YES' : 'NO'
println isit
def msg
def output = (msg != null) ? msg : 'default message...'
def elvisOutput = msg ?: 'default message...' // shortcut for above
println msg
println output
println elvisOutput
// -------------------------------------------------------
def num = 12
switch( num ) {
case 1:
println "1"
break
case 2:
println "2"
break
case 1..3:
println "in range 1..3"
break
case [1,2,12]:
println "num is in list [1,2,12]"
break
case Integer:
println "num is an Integer"
break
case Float:
println "num is a float"
break
default:
println "default..."
}
// in
def validAges = 18..35
def someAge = 19
println someAge in validAges |
I have already covered a number of these back in my basics section plus I have covered Closures as well.
| Looping examples | // while
List numbers = [1,2,3]
while( numbers ) {
// do something
numbers.remove(0)
}
assert numbers == [] // test above remove
// for loops
List nums = [1,2,3]
for( Integer i in 1..10 ) { // you can specify the data type
println i
}
for( i in 1..5 ) { // you don't have to specify the data type, remember its dynamic
// do stuff
}
// return/break/continue
String getFoo() {
"foo" // last line of any method is automatically returned, no need for return
}
Integer a = 1
while( true ) { // infinite loop
a++
break
}
assert a == 2
for( String s in 'a'..'z' ){ // use a range
if( s == 'a') continue // one-line if statement
println s
if( s > 'b' ) break
} |
Exception handling is very similar to Java, you use the try-catch block to try a piece of code and if it fails then the appropriate catch clause will be invoked, as with Java you can also throw your own exceptions.
| Exception examples | def foo() { // you don't need 'throws Exception' as part of the signature in Groovy
// do stuff
throw new Exception("Foo Exception")
}
List log = []
try {
foo()
} catch( Exception e ) {
log << e.message // same as e.getMessage()
} finally {
log << 'finally' // will always run regardless
}
println log
// Java 7 introduced a multi catch syntax
try {
// do stuff
} catch( FileNotFoundException | NullPointerException e ) { // notice only one |
println e.class.name
println e.message
} |