Groovy Goodness: Using Implicit call() Method

In Groovy we can invoke an implicit call() method on a Groovy object. We can leave out the call method name and just use (). We can use meta programming to add an implementation for the call() method to a class. In the following example script we add an implementation for the call() method with a single parameter to the String class. The implementation returns the element found at the range specified by the argument when we invoke the method:

Continue reading

Groovy Goodness: Boolean Implications

Since Groovy 1.8.3 we can use the implies() method on Boolean types. The implies() method implements a logical implication. This means that if we have two Boolean variables A and B, that if A is true, than B is true. So if A is true than it is implied B is true as well. If A is false than B can be either true or false. We could rewrite the implication as !A or B.

(Code written with Groovy 2.0.4)

Original article

Why Time Is No Measure For Progress

One of the questions that Project Managers always pop up during projects is: “how many days will it take to finish this work?”.

This is a very natural and sensible question. You want to know when a project will be finished, so people can have an expectancy of the needed budget, what functionality is in it and when it will be finished.

However, answering the question is more difficult than traditionally is assumed. Continue reading

Gradle Goodness: Customize IDEA Project File Generation

With the Gradle IDEA plugin we can generate JetBrains IntelliJ IDEA project files. The plugin uses defaults from our project to generate the files. If we also apply the Java plugin to our project then the Java settings for the project files are generated. We can customize the file generation in several ways. The most low level method is using the withXml hook. With this hook we have access to the XML before the file is written to disk. Here we can add or change XML elements and attribute values.

We use a closure as argument for the withXml hook and Gradle adds a XmlProvider object as argument. The easiest way to manipulate the XML is getting a groovy.util.Node from the XmlProvider. We also can get a DOM Element or StringBuilder to work with.

Continue reading

To Roo or not to Roo…

Recently I’ve been looking into Spring Roo to find ways to speed-up software development as well as reducing plumbing code.
In this blog post I will highlight some of the advantages and disadvantages of Spring Roo which I stumbled upon. So don’t expect a full featured introduction or tutorial; plenty of those already exist on the internet ;-)

A unique approach on code completion
Spring Roo takes on a different approach to code generation compared to other solutions.
Instead of generating additional .java files (through the “Generation Gap Pattern”) it generates so-called AspectJ inter-type declaration (ITD) .aj source files.
Each generated inter-type declaration (ITD) type will “weave in” structural changes to its target .java file; for example to add new methods (i.e. getter/setter methods) or an “implements …” clause.

Continue reading

Quick Tip: Installing and switching Grails versions on OS X using Homebrew

I’m using Homebrew “The missing package manager for OS X” for a while now but when it come’s to Grails I still install it manually: until now!

Installing the most recent version of Grails using Brew is straight forward:

But the main reason for me to install Grails manually is the fact that I ‘d like to be able to install multiple versions and select a specific version for the project I’m working on.
I just found out that that’s also very easy with Homebrew.

Continue reading

Google Guava Goodness: Converting Between ASCII Case Conventions

The Google Guava libraries contains useful utility classes and methods. If we want to convert between ASCII case conventions we can use the CaseFormat class. The class defines constants for upper and lower case CamelCase, upper and lower case hyphenated and upper case underscore. This means we can convert UPPER_VALUE to upper-value with a simple line of code.

(Sample with Google Guava version 13.0.1)

Original article

Google Guava Goodness: Joining Collection Elements

Sometimes I work on Java projects and then I miss the expressiveness of the Groovy language. For example to join elements in a list with a given separator to get a String value we only have to use the following code:

When I cannot use Groovy then the Google Guava library has many methods and classes to make working with Java code easier. Let’s see how we can use the Joiner class to join elements in a list with a separator:

Continue reading

Gradle Goodness: Using Objects for Version

One of the great things of Gradle is that the build scripts are code. We can use all the features of the Groovy language, we can refactor our build scripts to make them more maintainable, we can use variables and properties to define values and much more, just like our application code. In this post we see how we can create a class to define a version in our build script.

To set the version of a Gradle project we only have to assign a value to the version property. Normally we use a String value, but we can also assign an object. Gradle will use the toString() method of the object to get the String value for a version.

Continue reading

Google Guava Goodness: Matching All Elements in a Collection

The Google Guava libraries has many useful classes and methods. Normally I write code in Groovy and I am used to working with collections in an intuitive way. But sometimes I need to work with Java on my project and then the Google Guava libraries are a great alternative.

Suppose I want to check if all elements in a collection apply to a certain condition. In Groovy I would write this:

Continue reading