EventDispatcher implements EventAPI { public void dispatchEvent(Event e) { synchronized (listeners) { dispatching.incrementAndGet(); checkCanDispatchEventType(e.getEventType()); for (final Listener l : listeners.get(e.getEventType())) { l.handleEvent(e); } dispatching.decrementAndGet(); } update(); } EventDispatcher(Set<Enum<?>> supportedEventTypes);  EventDispatcher(Enum<?>... supportedEventTypes); void dispatchEvent(Event e); void safeDispatchEvent(Event e); @Override void addListener(Listener listener, Enum<?>... eventTypes); @Override void addListener(Listener listener,
      Iterable<? extends Enum<?>> eventTypes); @Override void removeListener(Listener listener, Enum<?>... eventTypes); @Override void removeListener(Listener listener,
      Iterable<? extends Enum<?>> eventTypes); @Override boolean containsListener(Listener listener, Enum<?> eventType); boolean hasListenerFor(Enum<?> eventType); EventAPI getPublicEventAPI();  }
@Test(expected = IllegalArgumentException.class) public void dispatchEventFail2() { dispatcher.dispatchEvent(new Event(OTHER_EVENT1)); }
@Test public void dispatchEvent() { assertFalse(dispatcher.hasListenerFor(EVENT1)); assertFalse(dispatcher.hasListenerFor(EVENT2)); assertFalse(dispatcher.hasListenerFor(EVENT3)); assertFalse(dispatcher.hasListenerFor(OTHER_EVENT1)); dispatcher.addListener(l1, EVENT1); assertTrue(dispatcher.hasListenerFor(EVENT1)); assertFalse(dispatcher.hasListenerFor(EVENT2)); assertFalse(dispatcher.hasListenerFor(EVENT3)); assertFalse(dispatcher.hasListenerFor(OTHER_EVENT1)); final Set<Enum<?>> set = new HashSet<Enum<?>>(asList(EVENT1, EVENT2)); api.addListener(l2, set); assertFalse(dispatcher.hasListenerFor(EVENT3)); dispatcher.addListener(l3, EVENT1, EVENT2, EVENT3); assertTrue(dispatcher.hasListenerFor(EVENT1)); assertFalse(dispatcher.hasListenerFor(OTHER_EVENT1)); dispatcher.dispatchEvent(new Event(EVENT2)); assertEquals(asList(), l1.getEventTypeHistory()); assertEquals(asList(EVENT2), l2.getEventTypeHistory()); assertEquals(asList(EVENT2), l3.getEventTypeHistory()); dispatcher.dispatchEvent(new Event(EVENT3)); assertEquals(asList(), l1.getEventTypeHistory()); assertEquals(asList(EVENT2), l2.getEventTypeHistory()); assertEquals(asList(EVENT2, EVENT3), l3.getEventTypeHistory()); dispatcher.dispatchEvent(new Event(EVENT1)); assertEquals(asList(EVENT1), l1.getEventTypeHistory()); assertEquals(asList(EVENT2, EVENT1), l2.getEventTypeHistory()); assertEquals(asList(EVENT2, EVENT3, EVENT1), l3.getEventTypeHistory()); dispatcher.dispatchEvent(new Event(EVENT3)); assertEquals(asList(EVENT1), l1.getEventTypeHistory()); assertEquals(asList(EVENT2, EVENT1), l2.getEventTypeHistory()); assertEquals(asList(EVENT2, EVENT3, EVENT1, EVENT3), l3.getEventTypeHistory()); }