Add priority to the eventbus

This commit is contained in:
Trevor Guidry
2019-01-09 02:11:33 -05:00
parent e009957ad6
commit dfe6443e2d
2 changed files with 10 additions and 4 deletions

View File

@@ -38,6 +38,7 @@ import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
import java.util.Comparator;
import java.util.function.Consumer; import java.util.function.Consumer;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.concurrent.ThreadSafe; import javax.annotation.concurrent.ThreadSafe;
@@ -62,6 +63,7 @@ public class EventBus
{ {
private final Object object; private final Object object;
private final Method method; private final Method method;
private final float priority;
@EqualsAndHashCode.Exclude @EqualsAndHashCode.Exclude
private final SubscriberMethod lamda; private final SubscriberMethod lamda;
@@ -105,6 +107,9 @@ public class EventBus
builder.putAll(subscribers); builder.putAll(subscribers);
} }
builder.orderValuesBy(Comparator.comparing(Subscriber::getPriority)
.thenComparing(s -> s.object.getClass().getName()));
for (Class<?> clazz = object.getClass(); clazz != null; clazz = clazz.getSuperclass()) for (Class<?> clazz = object.getClass(); clazz != null; clazz = clazz.getSuperclass())
{ {
for (final Method method : clazz.getDeclaredMethods()) for (final Method method : clazz.getDeclaredMethods())
@@ -160,7 +165,7 @@ public class EventBus
log.warn("Unable to create lambda for method {}", method, e); log.warn("Unable to create lambda for method {}", method, e);
} }
final Subscriber subscriber = new Subscriber(object, method, lambda); final Subscriber subscriber = new Subscriber(object, method, sub.priority(), lambda);
builder.put(parameterClazz, subscriber); builder.put(parameterClazz, subscriber);
log.debug("Registering {} - {}", parameterClazz, subscriber); log.debug("Registering {} - {}", parameterClazz, subscriber);
} }
@@ -196,7 +201,7 @@ public class EventBus
} }
final Class<?> parameterClazz = method.getParameterTypes()[0]; final Class<?> parameterClazz = method.getParameterTypes()[0];
map.remove(parameterClazz, new Subscriber(object, method, null)); map.remove(parameterClazz, new Subscriber(object, method, sub.priority(), null));
} }
} }
@@ -204,8 +209,8 @@ public class EventBus
} }
/** /**
* Posts provided event to all registered subscribers. Subscriber calls are invoked immediately and in order * Posts provided event to all registered subscribers. Subscriber calls are invoked immediately,
* in which subscribers were registered. * ordered by priority then their declaring class' name.
* *
* @param event event to post * @param event event to post
*/ */

View File

@@ -38,4 +38,5 @@ import java.lang.annotation.Target;
@Documented @Documented
public @interface Subscribe public @interface Subscribe
{ {
float priority() default 0;
} }