import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; import java.util.ArrayList; import java.util.List; public class ObserverPool { private final List observers; private final T dispatcher; public ObserverPool(Class observerClass) { observers = new ArrayList(); dispatcher = (T) Proxy.newProxyInstance(observerClass.getClassLoader(), new Class[] { observerClass }, new Dispatcher()); } public void add(T observer) { synchronized (observers) { observers.add(observer); } } public void remove(T observer) { synchronized (observers) { observers.remove(observer); } } public T getDispatcher() { return dispatcher; } public boolean hasObservers() { return !observers.isEmpty(); } private class Dispatcher implements InvocationHandler { public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable { List exceptions = new ArrayList(); return dispatchToObservers(method, exceptions, args); } private Object dispatchToObservers(Method method, List exceptionsThrown, Object... args) { try { synchronized (observers) { for (Object observer : observers) { try { method.invoke(observer, args); } catch (RuntimeException re) { exceptionsThrown.add(re); } catch (Exception e) { exceptionsThrown.add(e); } } } } catch (IllegalArgumentException e) { exceptionsThrown.add(e); } if (exceptionsThrown.isEmpty()) { return null; } else { throw new DispatchException(exceptionsThrown); } } } public static class DispatchException extends RuntimeException { private final List exceptionsThrown; public DispatchException(List exceptionsThrown) { this.exceptionsThrown = exceptionsThrown; } public List getExceptionsThrown() { return exceptionsThrown; } } }