Rename EventInterface, add unregister

This commit is contained in:
TheMode 2021-08-17 02:24:21 +02:00
parent a5a3b4f31e
commit 390c383e82
3 changed files with 37 additions and 22 deletions

View File

@ -9,7 +9,7 @@ import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Predicate;
public interface EventInterface<E extends Event> {
public interface EventBinding<E extends Event> {
static <E extends Event, T> @NotNull FilteredBuilder<E, T> filtered(@NotNull EventFilter<E, T> filter, @NotNull Predicate<T> predicate) {
return new FilteredBuilder<>(filter, predicate);
@ -17,8 +17,7 @@ public interface EventInterface<E extends Event> {
@NotNull Collection<Class<? extends Event>> eventTypes();
Consumer<E> consumer(Class<? extends Event> eventType);
@NotNull Consumer<@NotNull E> consumer(@NotNull Class<? extends Event> eventType);
class FilteredBuilder<E extends Event, T> {
private final EventFilter<E, T> filter;
@ -37,23 +36,28 @@ public interface EventInterface<E extends Event> {
return this;
}
public @NotNull EventInterface<E> build() {
public @NotNull EventBinding<E> build() {
final var copy = Map.copyOf(mapped);
final var eventTypes = copy.keySet();
return new EventInterface<>() {
Map<Class<? extends Event>, Consumer<E>> consumers = new HashMap<>(eventTypes.size());
for (var eventType : eventTypes) {
final var consumer = copy.get(eventType);
consumers.put(eventType, event -> {
final T handler = filter.getHandler(event);
if (!predicate.test(handler)) return;
consumer.accept(handler, event);
});
}
return new EventBinding<>() {
@Override
public @NotNull Collection<Class<? extends Event>> eventTypes() {
return eventTypes;
}
@Override
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);
};
public @NotNull Consumer<E> consumer(@NotNull Class<? extends Event> eventType) {
return consumers.get(eventType);
}
};
}

View File

@ -320,5 +320,7 @@ public interface EventNode<T extends Event> {
boolean unmap(@NotNull Object value);
void registerInterface(@NotNull EventInterface<? extends T> eventInterface);
void register(@NotNull EventBinding<? extends T> binding);
void unregister(@NotNull EventBinding<? extends T> binding);
}

View File

@ -221,13 +221,23 @@ class EventNodeImpl<T extends Event> implements EventNode<T> {
}
@Override
public void registerInterface(@NotNull EventInterface<? extends T> eventInterface) {
public void register(@NotNull EventBinding<? extends T> binding) {
synchronized (GLOBAL_CHILD_LOCK) {
for (var eventType : eventInterface.eventTypes()) {
for (var eventType : binding.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);
final var consumer = binding.consumer(eventType);
entry.bindingConsumers.add((Consumer<T>) consumer);
}
}
}
@Override
public void unregister(@NotNull EventBinding<? extends T> binding) {
synchronized (GLOBAL_CHILD_LOCK) {
for (var eventType : binding.eventTypes()) {
var entry = listenerMap.get(eventType);
if (entry == null) return;
entry.bindingConsumers.remove(binding.consumer(eventType));
}
}
}
@ -304,8 +314,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 Set<Consumer<T>> bindingConsumers = new CopyOnWriteArraySet<>();
final Map<Object, EventNode<T>> mappedNode = new WeakHashMap<>();
volatile int childCount;
@ -315,8 +324,8 @@ class EventNodeImpl<T extends Event> implements EventNode<T> {
void call(T event) {
// Event interfaces
if (!interfaceConsumers.isEmpty()) {
for (var consumer : interfaceConsumers) {
if (!bindingConsumers.isEmpty()) {
for (var consumer : bindingConsumers) {
consumer.accept(event);
}
}