• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Turn this Java into Groovy!

 
Rancher
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
pie sneak
Posts: 4727
Mac VI Editor Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a great idea. I'd set up an example myself, but that means having to write messy Java code.
 
Matthew Taylor
Rancher
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4727
Mac VI Editor Ruby
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I like your second variation. Those collection operators like "-" are sweet.
 
Matthew Taylor
Rancher
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4727
Mac VI Editor Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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:
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
groovy is great ..

thanks for the tutorial .. i am learning groovy at the moment .. preparing for using it together with grails.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic