7
7
import java .util .ArrayList ;
8
8
import java .util .HashMap ;
9
9
10
+ /**
11
+ * Registry for making objects available to listen for events.<br>
12
+ * <br>
13
+ * Implement an EventInterface into your class, then implement the function.<br>
14
+ * <br>
15
+ * In your initializer method, register the instance of the class with {@link EventListenerRegistry#register(EventBase)}
16
+ * <br>
17
+ * Example:
18
+ * <pre>
19
+ * public class ClassWithListener implements EventInit {
20
+ *
21
+ * @ Override
22
+ * public void onEventInit() {
23
+ * // Implement event specific code
24
+ * }
25
+ * }
26
+ * </pre>
27
+ * <br>
28
+ * To create and fire your own events, check {@link EventBase}<br>
29
+ */
10
30
public class EventListenerRegistry {
11
31
32
+ /**
33
+ * Base interface for events.<br>
34
+ * <br>
35
+ * To create a new event, create an interface and extend EventBase.<br>
36
+ * Only 1 method is accepted in an event, so it's best to add {@link FunctionalInterface} to the interface.<br>
37
+ * <br>
38
+ * Example:
39
+ * <pre>
40
+ * @ FunctionalInterface
41
+ * public interface EventInit extends EventBase {
42
+ * public void onEventInit();
43
+ * }
44
+ *
45
+ * @ FunctionalInterface
46
+ * public interface EventAdd extends EventBase {
47
+ * public int onEventAdd(int a, int b); // Accepts parameters and return types
48
+ * }
49
+ * </pre>
50
+ *
51
+ * To fire your event, use {@link EventListenerRegistry#fireEvent(Class)} with the parameter being the event class.<br>
52
+ *
53
+ * <pre>
54
+ * EventListenerRegistry.fireEvent(EventInit.class);
55
+ * </pre>
56
+ *
57
+ * To fire an event with parameters and return types:
58
+ * <pre>
59
+ * int eventResult = (int) EventListenerRegistry.fireEvent(EventAdd.class, a, b);
60
+ * </pre>
61
+ * When using parameters, the type and the amount of parameters has to match!
62
+ */
63
+ public interface EventBase {
64
+ }
65
+
12
66
/**
13
67
* Eventlistener Registry.<br>
14
68
* <br>
@@ -20,7 +74,10 @@ public class EventListenerRegistry {
20
74
*/
21
75
private static final HashMap <Class <?>, ArrayList <EventBase >> EVENTLISTENER_REGISTRY = new HashMap <>();
22
76
23
-
77
+ /**
78
+ * Registers an object to be an event listener. The object must implement an event extending {@link EventBase}
79
+ * @param eventListener The event listener to register
80
+ */
24
81
public static void register (EventBase eventListener ) {
25
82
if (eventListener == null ) {
26
83
throw new NullPointerException ("Tried to register a packethandler with value null" );
@@ -38,6 +95,10 @@ public static void register(EventBase eventListener) {
38
95
}
39
96
}
40
97
98
+ /**
99
+ * Unregisters an object from being an event listener.
100
+ * @param eventListener The event listener to unregister
101
+ */
41
102
public static void unregister (EventBase eventListener ) {
42
103
if (eventListener == null ) {
43
104
throw new NullPointerException ("Tried to unregister a packethandler with value null" );
@@ -74,17 +135,18 @@ public static Object fireEvent(Class<? extends EventListenerRegistry.EventBase>
74
135
* @return The result of the event, might be null if the event returns nothing
75
136
*/
76
137
public static Object fireEvent (Class <? extends EventListenerRegistry .EventBase > eventClass , Object ... eventParams ) {
77
- ArrayList <EventBase > registryList = EVENTLISTENER_REGISTRY .get (eventClass );
78
- if (registryList == null ) {
138
+ ArrayList <EventBase > listenerList = EVENTLISTENER_REGISTRY .get (eventClass );
139
+ if (listenerList == null ) {
79
140
throw new EventException ("The event has not been registered yet" , eventClass );
80
141
}
81
142
143
+ //
82
144
EventException toThrow = null ;
83
145
84
146
Method methodToCheck = getEventMethod (eventClass );
85
147
86
148
Object returnValue = null ;
87
- for (EventBase eventListener : registryList ) {
149
+ for (EventBase eventListener : listenerList ) {
88
150
Method [] methodsInListener = eventListener .getClass ().getDeclaredMethods ();
89
151
90
152
for (Method method : methodsInListener ) {
@@ -130,14 +192,29 @@ private static Method getEventMethod(Class<? extends EventListenerRegistry.Event
130
192
return test [0 ];
131
193
}
132
194
195
+ /**
196
+ * @param method The method to check
197
+ * @param name The name to check
198
+ * @return If method.getName equals name
199
+ */
133
200
private static boolean checkName (Method method , String name ) {
134
201
return method .getName ().equals (name );
135
202
}
136
203
204
+ /**
205
+ * @param method The method to check
206
+ * @param parameters The list of parameters
207
+ * @return True, if length of the method parameters is equal to the length of the object parameters
208
+ */
137
209
private static boolean checkLength (Method method , Object ... parameters ) {
138
210
return method .getParameterCount () == parameters .length ;
139
211
}
140
212
213
+ /**
214
+ * @param method The method to check
215
+ * @param parameters The list of parameters
216
+ * @return True, if the types of the parameters equal the object parameters
217
+ */
141
218
private static boolean checkTypes (Method method , Object ... parameters ) {
142
219
Class <?>[] methodParameterTypes = ClassUtils .primitivesToWrappers (method .getParameterTypes ());
143
220
Class <?>[] eventParameterTypes = getParameterTypes (parameters );
@@ -166,7 +243,4 @@ private static Class<?>[] getParameterTypes(Object... parameters) {
166
243
public static void clear () {
167
244
EVENTLISTENER_REGISTRY .clear ();
168
245
}
169
-
170
- public interface EventBase {
171
- }
172
246
}
0 commit comments