Generics
Hi,
I have a problem that I have been unable to code cleanly with
generics. I suspect it is not possible because of no run-time generics
info. The problematic code (a cast, highlighted in capitals in
comments) is in dispatchEvent(Event e) below.
Suppose I have a hierarchy of events. XEvent extends Event, YEvent
extends Event etc.
The purpose is to provide a type-safe event listener and an event
dispatcher.
I could create the interface:
public interface <T extends Event> EventListener {
void eventRaised(T event);
}
And also:
public interface EventManager {
<T extends Event> void addListener(EventManager<T> listener,
Class<T> type);
<T extends Event> void removeListener(EventManager<T> listener,
Class<T> type);
void raiseEvent(Event event);
}
I think the fact that I need to specify T in addListener twice (in
listener type and class type) already shows something's wrong...
Now implementing this seems impossible without casts and unchecked
types:
public class EventManagerImpl implements EventManager {
// no way to express the binding between event subclass as Map key
and event type of listener
private final Map<Class<? extends Event>, Set<EventListener<?
extends Event>>> myListenersByType = new HashMap<Class<? extends
Event>, Set<EventListener<? extends Event>>>();
// addListener, removeListener omitted for brevity
public void dispatchEvent(Event event) {
for (Map.Entry<Class<? extends Event>, Set<EventListener<? extends
Event>>> entry : myListenersByType.entrySet()) {
if (entry.getKey().isInstance(event)) {
for (EventListener<? extends Event> listener:
entry.getValue()) {
// UGLY CAST HERE - could this be avoided?
((EventListener<Event>) listener).eventRaised(event);
}
}
}
}
}
TIA,
Kofa