The problem is that you're iterating over a List<EventListener<? extends Event>>
, but the compiler doesn't know which class, Event
or a subclass of Event
, the ?
wildcard could be. The T
type parameter could also be Event
or any subclass of Event
. They do not match for this reason.
Try removing the wildcards:
// Removed wildcard
private HashMap<String, List<EventListener<Event>>> events = new HashMap<>();
public synchronized <T extends Event> void fireEvent(T event){
String key = event.getClass().getName();
if(events.containsKey(key)){
// Removed wildcard.
Iterator<EventListener<Event>> i = events.get(key).iterator();
while(i.hasNext()){
i.next().receiveEvent(event); // This compiles now.
}
}
}