Wednesday, November 25, 2009

Strong Groovy

Usually Groovy is thought of as a loose, dynamically typed language that is otherwise similar to Java. Given this, it would make sense to write in Groovy when dynamism is needed an write in Java when you need strong typing, etc. to avoid side effects. But a great differentiator for groovy over any other dynamic language running on the JVM is that it also has all of Java's support for Strong typing, Generics, immutable objects and the like. This allows Groovy a tremendous range using dynamism where it makes sense and locking it down where it makes sense to do that.

Recently I found myself looking for a solution for Value Lists for using in my HTML Select fields. Initially I went for quick and dirty and created a utility class (a singleton) with a bunch of "value" lists. Literally lists of NameValue objects with a corresponding java class like:


public class NameValue {
private String name;
private String value;

public NameValue(String name, String value) {
this.setName(name);
this.setValue(value);
}

@Override
public String toString() {
return getValue();
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getValue() {
return value;
}

public void setValue(String value) {
this.value = value;
}
}


I'm not sure why I used Java for this considering it was a grails project, but the class would have read better in groovy:


class NameValue {
String name, value

@Override
public String toString() {
return this.getValue();
}
}


Then I used my Singleton class to populate the lists as so:


public class ValueLists {
private static ValueLists ourInstance = new ValueLists();

public static ValueLists getInstance() {
return ourInstance;
}

private List approvals = new ArrayList();

public List getApprovals() {
return this.approvals;
}

approvals.add(new NameValue("1", "Yes"));
approvals.add(new NameValue("0", "No"));
approvals.add(new NameValue("2", "Exempt"));
}


While we're at it.... why not redo this class in groovy:


class ValueLists {
private static ValueLists ourInstance = new ValueLists();

public static ValueLists getInstance() {
return ourInstance;
}

private List approvals = [
new NameValue(name: "1", value: "Yes"),
new NameValue(name: "0", value: "No"),
new NameValue(name: "2", value: "Exempt")
]
}


Now I can use my Value Lists in my grails app like so:


<g:select name="approval" from="${ValueLists.instance.approvals}" optionkey="name"/>


Simple enough but it occurs to me that the ValueLists ought to be immutable as the lists should never change. It would then look like:


@immutable final class ValueLists {
private static ValueLists ourInstance = new ValueLists();

public static ValueLists getInstance() {
return ourInstance;
}

private List approvals = [
new NameValue(name: "1", value: "Yes"),
new NameValue(name: "0", value: "No"),
new NameValue(name: "2", value: "Exempt")
]
}


"approvals" (and any other properties) is now treated as private final with a getter. So my unwary teammates cannot override it. In addition, "approvals" is wrapped by an immutable wrapper class so my teammates can't get the list of NameValues and change it.

Of course if you've been working with Java 1.5 or newer for any significant amount of time it probably would have occurred to you that you could have used java's language support for enums. Well, no need to drop down to Java for this. My new Groovy Object is as follows(Approval.groovy):


enum Approval{
Yes("1", "Yes"),
No("0", "No"),
Exempt("2", "Exempt")

final String value
final String key

Approval(String key, String value) {
this.value = value
this.key = key
}

String toString() {
value
}
}


Now following this pattern I can code up all my grails selects as follows:


<g:select name="approval" from="${Approval}" optionkey="key" optionvalue="value"/>


The point is that Groovy is a Strongly typed language as well as it is a Dynamically typed language.

Monkey Search