This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Groovy and the fly likes Turn this Java into Groovy! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Languages » Groovy
Bookmark "Turn this Java into Groovy!" Watch "Turn this Java into Groovy!" New topic
Author

Turn this Java into Groovy!

Matthew Taylor
Rancher

Joined: Jun 13, 2004
Posts: 110
If you have some Java code, and you are interested in seeing what it would look like as Groovy code, post it here! If you think want to take a shot at translating it yourself, fire up your groovy console and go for it!

I'll start off with a simple example of reading a file into a String and printing it to stdout.

JAVA:


GROOVY:



[ August 31, 2008: Message edited by: Matthew Taylor ]

Grails Consultant
http://dangertree.net
Marc Peabody
pie sneak
Sheriff

Joined: Feb 05, 2003
Posts: 4727

This is a great idea. I'd set up an example myself, but that means having to write messy Java code.


A good workman is known by his tools.
Matthew Taylor
Rancher

Joined: Jun 13, 2004
Posts: 110
Here is a regular expression example that matches a zip code.

JAVA (from java2s.com):



GROOVY:



Both print this:



Aside from the obvious regex pattern matching simplicity in the Groovy code, you might also notice that you can call collection methods directly on a collection declaration:



This prints each number in the array without even instantiating a variable.

And here is another piece of syntactic sugar:



This will print the numbers 1 through 10 out to the screen, because 'times()' is a method on the Groovy Integer class that takes a Closure as an argument. Pretty awesome stuff.

[ September 16, 2008: Message edited by: Matthew Taylor ]
[ September 16, 2008: Message edited by: Matthew Taylor ]
Marc Peabody
pie sneak
Sheriff

Joined: Feb 05, 2003
Posts: 4727

Here's a couple more variations.

You could collect all the messages and then print them each.


Or what I like better, split our collection up and then print each group separately.
Matthew Taylor
Rancher

Joined: Jun 13, 2004
Posts: 110
I like your second variation. Those collection operators like "-" are sweet.
Matthew Taylor
Rancher

Joined: Jun 13, 2004
Posts: 110
I've been wanting to do an XML example, but all the Java code that deals with XML is so horrible that I've been putting it off. So I finally decided to omit the Java example, in the hopes that you've probably got some experience with Java XML parsing, and you know just how tedious and nasty it can be.

JAVA: See various nasty examples here

GROOVY:


Resulting in this printout:



I'm using the same xml file as my first post's example, and as you can see there is not much involved with the parsing. You just create a new parser and send it the text. Very easy.

And you can see how efficient it is to access the parsed data. The resulting 'catalog' object is a groovy.util.Node. Each node is a collection of Nodes with some nice convenience methods. You can access attributes by using the '@' symbol (the book ids in the printout are from attributes).

Another useful thing I've done is chained a couple of collection methods together to help me format my "England" search results. I first did a 'findAll' with a closure that returns true only if the book description text contains 'England', then I'm calling 'collect' on the results to convert each found Node into a String. This is the same thing as doing this:


Maybe next example will be an XML writer, which is also surprisingly easy.
[ September 18, 2008: Message edited by: Matthew Taylor ]
Marc Peabody
pie sneak
Sheriff

Joined: Feb 05, 2003
Posts: 4727

Originally posted by Matthew Taylor:
Maybe next example will be an XML writer, which is also surprisingly easy.

Repackaging the xml file using only the found books:


Creates:
adwin wijaya
Greenhorn

Joined: Oct 01, 2008
Posts: 4
groovy is great ..

thanks for the tutorial .. i am learning groovy at the moment .. preparing for using it together with grails.
 
 
subject: Turn this Java into Groovy!