mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-07 00:48:28 +01:00
Remove unnecessary map lookup for event interfaces
This commit is contained in:
parent
d5cdb36e47
commit
a5a3b4f31e
@ -6,6 +6,7 @@ import java.util.Collection;
|
|||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.function.BiConsumer;
|
import java.util.function.BiConsumer;
|
||||||
|
import java.util.function.Consumer;
|
||||||
import java.util.function.Predicate;
|
import java.util.function.Predicate;
|
||||||
|
|
||||||
public interface EventInterface<E extends Event> {
|
public interface EventInterface<E extends Event> {
|
||||||
@ -16,7 +17,8 @@ public interface EventInterface<E extends Event> {
|
|||||||
|
|
||||||
@NotNull Collection<Class<? extends Event>> eventTypes();
|
@NotNull Collection<Class<? extends Event>> eventTypes();
|
||||||
|
|
||||||
void call(@NotNull E event);
|
Consumer<E> consumer(Class<? extends Event> eventType);
|
||||||
|
|
||||||
|
|
||||||
class FilteredBuilder<E extends Event, T> {
|
class FilteredBuilder<E extends Event, T> {
|
||||||
private final EventFilter<E, T> filter;
|
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,
|
public <M extends E> FilteredBuilder<E, T> map(@NotNull Class<M> eventType,
|
||||||
@NotNull BiConsumer<@NotNull T, @NotNull M> consumer) {
|
@NotNull BiConsumer<@NotNull T, @NotNull M> consumer) {
|
||||||
|
//noinspection unchecked
|
||||||
this.mapped.put(eventType, (BiConsumer<Object, E>) consumer);
|
this.mapped.put(eventType, (BiConsumer<Object, E>) consumer);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
@ -44,11 +47,13 @@ public interface EventInterface<E extends Event> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void call(@NotNull E event) {
|
public Consumer<E> consumer(Class<? extends Event> eventType) {
|
||||||
final T handler = filter.getHandler(event);
|
final var consumer = copy.get(eventType);
|
||||||
if (!predicate.test(handler)) return;
|
return event -> {
|
||||||
final var consumer = copy.get(event.getClass());
|
final T handler = filter.getHandler(event);
|
||||||
consumer.accept(handler, event);
|
if (!predicate.test(handler)) return;
|
||||||
|
consumer.accept(handler, event);
|
||||||
|
};
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -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);
|
Check.stateCondition(!correct, "The node filter {0} is not compatible with type {1}", nodeType, valueType);
|
||||||
synchronized (mappedNodeCache) {
|
synchronized (mappedNodeCache) {
|
||||||
System.out.println("add " + value + " " + nodeImpl + " " + type);
|
|
||||||
entry.mappedNode.put(value, (EventNode<T>) nodeImpl);
|
entry.mappedNode.put(value, (EventNode<T>) nodeImpl);
|
||||||
mappedNodeCache.put(value, entry);
|
mappedNodeCache.put(value, entry);
|
||||||
}
|
}
|
||||||
@ -226,7 +225,9 @@ class EventNodeImpl<T extends Event> implements EventNode<T> {
|
|||||||
synchronized (GLOBAL_CHILD_LOCK) {
|
synchronized (GLOBAL_CHILD_LOCK) {
|
||||||
for (var eventType : eventInterface.eventTypes()) {
|
for (var eventType : eventInterface.eventTypes()) {
|
||||||
var entry = getEntry((Class<? extends T>) eventType);
|
var entry = getEntry((Class<? extends T>) eventType);
|
||||||
|
final var consumer = eventInterface.consumer(eventType);
|
||||||
entry.interfaces.add((EventInterface<T>) eventInterface);
|
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<EventFilter<?, ?>> filters;
|
||||||
final List<EventListener<T>> listeners = new CopyOnWriteArrayList<>();
|
final List<EventListener<T>> listeners = new CopyOnWriteArrayList<>();
|
||||||
final Set<EventInterface<T>> interfaces = new CopyOnWriteArraySet<>();
|
final Set<EventInterface<T>> interfaces = new CopyOnWriteArraySet<>();
|
||||||
|
final Set<Consumer<T>> interfaceConsumers = new CopyOnWriteArraySet<>();
|
||||||
final Map<Object, EventNode<T>> mappedNode = new WeakHashMap<>();
|
final Map<Object, EventNode<T>> mappedNode = new WeakHashMap<>();
|
||||||
volatile int childCount;
|
volatile int childCount;
|
||||||
|
|
||||||
@ -313,9 +315,9 @@ class EventNodeImpl<T extends Event> implements EventNode<T> {
|
|||||||
|
|
||||||
void call(T event) {
|
void call(T event) {
|
||||||
// Event interfaces
|
// Event interfaces
|
||||||
if (!interfaces.isEmpty()) {
|
if (!interfaceConsumers.isEmpty()) {
|
||||||
for (EventInterface<T> inter : interfaces) {
|
for (var consumer : interfaceConsumers) {
|
||||||
inter.call(event);
|
consumer.accept(event);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Mapped listeners
|
// Mapped listeners
|
||||||
|
Loading…
Reference in New Issue
Block a user