Alex Rivera | Logout

Type safety, Java generics and querying

Asked 2009-05-15T14:53:55.903
10

I have an interesting situation and I'm wondering if there is a better way to do this. The situation is this, I have a tree structure (an abstract syntax tree, specifically) and some nodes can contain child nodes of various types but all extended from a given base class.

I want to frequently do queries on this tree and I'd like to get back the specific subtypes I'm interested in. So I created a predicate class that I can then pass into a generic query method. At first I had a query method that looked like this:

public <T extends Element> List<T> findAll(IElementPredicate pred, Class<T> c);

where the Class argument was used just to indicate the return type. What bothered me about this approach is that all of my predicates were already for specific types so there is redundant information here. A typical call might look like:

List<Declaration> decls = 
    scope.findAll(new DeclarationPredicate(), Declaration.class);

So I refactored it like this:

public <T extends Element> List<T> findAll(IElementPredicate<T> pred);

Where the IElementPredicate interface looks like this:

public interface IElementPredicate<T extends Element> {
    public boolean match(T e);
    public String getDescription();
    public Class<T> getGenericClass();
}

The point here is that the predicate interface is expanded to provide the Class object instead. It makes writing the actual findAll method a little bit more work and it adds a bit more work in writing the predicate, but those are both essentially tiny "one-time" things and it makes the query call so much nicer because you don't have to add the extra (potentially redundant) argument, e.g.

List<Declaration> decls = scope.findAll(new DeclarationPredicate());

                    
                    
                    
Edit
Report

1 Answer

1

Your tree structure sounds very much like an XML DOM object. Have you considered translating your tree into a DOM structure and using XPath to do your queries? It might be a lot less custom code.

answered 2009-05-16T05:14:05.227

Your Answer