开发者

Type-safe mapping from Class<T> to Thing<T>

开发者 https://www.devze.com 2023-01-02 15:22 出处:网络
I want to make a map-kind of container that has the following interface: public <T> Thing<T> get(Class<T> clazz);

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 Ts 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&l开发者_C百科t;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.


If you want to be able to put different types shouldn't you declare two type parameters?

public <K, V> Thing<V> get(Class<K> clazz);
public <K, V> void put(Class<K> clazz, Thing<V> thing);

or did I misunderstand the question?

Edit: I see, well if you want o container that can hold entities of different types, there's no way you can have complete type safety, since when you declare your container you can only put one type on the container and then you may be able to put objects in, but you can't be sure what you get back. At best you'll end up putting in objects as Object, and then doing instanceof and casts when you get them back. All collections have this problem. Imagine you have a Collection<T extends Thing>. You may put in it Things, ChildOfThings, or GrandChildOfThings, but when you get it back, you're only guarantee is that it's a Thing, you can't tell if it's a Child or GrandChild, without actually testing it.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号