Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 4.0

if - else statement

Groovy supports the usual if - else syntax from Java

Code Block
def x = false
def y = false

if ( !x ) {
    x = true
}

assert x == true

if ( x ) {
    x = false
} else {
    y = true
}

assert x == y

Groovy also supports the normal Java "nested" if then else if syntax:

Code Block
if ( ... ) {
...
} else if (...) {
...
} else {
...
}

ternary operator

Groovy also supports the ternary operator:

Code Block
def y = 5
def x = (y > 1) ? "worked" : "failed"
assert x == "worked"

See also: the elvis operator

switch statement

The switch statement in Groovy is backwards compatible with Java code; so you can fall through cases sharing the same code for multiple matches.

One difference though is that the Groovy switch statement can handle any kind of switch value and different kinds of matching can be performed.

Code Block
def x = 1.23
def result = ""

switch ( x ) {
    case "foo":
        result = "found foo"
        // lets fall through

    case "bar":
        result += "bar"

    case [4, 5, 6, 'inList']:
        result = "list"
        break

    case 12..30:
        result = "range"
        break

    case Integer:
        result = "integer"
        break

    case Number:
        result = "number"
        break

    default:
        result = "default"
}

assert result == "number"

Switch supports the following kinds of comparisons

  • Class case values matches if the switchValue is an instanceof the class
  • Regular expression case value matches if the string of the switchValue matches the regex
  • Collection case value matches if the switchValue is contained in the collection. This also includes ranges too (since they are Lists)
  • if none of the above are used then the case value matches if the case value equals the switch value

default: must go at the end of the switch/case as Jochen outlined in this thread from the groovy-user mailing list which Jochen states:

"because a Java switch/Case does not work like a Groovy switch/case. In Java a case can take only int compatible constants, in Groovy it can take expressions. In Java all cases share a scope, in Groovy each case has its own scope. In Groovy we call the isCase method, in Java it has to be a number we switch with. If we for example use a closure as case, then this might cause side effects. There are cases where we can let them behave the same and usually when using the java version you won't see a difference in Groovy besides the placement and logic of default."

So, while in Java the default can be placed anywhere in the switch/case, the default in Groovy is used more as an else than assigning a default case.

How switch works

The case statement performs a match on the case value using the isCase(switchValue) method, which defaults to call equals(switchValue) but has been overloaded for various types like Class or regex etc.

So you could create your own kind of matcher class and add an isCase(switchValue) method to provide your own kind of matching.