001 package de.deepamehta.core.service; 002 003 import java.util.HashMap; 004 import java.util.Map; 005 006 007 008 /** 009 * Base class for all events. 010 * That is core events as well as plugin events. 011 */ 012 public abstract class DeepaMehtaEvent { 013 014 // ------------------------------------------------------------------------------------------------- Class Variables 015 016 /** 017 * A map of all known events (contains core events as well as plugin events). 018 * Used to find the event that corresponds to a certain listener interface. 019 */ 020 private static Map<String, DeepaMehtaEvent> events = new HashMap(); 021 022 // ---------------------------------------------------------------------------------------------- Instance Variables 023 024 /** 025 * The corresponding listener interface for this event. 026 */ 027 private final Class listenerInterface; 028 029 // ---------------------------------------------------------------------------------------------------- Constructors 030 031 public DeepaMehtaEvent(Class listenerInterface) { 032 this.listenerInterface = listenerInterface; 033 putEvent(this, listenerInterface); 034 } 035 036 // -------------------------------------------------------------------------------------------------- Public Methods 037 038 /** 039 * Delivers this event to the given listener. 040 */ 041 public abstract void deliver(EventListener listener, Object... params); 042 043 /** 044 * Returns the corresponding listener interface for this event. 045 */ 046 public Class getListenerInterface() { 047 return listenerInterface; 048 } 049 050 /** 051 * Returns the event that corresponds to the given listener interface. 052 */ 053 public static DeepaMehtaEvent getEvent(Class listenerInterface) { 054 DeepaMehtaEvent event = events.get(listenerInterface.getName()); 055 // 056 if (event == null) { 057 throw new RuntimeException("The event for listener " + listenerInterface + " is unknown"); 058 } 059 // 060 return event; 061 } 062 063 // === 064 065 @Override 066 public String toString() { 067 return getClass().getName() + " (" + listenerInterface.getName() + ")"; 068 } 069 070 // ------------------------------------------------------------------------------------------------- Private Methods 071 072 private void putEvent(DeepaMehtaEvent event, Class listenerInterface) { 073 events.put(listenerInterface.getName(), event); 074 } 075 }