Is it possible to tie nested generics?

Posted by Michael Deardeuff on Stack Overflow See other posts from Stack Overflow or by Michael Deardeuff
Published on 2010-04-06T08:36:15Z Indexed on 2010/04/06 10:33 UTC
Read the original article Hit count: 191

Filed under:
|
|

Is it possible to tie nested generics/captures together?

I often have the problem of having a Map lookup of class to genericized item of said class. In concrete terms I want something like this (no, T is not declared anywhere).

private Map<Class<T>, ServiceLoader<T>> loaders = Maps.newHashMap();

In short, I want loaders.put/get to have semantics something like these:

<T> ServiceLoader<T> get(Class<T> klass) {...}
<T> void put(Class<T> klass, ServiceLoader<T> loader) {...}

Is the following the best I can do? Do I have to live with the inevitable @SuppressWarnings("unchecked") somewhere down the line?

private Map<Class<?>, ServiceLoader<?>> loaders = Maps.newHashMap();

© Stack Overflow or respective owner

Related posts about java

Related posts about generics