A Little Groovy Goodness

Ever since attending the SpringOne 2GX conference, I’ve been intrigued by Groovy. I’ve finally had a bit of time to start working with it, and decided to share some of the niceties of Groovy. This post is a very simple example of the differences in coding between Java and Groovy, so expect to see more as time, and my knowledge permits.

The first thing I really liked about Groovy was the removal of explicit setters and getters. I’ve always hated the clutter, and especially since it makes it harder to see if any getter/setter is doing something ‘special’, or if there are methods that aren’t setters/getters.

So, a simple bean defined like so in Java to support easier setting of variables

public class Bean {
    private long numberOfCompanies;
    private long numberOfUsersPerCompany;
    private long numberOfSecureMessages;

    public long getNumberOfCompanies() {
        return numberOfCompanies;
    }
    public Bean setNumberOfCompanies(long numberOfCompanies) {
        this.numberOfCompanies = numberOfCompanies;
        return this;
    }

	// code snipped here
}

	//used like this
        growthRequest
            .setNumberOfCompanies(1L)
            .setNumberOfUsersPerCompany(1L)
            .setNumberOfSecureMessages(2L);

becomes this in Groovy

public class bean {
    long numberOfCompanies
    long numberOfUsersPerCompany
    long numberOfSecureMessages
}

 // and gets used like this
        growthRequest.identity {
            numberOfCompanies = 1L
            numberOfUsersPerCompany = 1L
            numberOfSecureMessages = 2L
		}

// or like this, without defining a constructor...
		GrowthRequest gr = new GrowthRequest("numberOfCompanies:1L", "numberOfSecureMessages:2L", "numberOfUsersPerCompany:1L")

Groovy also supports the concept of a read-only property. It uses the final keyword, which is a little confusing to us Java devs, as the field isn’t final in the Java sense. A getter will be defined in the public interface of the Groovy object, but the object can still modify the value internally.

Another case that is very nice is long strings, or strings that are concatenated with variables. In Java, we have

String s = "some text that is long " +
		"and some more text that goes here" +
		someVar + " more text and more";

whereas Groovy would have

String s = """some text that is long and some more text that goes here ${someVar} more text and more"""

If your string is just a regular string (i.e. no variable replacement), you can use apostrophes. If you use a single quotation mark, or triple quotation marks, then Groovy will parse the string to search for replacements. So, for performance reasons, unless you need a variable replacement, use apostrophes for strings.

The heredocformat allows for much easier test string generation. For example

String s = """ <xml> <entities> <entity>1</entity> <entity id="idValue">2</entity>
			</entities>
		</xml>

Keep in mind that I’m intentionally typing variables. Groovy allows for no typing as well, so the previous example could merely def s.

It's only fair to share...
Share on FacebookGoogle+Tweet about this on TwitterShare on LinkedIn

Leave a Reply