After coding Java for quite some time, I recently had the refreshing experience of coding in Groovy. It is fairly easy to transition to Groovy after coding in Java, and best of all compiled Groovy code will run using the same JVM that you use for Java.
Java code has a lot of rules and syntax that may not always be needed from a specification standpoint in order to accomplish our goals. For instance, why do we need so many of the following when we code?
Compare the following examples that print Hello Associates to the screen in both languages.
public class Example {
public static void main(String[] args) {
System.out.println("Hello Associates!");
}
}
println "Hello Associates!"
Groovy provides a number of mechanisms to assist in making your code more readable. A few of these are as follows.
Please feel free to look at the following application code that creates three objects and prints a comparison of the objects. The Groovy implementation definitely has some benefits of enabling a servicing developer to troubleshoot fewer lines of code if a problem is to be found.
public class AllowedValue {
public static void main(String[] args) {
AllowedValueTO firstTO = new AllowedValueTO("12", "Auto");
AllowedValueTO secondTO = new AllowedValueTO("12", "Auto");
AllowedValueTO thirdTO = new AllowedValueTO("13", "Auto ");
System.out.println(firstTO.equals(secondTO));
System.out.println(firstTO.equals(thirdTO));
}
}
class AllowedValueTO {
private String key;
private String value;
public AllowedValueTO(String key, String value) {
this.key = key;
this.value = value;
}
public String getKey() {
return key;
}
public String getValue() {
return value;
}
}
firstTO = new AllowedValueTO("12","Auto")
secondTO = new AllowedValueTO("12","Auto")
thirdTO = new AllowedValueTO("13","Auto ")
println firstTO == secondTO
println firstTO == thirdTO
@Immutable class AllowedValueTO {
String key
String value
}
Groovy can provide simplifications beyond defining simple objects, comparing them, and printing them. Let’s now look at an a collection of objects.
Groovy allows us to remove much of the noise found in Java and other languages. For example, one can do the following in Groovy.
import java.util.ArrayList;
import java.util.List;
public class ClientList {
private List<String> clientIds;
public ClientList() {
clientIds = new ArrayList<String>();
}
public static void main(String[] args) {
ClientList cList = new ClientList();
cList.add("CLIENT_ID_1");
cList.add("CLIENT_ID_2");
System.out.print(" "+cList.clientIds.getClass()+" : ");
System.out.println(cList.toString());
}
public boolean add(Object obj) {
return clientIds.add((String)obj);
}
public String toString() {
return ""+clientIds.getClass() + " : "+clientIds.toString();
}
}
class ClientList {
@Delegate List clientIds = []
static main(args) {
def cList = new ClientList();
cList.add "CLIENT_ID_1"
cList.add "CLIENT_ID_2"
println ""+cList.clientIds.class +" : "+cList
}
}
Groovy also provides the following nifty features!
====Additional Resources==== For more information, please feel free to consult any of the following resources.
[[http://groovy.codehaus.org | Groovy on the Web]] |
[[http://www.ibm.com/developerworks/java/library/j-pg08259.html | Groovy and Delegates]] |
[[http://en.wikipedia.org/wiki/Groovy_%28programming_language%29 | Groovy on Wikipedia]] |
[[http://groovy.codehaus.org/Eclipse+Plugin | Groovy Eclipse Plug-In]] |