From 5afd8d98711797f109c913541d8a2e99d8db3a85 Mon Sep 17 00:00:00 2001 From: Daniil Date: Wed, 25 Jul 2018 21:06:58 +0700 Subject: [PATCH] Extracted base EventLoop class --- .../java/mc/core/events/BaseEventLoop.java | 41 +++++++++++++++++++ .../java/mc/core/events/SimpleEventLoop.java | 37 +++-------------- 2 files changed, 46 insertions(+), 32 deletions(-) create mode 100644 event-loop/src/main/java/mc/core/events/BaseEventLoop.java diff --git a/event-loop/src/main/java/mc/core/events/BaseEventLoop.java b/event-loop/src/main/java/mc/core/events/BaseEventLoop.java new file mode 100644 index 0000000..59231d6 --- /dev/null +++ b/event-loop/src/main/java/mc/core/events/BaseEventLoop.java @@ -0,0 +1,41 @@ +package mc.core.events; + +import lombok.extern.slf4j.Slf4j; + +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; + +@Slf4j +public abstract class BaseEventLoop implements EventLoop { + @Override + public void addEventHandler(Object object) { + for (Method method : object.getClass().getDeclaredMethods()) { + EventHandler annotation = method.getAnnotation(EventHandler.class); + if (annotation == null) + continue; // We are not interested in methods without @EventHandler annotation + + if (!Modifier.isPublic(method.getModifiers())) { + log.error("Unable to register {} as an EventHandler. Method must have a 'private' access modifier.", method.toString()); + continue; + + } + + if (method.getParameterCount() != 1) { + log.error("Unable to register {} as an EventHandler. Method must have exactly one argument.", method.toString()); + continue; + } + + Class firstParamType = method.getParameterTypes()[0]; + if (!Event.class.isAssignableFrom(firstParamType)) { + log.error("Unable to register {} as an EventHandler. First parameter type must implement 'Event' interface.", method.toString()); + continue; + } + + @SuppressWarnings("unchecked") Class eventType = (Class) firstParamType; + + registerMethod(object, method, annotation, eventType); + } + } + + protected abstract void registerMethod(Object object, Method method, EventHandler annotation, Class eventType); +} diff --git a/event-loop/src/main/java/mc/core/events/SimpleEventLoop.java b/event-loop/src/main/java/mc/core/events/SimpleEventLoop.java index 47e2ff3..5963a38 100644 --- a/event-loop/src/main/java/mc/core/events/SimpleEventLoop.java +++ b/event-loop/src/main/java/mc/core/events/SimpleEventLoop.java @@ -10,7 +10,7 @@ import java.lang.reflect.Modifier; import java.util.*; @Slf4j -public class SimpleEventLoop implements EventLoop { +public class SimpleEventLoop extends BaseEventLoop { private Map, List> handlers = new HashMap<>(); @Override @@ -27,39 +27,12 @@ public class SimpleEventLoop implements EventLoop { } } - @Override - public void addEventHandler(Object object) { - for (Method method : object.getClass().getDeclaredMethods()) { - EventHandler annotation = method.getAnnotation(EventHandler.class); - if (annotation == null) - continue; // We are not interested in methods without @EventHandler annotation - - if (!Modifier.isPublic(method.getModifiers())) { - log.error("Unable to register {} as an EventHandler. Method must have a 'private' access modifier.", method.toString()); - continue; - - } - - if (method.getParameterCount() != 1) { - log.error("Unable to register {} as an EventHandler. Method must have exactly one argument.", method.toString()); - continue; - } - - Class firstParamType = method.getParameterTypes()[0]; - if (!Event.class.isAssignableFrom(firstParamType)) { - log.error("Unable to register {} as an EventHandler. First parameter type must implement 'Event' interface.", method.toString()); - continue; - } - - @SuppressWarnings("unchecked") Class eventType = (Class) firstParamType; - - List eventHandlers = handlers.computeIfAbsent(eventType, s -> new ArrayList<>()); - eventHandlers.add(new ExecutorLink(annotation.priority().getValue(), annotation.ignoreCancelled(), method, object)); - eventHandlers.sort(Comparator.comparingInt(o -> o.priority)); - } + protected void registerMethod(Object object, Method method, EventHandler annotation, Class eventType) { + List eventHandlers = handlers.computeIfAbsent(eventType, s -> new ArrayList<>()); + eventHandlers.add(new SimpleEventLoop.ExecutorLink(annotation.priority().getValue(), annotation.ignoreCancelled(), method, object)); + eventHandlers.sort(Comparator.comparingInt(o -> o.priority)); } - /** * This class describes */