I'm trying to implement an event bus that will receive register
listeners like this
public <T extends Event> void registerListener(Class<T>
eventClassname, EventListener<T> eventListener);
(Event is a custom interface)
what i would like then to achieve would be to be able to fire an event
public void fireEvent(Event event);
and be able to trigger all my event listeners that either are of the
same event.getClass() or that are assignable from the event class that
has been fired. Getting all the listeners that correspond to the event
class itself is no problem, but how can i achieve getting all the
listeners that registered to a interface that the event class might
implement without being able to use Class.isAssignableFrom()?
Thanks for any hints and help in advance.
Dominik
Chris
On Feb 19, 3:58 am, Dominik Steiner <dominik.j.stei...@googlemail.com>
wrote:
Map<Class<?>, List<Listener<?>> handledEventClasses;
<T extends Event> List<Listener<T>> get(Class<T> eventClass) {
List<Listenter<T>> result = new ArrayList<Listener<T>>
for ( Entry<Class<T>, List<Listener<T> entry :
handledEventClasses.entrySet() ) {
if ( eventClass.isAssignableFrom(entry.getKey()) {
result.addAll((List<Listener<T>>)entry.getValue());
}
}
return result;
}
Of course, I neglect any null checking, and I don't know if that will
compile or not, but I think you can follow the basic logic.
Also, this method will probably need to be better optimized since it
runs in linear time according to the number of event classes you
register. This could be done by making the put method intelligent
enough to put the listeners into bins for each class you register.
On Feb 19, 4:43 am, Chris Lercher <cl_for_mail...@gmx.net> wrote:
> The JRE Emulation Reference says, that getSuperclass() is supported.http://code.google.com/webtoolkit/doc/latest/RefJreEmulation.html
yes, thanks for the tip, I already tried that by getSuperclass() will
not get any classes that are implemented by this class but only a
superclass. So I would not be able to use interfaces but only extend
from a base class to which i could also attach a listener too and that
would be triggered if a subclass event would be fired, but that's not
the same than having the power and flexibility of interfaces.
@Nathan,
your solution is what I tried first and my junit tests would run just
fine with this but GWT doesn't implement the isAssignableFrom()
method, so that's why I'm looking for some alternatives if there are?
Thanks again for your great help and perhaps there are other solutions
out there still?
Dominik
> --
> You received this message because you are subscribed to the Google
> Groups "Google Web Toolkit" group.
> To post to this group, send email to google-we...@googlegroups.com
> .
> To unsubscribe from this group, send email to google-web-tool...@googlegroups.com
> .
> For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en
> .
>
http://code.google.com/p/google-web-toolkit/issues/detail?id=4663
Dominik
On 19 Feb., 09:09, Dominik Steiner <dominik.j.stei...@googlemail.com>
wrote: