Thursday, January 21, 2010

Groovy: Switch on Steroids

I have blogged before regarding Groovy's support for switching on String. Groovy can switch on much more than just Strings (and the same types as Java) and I demonstrate that briefly here.

Groovy's switch statement will use a method implemented with the name "isCase" to determine if a particular switch option is matched. This means that custom objects are "switchable" in Groovy. For the simple example in this blog post, I'll use the Java class State.java:

State.java

package dustin.examples;

public class State
{
private String stateName;

public State(final String newStateName)
{
this.stateName = newStateName;
}

/**
* Method to be used by Groovy's switch implicitly when an instance of this
* class is switched on.
*
* @param compareString String passed via case to me to be compared to my
* state name.
*/
public boolean isCase(final String compareString)
{
return compareString != null ? compareString.equals(this.stateName) : false;
}

@Override
public String toString()
{
return this.stateName;
}
}


The simple standard Java class shown above implements an isCase method that will allow Groovy to switch on it. The following Groovy script uses this class and is able to successfully switch on the instance of State.


#!/usr/bin/env groovy

import dustin.examples.State
def state = new State("Colorado")
print "The motto for the state of ${state} is '"
switch (state)
{
case "Alabama" : print "Audemus jura nostra defendere"
break
case "Alaska" : print "North to the future"
break
case "Arizona" : print "Ditat Deus"
break
case "Arkansas" : print "Regnat populus"
break
case "California" : print "Eureka"
break
case "Colorado" : print "Nil sine numine"
break
case "Connecticut" : print "Qui transtulit sustinet"
break
default : print "<<State ${state} not found.>>"
}
println "'"


The output in the next screen snapshot indicates that the Groovy script is able to successfully switch on an instance of a State object.



The beauty of this ability to have classes be "switchable" based on the implementation of an isCase() method is that it allows more concise syntax in situations that otherwise might have required lengthy if/else if/else constructs. It's prefable to avoid such constructs completely, but sometimes we run into them and the Groovy switch statement makes them less tedious.

It is entirely possible with the Groovy switch to have multiple switch options match the specified conditions. Therefore, it is important to list the case statements in order of which match is preferred because the first match will be the one executed. The break keyword is used in Groovy's switch as it is in Java.

There is much more power in what the Groovy switch supports. Some posts that cover this power include Groovy Goodness: The Switch Statement, Groovy, let me count the ways in which I love thee, and the Groovy documentation.

1 comment:

daniel said...

Yes i am really agree with all of your knowledge....and its really gr8 you have added coding here.....