@Test public void notifyObservers() { final String event = new String("I'm an event!"); sut.addObserver(new String(), stringObserver); sut.addObserver(new String(), stringObserver2); sut.addObserver(new Boolean(true), booleanObserver); context.checking(new Expectations() { { oneOf(stringObserver).update(event); oneOf(stringObserver2).update(event); } }); sut.notifyObservers(event); context.assertIsSatisfied(); }
@SuppressWarnings("unchecked") public void notifyObservers(final Object event) { int thisRunId = ++lastNotifyRunId; boolean shouldLog = log != null && log.isLoggable(Level.FINER); lastFiredEvent.put(event.getClass(), event); List<Observer< ? >> observers = observerHandlers.get(event.getClass()); if (observers == null || observers.isEmpty()) { if (shouldLog) { log.finer("[" + thisRunId + "] Notify with event " + event.getClass().getName() + " " + event + " for no observers."); } } else { if (shouldLog) { log.finer("[" + thisRunId + "] Starting notify with event " + event.getClass().getName() + " " + event + " for " + observers.size() + " observers."); } for (Observer observer : observers) { if (shouldLog) { log.finer("[" + thisRunId + "] Notifying observer " + observer.getClass().getName() + " " + observer); } try { observer.update(event); } catch (Exception ex) { try { if (log != null) { log.log(Level.WARNING, "Event listener '" + observer + "' of type " + observer.getClass().getName() + " for event '" + event + "' of type " + event.getClass().getName() + " threw an exception.", ex); } } catch (Exception ex2) { if (log != null) { log.log(Level.WARNING, "Event listener threw an exception.", ex); } } } } if (shouldLog) { log.finer("[" + thisRunId + "] Finishing notify"); } } }
EventBus { @SuppressWarnings("unchecked") public void notifyObservers(final Object event) { int thisRunId = ++lastNotifyRunId; boolean shouldLog = log != null && log.isLoggable(Level.FINER); lastFiredEvent.put(event.getClass(), event); List<Observer< ? >> observers = observerHandlers.get(event.getClass()); if (observers == null || observers.isEmpty()) { if (shouldLog) { log.finer("[" + thisRunId + "] Notify with event " + event.getClass().getName() + " " + event + " for no observers."); } } else { if (shouldLog) { log.finer("[" + thisRunId + "] Starting notify with event " + event.getClass().getName() + " " + event + " for " + observers.size() + " observers."); } for (Observer observer : observers) { if (shouldLog) { log.finer("[" + thisRunId + "] Notifying observer " + observer.getClass().getName() + " " + observer); } try { observer.update(event); } catch (Exception ex) { try { if (log != null) { log.log(Level.WARNING, "Event listener '" + observer + "' of type " + observer.getClass().getName() + " for event '" + event + "' of type " + event.getClass().getName() + " threw an exception.", ex); } } catch (Exception ex2) { if (log != null) { log.log(Level.WARNING, "Event listener threw an exception.", ex); } } } } if (shouldLog) { log.finer("[" + thisRunId + "] Finishing notify"); } } } }
EventBus { @SuppressWarnings("unchecked") public void notifyObservers(final Object event) { int thisRunId = ++lastNotifyRunId; boolean shouldLog = log != null && log.isLoggable(Level.FINER); lastFiredEvent.put(event.getClass(), event); List<Observer< ? >> observers = observerHandlers.get(event.getClass()); if (observers == null || observers.isEmpty()) { if (shouldLog) { log.finer("[" + thisRunId + "] Notify with event " + event.getClass().getName() + " " + event + " for no observers."); } } else { if (shouldLog) { log.finer("[" + thisRunId + "] Starting notify with event " + event.getClass().getName() + " " + event + " for " + observers.size() + " observers."); } for (Observer observer : observers) { if (shouldLog) { log.finer("[" + thisRunId + "] Notifying observer " + observer.getClass().getName() + " " + observer); } try { observer.update(event); } catch (Exception ex) { try { if (log != null) { log.log(Level.WARNING, "Event listener '" + observer + "' of type " + observer.getClass().getName() + " for event '" + event + "' of type " + event.getClass().getName() + " threw an exception.", ex); } } catch (Exception ex2) { if (log != null) { log.log(Level.WARNING, "Event listener threw an exception.", ex); } } } } if (shouldLog) { log.finer("[" + thisRunId + "] Finishing notify"); } } } EventBus(); }
EventBus { @SuppressWarnings("unchecked") public void notifyObservers(final Object event) { int thisRunId = ++lastNotifyRunId; boolean shouldLog = log != null && log.isLoggable(Level.FINER); lastFiredEvent.put(event.getClass(), event); List<Observer< ? >> observers = observerHandlers.get(event.getClass()); if (observers == null || observers.isEmpty()) { if (shouldLog) { log.finer("[" + thisRunId + "] Notify with event " + event.getClass().getName() + " " + event + " for no observers."); } } else { if (shouldLog) { log.finer("[" + thisRunId + "] Starting notify with event " + event.getClass().getName() + " " + event + " for " + observers.size() + " observers."); } for (Observer observer : observers) { if (shouldLog) { log.finer("[" + thisRunId + "] Notifying observer " + observer.getClass().getName() + " " + observer); } try { observer.update(event); } catch (Exception ex) { try { if (log != null) { log.log(Level.WARNING, "Event listener '" + observer + "' of type " + observer.getClass().getName() + " for event '" + event + "' of type " + event.getClass().getName() + " threw an exception.", ex); } } catch (Exception ex2) { if (log != null) { log.log(Level.WARNING, "Event listener threw an exception.", ex); } } } } if (shouldLog) { log.finer("[" + thisRunId + "] Finishing notify"); } } } EventBus(); static EventBus getInstance(); void addObserver(final Object event, final Observer< ? > observer); void bufferObservers(); void restoreBufferedObservers(); void addObserver(final Class< ? extends Object> event, final Observer< ? > observer,
            final Boolean fireLastEvent); void addObservers(final Observer observer, final Class... events); void addObserver(final Class< ? extends Object> event, final Observer< ? > observer); void removeObserver(final Object event, final Observer< ? > observer); void removeObserver(final Class< ? extends Object> event, final Observer< ? > observer); void clear(); @SuppressWarnings("unchecked") void notifyObservers(final Object event); }
