forked from RepoMirrors/seppuku
Updated Pomelo to support basic listener priorities
Requested by noil, could use some more documentation but it's a bandaid on an old system, should plan to improve it soon.
This commit is contained in:
parent
de7b2a22a1
commit
22fac04d94
|
@ -9,7 +9,7 @@ import team.stiff.pomelo.filter.EventFilter;
|
|||
* @author Daniel
|
||||
* @since May 31, 2017
|
||||
*/
|
||||
public interface EventHandler {
|
||||
public interface EventHandler extends Comparable<EventHandler> {
|
||||
|
||||
/**
|
||||
* Invoked when the listener needs to handle
|
||||
|
@ -24,6 +24,13 @@ public interface EventHandler {
|
|||
*/
|
||||
Object getListener();
|
||||
|
||||
/**
|
||||
* The priority of the current listener inside the container.
|
||||
*
|
||||
* @return listener priority
|
||||
*/
|
||||
ListenerPriority getPriority();
|
||||
|
||||
/**
|
||||
* All of the filters that will be tested on the
|
||||
* handler when being dispatched.
|
||||
|
|
|
@ -0,0 +1,28 @@
|
|||
package team.stiff.pomelo.handler;
|
||||
|
||||
/**
|
||||
* Designates the order within a listener of event distribution. This
|
||||
* is not globally sorted as the current structure of stored event listeners is
|
||||
* too complex to properly sort without major code refactoring.
|
||||
*
|
||||
* todo: hint hint...
|
||||
*/
|
||||
public enum ListenerPriority {
|
||||
LOWEST(-750),
|
||||
LOWER(-500),
|
||||
LOW(-250),
|
||||
NORMAL(0),
|
||||
HIGH(250),
|
||||
HIGHER(500),
|
||||
HIGHEST(750);
|
||||
|
||||
private final int priorityLevel;
|
||||
|
||||
ListenerPriority(final int priorityLevel) {
|
||||
this.priorityLevel = priorityLevel;
|
||||
}
|
||||
|
||||
public int getPriorityLevel() {
|
||||
return priorityLevel;
|
||||
}
|
||||
}
|
|
@ -2,6 +2,8 @@ package team.stiff.pomelo.impl.annotated.handler;
|
|||
|
||||
import team.stiff.pomelo.filter.EventFilter;
|
||||
import team.stiff.pomelo.handler.EventHandler;
|
||||
import team.stiff.pomelo.handler.ListenerPriority;
|
||||
import team.stiff.pomelo.impl.annotated.handler.annotation.Listener;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
|
@ -31,6 +33,11 @@ public final class MethodEventHandler implements EventHandler {
|
|||
*/
|
||||
private final Set<EventFilter> eventFilters;
|
||||
|
||||
/**
|
||||
* The annotation to the event listener.
|
||||
*/
|
||||
private final Listener listenerAnnotation;
|
||||
|
||||
public MethodEventHandler(final Object listenerParent, final Method method,
|
||||
final Set<EventFilter> eventFilters) {
|
||||
this.listenerParent = listenerParent;
|
||||
|
@ -39,6 +46,7 @@ public final class MethodEventHandler implements EventHandler {
|
|||
|
||||
this.method = method;
|
||||
this.eventFilters = eventFilters;
|
||||
this.listenerAnnotation = method.getAnnotation(Listener.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -62,8 +70,19 @@ public final class MethodEventHandler implements EventHandler {
|
|||
return method;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ListenerPriority getPriority() {
|
||||
return listenerAnnotation.priority();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterable<EventFilter> getFilters() {
|
||||
return eventFilters;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(final EventHandler eventHandler) {
|
||||
return Integer.compare(eventHandler.getPriority().getPriorityLevel(),
|
||||
getPriority().getPriorityLevel());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package team.stiff.pomelo.impl.annotated.handler.annotation;
|
||||
|
||||
import team.stiff.pomelo.filter.EventFilter;
|
||||
import team.stiff.pomelo.handler.ListenerPriority;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
|
@ -26,4 +27,11 @@ public @interface Listener {
|
|||
* @return array of filters
|
||||
*/
|
||||
Class<? extends EventFilter>[] filters() default { };
|
||||
|
||||
/**
|
||||
* The priority of the event listener in the container.
|
||||
*
|
||||
* @return listener priority
|
||||
*/
|
||||
ListenerPriority priority() default ListenerPriority.NORMAL;
|
||||
}
|
||||
|
|
|
@ -8,10 +8,7 @@ import team.stiff.pomelo.impl.annotated.handler.MethodEventHandler;
|
|||
import team.stiff.pomelo.impl.annotated.handler.annotation.Listener;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.*;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/**
|
||||
|
@ -33,7 +30,7 @@ public final class MethodHandlerScanner implements EventHandlerScanner {
|
|||
.filter(method -> method.isAnnotationPresent(Listener.class))
|
||||
.filter(method -> method.getParameterCount() == 1)
|
||||
.forEach(method -> eventHandlers
|
||||
.computeIfAbsent(method.getParameterTypes()[0], obj -> new HashSet<>())
|
||||
.computeIfAbsent(method.getParameterTypes()[0], obj -> new TreeSet<>())
|
||||
.add(new MethodEventHandler(listenerContainer, method,
|
||||
filterScanner.scan(method))));
|
||||
return eventHandlers;
|
||||
|
|
Loading…
Reference in New Issue