Groovy - Basics

Yesterday, I was reading a bit about scripting languages and started playing with ‘Groovy’, which is an alternative to ‘Java’. You can say that it’s a bit higher-level language than Java, as it is built on Java libraries (or classes) and provides one more abstraction layer. Groovy, which is a ‘Dynamic Language’ (dynamically typed) was built to provide a scripting language for the Java platform, along the lines of Python, Ruby and Perl. Below are some imp points:

1) The Groovy compiler can be used to generate ‘Java Bytecode’, which can be integrated with any other java module/project.

2) Most of the Java code is also a valid syntax for Groovy.

3) Groovy is dynamically typed, whereas Java is static and strongly typed. Dynamically typed languages have lesser code, but more execution time.

4) Groovy also provides special XML processing through the classes XmlParser, XmlSlurper, DOMCategory.

Apart from these, Groovy has features like ‘Dynamic Typing’, ‘Closures’ and ‘builders’. Also, it’s pretty useful for testing as it supports unit-testing and mocking out-of-the-box.
After doing a bit of reading and coding some basic scripts, I tried to code a Groovy solution for the Java code that I wrote for solving the “Google’s Billboard Puzzle” (from my other blog). It’s for calculating “Euler’s Number (e)” , which is pretty basic and simple code. Here’s the code:

BigDecimal e = new BigDecimal(1.0G)
BigDecimal temp = new BigDecimal(1.0G)

for(i in 1..100) {
temp *= i
e += new BigDecimal(”1″).divide(temp, new java.math.MathContext(10000))

println(”e : ” + e)
println(”length of e calculated : ” + (e+”").length())

As you can see, the ‘Groovy’ script also has ‘Java’ code in it (and is valid). Since, Groovy is a dynamically typed language, it took more time than the Java code took, to produce the solution.

Here’s one more script, which outputs the ‘hashcode’ of a string:

def str = “abc”
int hashcode = 1;
int len = str.length()

for(i in 0..(len-1)) {
hashcode = 31*hashcode + str.charAt(i)

println(”hashcode : ” + hashcode)

The above code is a naive approach to calculate the ‘hashcode’ and is not a cryptographically secure function. Its based on the formula “s[0] * 31n − 1 + s[1] * 31n − 2 + … + s[n − 1]“, which is actually ‘Rabin-Karp Algorithm (RK)’. One might be wondering why there in ‘31′ in that Math expression. Its because Java’s int is ‘32′ bit, in length.

Groovy can also be used to test web applications, webservices, et al. Groovy can be used to test webservices in the following way:

* Act as a normal webservice client
* Use ‘WebTest’
* Use ‘SoapUI’

Ok..I decided to code a webservice client and googled for ‘Free Publicly Available Webservices’ and found a couple of websites with publicly available webservices. I picked the CDYNE’’s ‘Profanity Filter’ webservice (which filters words that are considered profanity) and provided the WSDL url to the Groovy script. Here’s the script (minimalistic):


def proxy = new WSClient(””, this.class.classLoader)

def result = proxy.SimpleProfanityFilter(”You crappy bugga!”)

For the above script to run on ‘Groovy Console’, you have to copy the ‘groovyws’ jar file into Groovy’s ‘lib’ directory.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.