I want to make a map-kind of container that has the following interface:
public <T> Thing<T> get(Class<T> clazz);
public <T> void put(Class<T> clazz, Thing<T> thing);
The interesting point is that the T
s in each Class<T>
-> Thing<T>
pair is the same T
, but the container should be able to hold many different types of pairs. Initially I tried a (Hash)Map. But, for instance,
Map<Class<T>, Thing<T>>
is not right, because then T
would be the same T
for all pairs in that map. Of course,
Map<Class<?>, Thing<?>>
works, but then I don't have type-safety guarantees so that when I get(String.class)
, I can't be sure that I get a Thing<String>
instance back.
Is there an obvious way to accomplish the kind of type safety that I'm looking for?
The map itself would not guarantee that, but if you are accessing it only through the above methods, you will have the desired safety.