KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I'm facing some problems with Generics when using Google Guava's excellent Multimap. I have a type Handler defined as such public interface Handler<T extends Serializable> { void handle(T t); } In another class I've defined a multimap that maps a String to a collection of Handlers. private Multimap<String, Handler<? extends Serializable>> multimap = ArrayListMultimap.create(); Now when I try to do stuff with the multimap, I'm getting compiler errors. My first attempt looked like this: public <T extends Serializable> void doStuff1(String s, T t) { Collection<Handler<T>> collection = multimap.get(s); for (Handler<T> handler : collection) { handler.handle(t); } } which resulted in the following error. Type mismatch: cannot convert from Collection<Handler<? extends Serializable>> to Collection<Handler<T>> Afterwards, I tried to code it like this public void doStuff2(String s, Serializable serializable) { Collection<Handler<? extends Serializable>> collection = multimap.get(s); for (Handler<? extends Serializable> handler : collection) { handler.handle(serializable); } } which unfortunately failed as well: The method handle(capture#1-of ? extends Serializable) in the type Handler<capture#1-of ? extends Serializable> is not applicable for the arguments (Serializable) Any help would be greatly appreciated. Thanks. Update: The only way I have managed to fix this is by suppressing compiler warnings. Given the following handler: public interface Handler<T extends Event> { void handle(T t); Class<T> getType(); } I can write the event bus as such. <
Tags (comma-separated)
Save Edits
Cancel