Remove unnecessary map lookup for event interfaces

This commit is contained in:
TheMode 2021-08-17 01:55:22 +02:00
parent d5cdb36e47
commit a5a3b4f31e
2 changed files with 17 additions and 10 deletions

View File

@ -6,6 +6,7 @@ import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Predicate;
public interface EventInterface<E extends Event> {
@ -16,7 +17,8 @@ public interface EventInterface<E extends Event> {
@NotNull Collection<Class<? extends Event>> eventTypes();
void call(@NotNull E event);
Consumer<E> consumer(Class<? extends Event> eventType);
class FilteredBuilder<E extends Event, T> {
private final EventFilter<E, T> filter;
@ -30,6 +32,7 @@ public interface EventInterface<E extends Event> {
public <M extends E> FilteredBuilder<E, T> map(@NotNull Class<M> eventType,
@NotNull BiConsumer<@NotNull T, @NotNull M> consumer) {
//noinspection unchecked
this.mapped.put(eventType, (BiConsumer<Object, E>) consumer);
return this;
}
@ -44,11 +47,13 @@ public interface EventInterface<E extends Event> {
}
@Override
public void call(@NotNull E event) {
final T handler = filter.getHandler(event);
if (!predicate.test(handler)) return;
final var consumer = copy.get(event.getClass());
consumer.accept(handler, event);
public Consumer<E> consumer(Class<? extends Event> eventType) {
final var consumer = copy.get(eventType);
return event -> {
final T handler = filter.getHandler(event);
if (!predicate.test(handler)) return;
consumer.accept(handler, event);
};
}
};
}

View File

@ -202,7 +202,6 @@ class EventNodeImpl<T extends Event> implements EventNode<T> {
});
Check.stateCondition(!correct, "The node filter {0} is not compatible with type {1}", nodeType, valueType);
synchronized (mappedNodeCache) {
System.out.println("add " + value + " " + nodeImpl + " " + type);
entry.mappedNode.put(value, (EventNode<T>) nodeImpl);
mappedNodeCache.put(value, entry);
}
@ -226,7 +225,9 @@ class EventNodeImpl<T extends Event> implements EventNode<T> {
synchronized (GLOBAL_CHILD_LOCK) {
for (var eventType : eventInterface.eventTypes()) {
var entry = getEntry((Class<? extends T>) eventType);
final var consumer = eventInterface.consumer(eventType);
entry.interfaces.add((EventInterface<T>) eventInterface);
entry.interfaceConsumers.add((Consumer<T>) consumer);
}
}
}
@ -304,6 +305,7 @@ class EventNodeImpl<T extends Event> implements EventNode<T> {
final List<EventFilter<?, ?>> filters;
final List<EventListener<T>> listeners = new CopyOnWriteArrayList<>();
final Set<EventInterface<T>> interfaces = new CopyOnWriteArraySet<>();
final Set<Consumer<T>> interfaceConsumers = new CopyOnWriteArraySet<>();
final Map<Object, EventNode<T>> mappedNode = new WeakHashMap<>();
volatile int childCount;
@ -313,9 +315,9 @@ class EventNodeImpl<T extends Event> implements EventNode<T> {
void call(T event) {
// Event interfaces
if (!interfaces.isEmpty()) {
for (EventInterface<T> inter : interfaces) {
inter.call(event);
if (!interfaceConsumers.isEmpty()) {
for (var consumer : interfaceConsumers) {
consumer.accept(event);
}
}
// Mapped listeners