Handler Caching #37

Merged
kske merged 5 commits from f/handler-caching into develop 2022-01-18 17:11:38 +01:00
Showing only changes of commit 8609c6a90c - Show all commits

View File

@ -193,9 +193,7 @@ public final class EventBus {
* @since 1.2.0 * @since 1.2.0
*/ */
private NavigableSet<EventHandler> getHandlersFor(Class<?> eventType) { private NavigableSet<EventHandler> getHandlersFor(Class<?> eventType) {
if (bindingCache.containsKey(eventType)) { return bindingCache.computeIfAbsent(eventType, k -> {
return bindingCache.get(eventType);
} else {
// Get handlers defined for the event class // Get handlers defined for the event class
TreeSet<EventHandler> handlers = TreeSet<EventHandler> handlers =
@ -208,9 +206,8 @@ public final class EventBus {
if (handler.isPolymorphic()) if (handler.isPolymorphic())
handlers.add(handler); handlers.add(handler);
bindingCache.put(eventType, handlers);
return handlers; return handlers;
} });
} }
/** /**
@ -396,13 +393,13 @@ public final class EventBus {
private void bindHandler(EventHandler handler) { private void bindHandler(EventHandler handler) {
// Bind handler // Bind handler
bindings.putIfAbsent(handler.getEventType(), new TreeSet<>(byPriority));
logger.log(Level.DEBUG, "Binding event handler {0}", handler); logger.log(Level.DEBUG, "Binding event handler {0}", handler);
bindings.get(handler.getEventType()).add(handler); bindings.computeIfAbsent(handler.getEventType(), k -> new TreeSet<>(byPriority))
.add(handler);
// Insert handler into cache // Insert handler into cache
bindingCache.putIfAbsent(handler.getEventType(), new TreeSet<>(byPriority)); bindingCache.computeIfAbsent(handler.getEventType(), k -> new TreeSet<>(byPriority))
bindingCache.get(handler.getEventType()).add(handler); .add(handler);
// Handler is polymorphic => insert where applicable // Handler is polymorphic => insert where applicable
if (handler.isPolymorphic()) if (handler.isPolymorphic())