tags:

views:

96

answers:

3

Here is what I would like to write in my java code:

private <A extends Action<R>, R extends Result> MyType<A,R> member;

This is invalid syntax however. So I end up writing:

private MyType<? extends Action<? extends Result>, ? extends Result> member;

But this disregard the fact that both classes derived from Result are the same. My class methods all enforce this relationship, so I can be sure that MyType enforces it, but I still have to unsafely typecast member in some instances.

More details

Here is the precise version of what I want to do, although it is much more criptic:

I wish I could do:

private <A extends Action<R>, R extends Result> 
    Map< Class<A>, ActionHandler<A,R> > handlers;

Instead I have to do:

private Map< Class< ? extends Action<? extends Result> >, 
             ActionHandler<? extends Action<? extends Result>, 
                           ? extends Result> > handlers;

My methods enforce the desired relationship and look like that:

public <A extends Action<R>, R extends Result> void addHandler( 
    ActionHandler<A, R> handler ) {
  handlers.put( handler.getActionType(), handler );
}

I would like the following method:

public <A extends Action<R>, R extends Result> ActionHandler<A, R> 
    findHandler( A action ) {
  return handlers.get( action.getClass() );
}

But this doesn't work: I have to add a cast and a @SuppressWarnings("unchecked").

Things I tried

I tried creating a new class just for that purpose :

public class MyMap <A extends Action<R>, R extends Result> extends 
    HashMap< Class<A>, ActionHandler<A,R> > { ... }

MyMap< ?, ? > handlers;

But it didn't work, I still need a cast.

A: 

Move the parameterization to your class itself?

public class X<A, R...> {
    private Map<A,R> blah;
    public R method(A type) {}
}
Steven Schlansker
Parameterizing the class would mean that all the elements in the map have to be of the same type. That's not what I want.
Philippe Beaudoin
Ach, sorry, it's really hard to figure out what you're trying to do. Hard to express, I guess :-p
Steven Schlansker
+1  A: 

So you want to implement a type-safe heterogeneous container without casting.

Interesting puzzle.

I think this is impossible in Java. Consider:

class Entry<K,V> { 
    K key;
    V value;
}
interface Map<E extends Entry<?,?>> { 
    void put(E e);
    E get(Object k);
}

class MyEntry<T> extends Entry<Class<T>, T> { }

A caller holding a Map<MyEntry<?>> can now be sure that entries are type safe, and the put method specified in Map nicely enforces this. Also, the get method guarantees that a proper mapping is returned. What I could not express, is that its argument is of the returned entry's key type.

I tried this as follows:

<K, V, RE extends E & Entry<K,V>> V get(K k);

Unfortunately, it is illegal for a type parameter to implement an interface twice with different type arguments, or as javac puts it:

a type variable may not be followed by other bounds

meriton
+2  A: 

There's no clean way to do this. The best you can do is what you've already done -- hide the Map behind methods that enforce type safety, and hide the necessary casts in those methods.

From Effective Java 2nd edition, Item 29: "Consider typesafe heterogeneous containers" (in which Josh Bloch sketches out a simpler version of what you're doing, a "map" where the keys are classes and the values are instances of the key class):

The next thing to notice is that the value type of the favorites Map is simply Object. In other words, the Map does not guarantee the type relationship between keys and values. In fact, Java's type system is not powerful enough to express this. But we know that it's true, and we take advantage of it when it comes time to retrieve a favorite.

Given that you can't get any useful type enforcement for the values anyway, the least messy implementation is probably something like this:

class HandlerRegistry {

    private Map<Class<?>, Object> map = new HashMap<Class<?>, Object>();

    public <R extends Result, A extends Action<R>> 
      void addHandler(Class<A> actionClass, ActionHandler<A, R> handler) {
        map.put(actionClass, handler);
    }

    @SuppressWarnings("unchecked")
    public <R extends Result, A extends Action<R>> ActionHandler<A, R>
      findHandler(A action) {
        return (ActionHandler<A, R>) map.get(action.getClass());
    }
}

One thing to note about a Map-based solution: if action isn't exactly the class used as the key, map.get() isn't going to work -- for instance, if it's a subclass, or if the key class is an interface and the action itself is the implementation. You might be better off with:

    for (Map.Entry<Class<?>, Object> entry : map.entrySet()) {
        if (entry.getKey().isInstance(action)) {
            return (ActionHandler<A, R>) entry.getValue();
        }
    }
David Moles
Great detailed answer! Thanks!
Philippe Beaudoin