Notifier

/** * Notifier * from org.junit.runner.notification.RunNotifier */ public class Notifier { /** * Listener list */ List<EventListener> listeners = new ArrayList<EventListener>(); /** * Template for looping listeners. As inner class LoopNotifier could access * fields in out class. */ abstract class LoopNotifier { void run() { Iterator<EventListener> it = listeners.iterator(); while (it.hasNext()) { try { notifyListener(it.next()); } catch (Exception e) { it.remove();// Remove the exception listener, otherwise fireException will notify the listener again. fireException(e); } } } /** * To be implemented in real call. */ abstract protected void notifyListener(EventListener each); } /** * Sample method */ public void fireEvent() { new LoopNotifier() { @Override protected void notifyListener(EventListener each) { // Do business } }.run(); } public void fireException(Exception e) { new LoopNotifier() { @Override protected void notifyListener(EventListener each) { // Exception handling } }.run(); } public void addListener(EventListener listener) { listeners.add(listener); } public void removeListener(EventListener listener) { listeners.remove(listener); } }  

你可能感兴趣的:(exception,list,Class,Access,each)