SafeEvent.java 898 B

123456789101112131415161718192021222324252627282930313233343536
  1. package com.poqop.document.events;
  2. import java.lang.reflect.Method;
  3. public abstract class SafeEvent<T> implements Event<T>
  4. {
  5. private final Class<?> listenerType;
  6. protected SafeEvent()
  7. {
  8. listenerType = getListenerType();
  9. }
  10. private Class<?> getListenerType()
  11. {
  12. for (Method method : getClass().getMethods())
  13. {
  14. if ("dispatchSafely".equals(method.getName()) && !method.isSynthetic())
  15. {
  16. return method.getParameterTypes()[0];
  17. }
  18. }
  19. throw new RuntimeException("Couldn't find dispatchSafely method");
  20. }
  21. @SuppressWarnings({"unchecked"})
  22. public final void dispatchOn(Object listener)
  23. {
  24. if (listenerType.isAssignableFrom(listener.getClass()))
  25. {
  26. dispatchSafely((T) listener);
  27. }
  28. }
  29. public abstract void dispatchSafely(T listener);
  30. }