Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

This example looks at the issues surrounding a mixed Java/Groovy application. This issue only arises when there is mutual dependencies between your mixed language source files. So, if part of your system is pure Java for instance, you won't have this problem. You would just compile that part of your system first and reference the resulting class/jar file(s) from the part of your system that was written in Groovy.

The legacy version of our application

...

Our hack failed because if we run javac first, ZipSorter won't have the Box class available because it is written in Groovy. If we run groovyc first, WeightSorter doesn't
have the Postpack class because it is written in Java. Similarly, if using our IDE, we will face the same deadly embrace problem.

A proper version 2 of our application

The way we get around this problem is to define some common interfaces as follows:

Code Block

package v2;

public interface Parcel {
    int getWeight();
    int getZip();
}
Code Block

package v2;

import java.util.List;

public interface Sorter {
    List sort(Parcel[] unsorted);
}

Now we write our Java and Groovy parts of the system being careful to refer only to the interfaces, e.g. the Java files would become:

Code Block

package v2;

public class Postpack implements Parcel, Comparable {
    private final int weight;
    private final int zip;

    public int getWeight() {
        return weight;
    }

    public int getZip() {
        return zip;
    }

    public String toString() {
        return "Postpack[Weight=" + weight + ", Zip=" + zip + "]";
    }

    public Postpack(int weight, int zip) {
        this.weight = weight;
        this.zip = zip;
    }

    public int compareTo(Object o) {
        Parcel other = (Parcel) o;
        return zip - other.getZip();
    }
}
Code Block

package v2;

import java.util.List;
import java.util.Arrays;

public class ZipSorter implements Sorter {
    public List sort(Parcel[] items) {
        Arrays.sort(items);
        return Arrays.asList(items);
    }
}

And the Groovy ones look like:

Code Block

package v2
public class Box implements Parcel, Comparable {
    int weight
    int zip
    String toString() {
        return "Box[Weight=" + weight + ", Zip=" + zip + "]"
    }
    int compareTo(other) { return zip - other.zip }
}
Code Block

package v2
public class WeightSorter implements Sorter {
    List sort(Parcel[] items) {
        items.toList().sort{ p1, p2 -> p1.weight <=> p2.weight }
    }
}

Finally, our main method looks like:

Code Block

package v2;

import java.util.Arrays;

public class SortMain {
    private static Box box1 = new Box();
    private static Parcel[] packs = { new Postpack(60, 12345), box1, new Postpack(50, 54321) };
    private static Sorter zipSorter = new ZipSorter();
    private static Sorter weightSorter = new WeightSorter();
    public static void main(String[] args) {
        box1.setWeight(55);
        box1.setZip(99999);
        System.out.println("Unsorted:         " + Arrays.asList(packs));
        System.out.println("Sorted by weight: " + weightSorter.sort(packs));
        System.out.println("Sorted by zip:    " + zipSorter.sort(packs));
    }
}

We need to compile the interfaces first, then we can compile the Groovy or Java files (excluding SortMain) in either order. Finally we compile SortMain as it is the class that knows about the concrete implementations. If we were using Spring or our own factory methods we could have reduced or eliminated the need to treat SortMain as a special case, e.g. Spring could have the concrete classes listed in a beans.xml file and SortMain whether it was written in Java or Groovy could have been compiled along with all the other files written in its language.

Now when we run the program we get the following output:

Code Block
none
none

Unsorted:         [Postpack[Weight=60, Zip=12345], Box[Weight=55, Zip=99999], Postpack[Weight=50, Zip=54321]]
Sorted by weight: [Postpack[Weight=50, Zip=54321], Box[Weight=55, Zip=99999], Postpack[Weight=60, Zip=12345]]
Sorted by zip:    [Postpack[Weight=60, Zip=12345], Postpack[Weight=50, Zip=54321], Box[Weight=55, Zip=99999]]

For more details, see chapter 11 of GINA.