Friday, August 25, 2006

Java Joe Closures - Quad Tree Example

There has been some discussion about the need for closures in Java, about whether they really would be useful to the great majority of Java programmers. Well I consider myself to write pretty straight-forward Java but I frequently find myself writing this sort of code.

Consider an example from the GIS arena. I have a 'quad-tree' spatial index and I want to be able to add content to the 'quad' that represents a particular lat/long point. I haven't written the quad-tree but it provides a convenience method to find a quad given a lat/long point:

// content to be stored on a quad
public class Content {}
public class Point {}


public class Quad {
// quad on which content can be stored
public void add(Content c) {}

// convenience method for finding the relevant quad in the
// quad-tree based on a root quad and a lat/lng
public void findQuad(
Point pnt,
QuadListener listener );
}

// define a listener to be invoked when the correct quad
// is found
public interface QuadListener {
void foundQuad( Quad q );
}



If this was Javascript then I could use the classes that I have been supplied with to write a generic addContentToQuadTree() method as below:

function addContentToQuadTree( root,pnt,c ) {
root.findQuad( pnt,function(q) { q.add(c); } );
}


To do the samething in Java I need to write:

// define a class to hold the content so it can be added to the quad
static class AddContentListener implements QuadListener {
private final Content c;

public AddContentListener( Content c ) {
this.c = c;
}

public void foundQuad( Quad q ) {
q.add( c );
}
}

// high level method to add the specified content to the
// quad defined by root and pnt
public void addContentToQuadTree( Quad root, Point pnt, Content c ) {
root.findQuad( pnt,new AddContentListener( c ) );
}



Closures allow you to avoid defining this intermediate class.

No comments: