Using Groovy for Tacos-Demo & Tapestry

So, since groovy compiles down to bytecode, it can be used anywhere… That’s how i did a test tapestry page a month ago.

But this time i took some time off to play with groovy and the tacos demo app (Shing is hosting it for us right now) and the JetGroovy plugin of IntelliJ Idea 7.0M2. My findings are really nothing new and mostly deal with java vs. groovy … things!

So, the ajax tree demo uses a TreeData bean for its data… here it is in java:

 public class TreeData {
  private String name;
     private List<TreeData> children;
 
     public TreeData() {this(null);}
 
     public TreeData(String name) {
         this.name = name;
         children = new ArrayList<TreeData> ();
     }
 
     public String getName() { return name; }
 
     public void setName(String name) { this.name = name; }
 
     public List getChildren() { return children; }
 
     public void setChildren(List<TreeData> children) { this.children = children; }
 
     public void addChild(TreeData value) {
         String name = value.getName();
         for (int i = 0; i < children.size(); i++) {
             TreeData treeData = children.get(i);
             if (name.compareTo(treeData.getName()) < 0) {
                 children.add(i, value);
                 return;
             }
         }
         children.add(value);
     }
 
     public TreeData getOrCreateChild(String value) {
         for (TreeData treeData : children) {
             if (treeData.getName().equals(value))
                 return treeData;
         }
         TreeData data = new TreeData(value);
         addChild(data);
         return data;
     }
 }

and here it is in groovy:

 class TreeDataGroovy {
     String name
     List<TreeDataGroovy> children
 
     def addChild(TreeDataGroovy value) {
         if (children==null) children = [];
         int pos = children.findIndexOf { value.name < it.name }
         children.add(pos, value)
     }
 
     TreeDataGroovy getOrCreateChild(String value) {
         TreeDataGroovy data = children.find {it.name==value};
         if (data==null) {
             data = new TreeDataGroovy(name:value)
             addChild(data)
         }
         return data;
     }
 }

On the other hand, I ended up dealing with exceptions and errors quite more often that i do in java (perhaps due to my lack of knowledge for groovy) so it did force me in more unit tests, which some say is a good thing and others the opposite.