The Groovy in Java
by
, June 22nd, 2011 at 09:53 PM (9736 Views)
Although scripting languages seem like dime a dozen, there is always that learning curve to become familiar (let alone an expert) with a new language. For those who are more familiar with java - enter groovy. Groovy is a powerful higher level language based upon java, making certain tasks trivial to accomplish relative to the low level java equivalent. The power of groovy comes into play in its ability to plug-into java, and be plugged into from java. Groovy can access class files and libraries, can be compiled into class files and libraries, or simply used for quick and dirty one time operations.
Groovy is a simple language to learn if one knows the basics of java. Rather than reiterate what already exists in tutorials, I will demonstrate the ease of using groovy relative to the equivalent in java. Note that the demonstrations below are trivial examples which only print to the command line - typically one wishes to do more than print to the command line.
Reading Files:
Below are 2 examples which read a file and print the output to the command line
In java
BufferedReader reader = null; try{ reader = new BufferedReader(new FileReader("filePath")); while ( ( line = reader.readLine() ) != null ){ System.out.println(line); } }catch(IOException e){ throw e; }finally{ if ( reader != null ){ try{reader.close;}catch(Exception e){} } }
In groovy
f = new java.io.File("filePath"); lines = f.readLines(); println("loop"); for ( int i = 0; i < lines.size(); i++ ){ println lines[i] }
Both methods accomplish the same task, but the differences should be obvious (in particular, the number of lines).
Reading a web page:
Java
try{ URL url = new URL(myWebpageURL); URLConnection conn = url.openConnection(); BufferedReader reader = null; try{ reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); while ( ( line = reader.readLine() ) != null ){ System.out.println(line); } }catch(IOException e){ throw e; }finally{ if ( reader != null ){ try{reader.close;}catch(Exception e){} } } }catch(Exception e){ e.printStackTrace(); }
Groovy
myURL = new java.net.URL(myWebpageURL) println(myURL.getText())
Hmmm…2 lines versus, well, I lost count. Of course, in both of the situation above, writing utility functions and libraries would greatly facilitate the process, reducing repeated calls down to a single method call. Often times however, these libraries can be cumbersome when managing across multiple projects - necessitating imports, classpath management, and making library updates difficult.
Data Structures
How about data structures? Maps and Lists are a breeze to create in groovy
myMap = [0:"zero", 1:"One", 2:"Two"] myList = [0,1,2,3] println myMap[0]//prints zero println myMap[myList[0]]//prints zero
Function Pointers
Java lacks what those with a C background would call function pointers - the ability to pass references to a particular method. These operations are allowed in groovy through groovy's closure capability
f1 = {println "first"} f2 = {println "second"} x = 0; def fun if ( x == 0 ){ fun = f1 }else{ fun = f2 } fun()
User Interfaces
Swing user interfaces? The code below creates a very simple JFrame with a JButton that prints to the command line when pressed.
import groovy.swing.SwingBuilder sb = new SwingBuilder() frame = sb.frame(title:"Groovy Frame"){ button(text:"Press Me", actionPerformed:{ println "I've been pressed" } ) } frame.pack() frame.setVisible(true)
The above shows just a taste of what groovy has to offer. Additional features include ease regular expressions (together with with perl like syntax), classes, threads, SQL connections, and just about anything that can be done in java.
While there are many other languages which provide similar ease of features (perl, php, etc…), groovy provides advantages to a programmer more comfortable in java, and allows one to plug into pre-existing java libraries. Groovy provides very convenient regular expression utilities similar to Perl, a runtime console to quickly interpret short snippets of code, java like exceptions (which provide a wealth of details regarding compile and/or runtime errors), and much more. The high level implementation of Groovy allows projects - both large and small - to be developed quickly without the need for lower level code management.
Links:
Groovy - Home
IBM: Feeling Groovy