EventBus { @SuppressWarnings("unchecked") public void notifyObservers(final Object event) { int thisRunId = ++lastNotifyRunId; boolean shouldLog = log != null && log.isLoggable(Level.FINER); lastFiredEvent.put(event.getClass(), event); List<Observer< ? >> observers = observerHandlers.get(event.getClass()); if (observers == null || observers.isEmpty()) { if (shouldLog) { log.finer("[" + thisRunId + "] Notify with event " + event.getClass().getName() + " " + event + " for no observers."); } } else { if (shouldLog) { log.finer("[" + thisRunId + "] Starting notify with event " + event.getClass().getName() + " " + event + " for " + observers.size() + " observers."); } for (Observer observer : observers) { if (shouldLog) { log.finer("[" + thisRunId + "] Notifying observer " + observer.getClass().getName() + " " + observer); } try { observer.update(event); } catch (Exception ex) { try { if (log != null) { log.log(Level.WARNING, "Event listener '" + observer + "' of type " + observer.getClass().getName() + " for event '" + event + "' of type " + event.getClass().getName() + " threw an exception.", ex); } } catch (Exception ex2) { if (log != null) { log.log(Level.WARNING, "Event listener threw an exception.", ex); } } } } if (shouldLog) { log.finer("[" + thisRunId + "] Finishing notify"); } } } EventBus(); static EventBus getInstance(); void addObserver(final Object event, final Observer< ? > observer); void bufferObservers(); void restoreBufferedObservers(); void addObserver(final Class< ? extends Object> event, final Observer< ? > observer,
            final Boolean fireLastEvent); void addObservers(final Observer observer, final Class... events); void addObserver(final Class< ? extends Object> event, final Observer< ? > observer); void removeObserver(final Object event, final Observer< ? > observer); void removeObserver(final Class< ? extends Object> event, final Observer< ? > observer); void clear(); @SuppressWarnings("unchecked") void notifyObservers(final Object event);  }