Simplify binding access calls in EventBus
This commit is contained in:
parent
ebc11555f6
commit
e040f6ab1b
@ -51,11 +51,12 @@ public final class EventBus {
|
|||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
private final Map<Class<? extends IEvent>, TreeSet<EventHandler>> bindings
|
private final Map<Class<? extends IEvent>, TreeSet<EventHandler>> bindings =
|
||||||
= new ConcurrentHashMap<>();
|
new ConcurrentHashMap<>();
|
||||||
private final Set<EventListener> registeredListeners = ConcurrentHashMap.newKeySet();
|
private final Set<EventListener> registeredListeners =
|
||||||
private final ThreadLocal<DispatchState> dispatchState
|
ConcurrentHashMap.newKeySet();
|
||||||
= ThreadLocal.withInitial(DispatchState::new);
|
private final ThreadLocal<DispatchState> dispatchState =
|
||||||
|
ThreadLocal.withInitial(DispatchState::new);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Dispatches an event to all event handlers registered for it in descending order of their
|
* Dispatches an event to all event handlers registered for it in descending order of their
|
||||||
@ -97,9 +98,7 @@ public final class EventBus {
|
|||||||
private List<EventHandler> getHandlersFor(Class<? extends IEvent> eventClass) {
|
private List<EventHandler> getHandlersFor(Class<? extends IEvent> eventClass) {
|
||||||
|
|
||||||
// Get handlers defined for the event class
|
// Get handlers defined for the event class
|
||||||
Set<EventHandler> handlers
|
Set<EventHandler> handlers = bindings.getOrDefault(eventClass, new TreeSet<>());
|
||||||
= bindings.containsKey(eventClass) ? bindings.get(eventClass)
|
|
||||||
: new TreeSet<>();
|
|
||||||
|
|
||||||
// Get subtype handlers
|
// Get subtype handlers
|
||||||
for (var binding : bindings.entrySet())
|
for (var binding : bindings.entrySet())
|
||||||
@ -151,8 +150,7 @@ public final class EventBus {
|
|||||||
|
|
||||||
// Initialize and bind the handler
|
// Initialize and bind the handler
|
||||||
var handler = new EventHandler(listener, method, annotation);
|
var handler = new EventHandler(listener, method, annotation);
|
||||||
if (!bindings.containsKey(handler.getEventType()))
|
bindings.putIfAbsent(handler.getEventType(), new TreeSet<>());
|
||||||
bindings.put(handler.getEventType(), new TreeSet<>());
|
|
||||||
logger.log(Level.DEBUG, "Binding event handler {0}", handler);
|
logger.log(Level.DEBUG, "Binding event handler {0}", handler);
|
||||||
bindings.get(handler.getEventType())
|
bindings.get(handler.getEventType())
|
||||||
.add(handler);
|
.add(handler);
|
||||||
@ -163,8 +161,7 @@ public final class EventBus {
|
|||||||
logger.log(
|
logger.log(
|
||||||
Level.WARNING,
|
Level.WARNING,
|
||||||
"No event handlers bound for event listener {0}",
|
"No event handlers bound for event listener {0}",
|
||||||
listener.getClass().getName()
|
listener.getClass().getName());
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user