I have a class with a set of wildcard types that is singleton, for example:
public ObliviousClass{ private static final ObliviousClass INSTANCE = new ObliviousClass(); private Map<Key, Type<?>> map = new HashMap<Key, Type<?>>(); public void putType(Key key, Type<?> type){ map.put(type); }
I would like to be able to add various types of parameters to this collection in client code:
void clientMethod(){ ObliviousClass oc = ObliviousClass.getInstance(); Type<Integer> intType = ... Type<String> stringType = ... oc.putType(new Key(0), intType); oc.putType(new Key(1), stringType); }
Up to this point, as I understand it, everything is in order. But the client also needs to get Type<?>
Provided Key
. Thus, the following method will be added to ObliviousClass
:
public Type<?> getType(Key key){ return map.get(key); }
But in my handy copy of Effective Java, I read:
Do not use lookup types as return types.
I understand this problem, as the client would need to return the returned Type<?>
. But I really don't want to make ObliviousClass
generic type, ObliviousClass<T>
, because then my client code did not work above ...
Is there a better design for what I'm trying to do? -My current solution is to provide a static method for the client; sort of:
public static <T> void getType(ObliviousClass instance, Key key, Type<T> dest){ dest = (Type<T>)instance.getType(key); }
I searched around, but could not find an answer that completely cleared my confusion.