Remove second generic from EventNode

This commit is contained in:
TheMode 2021-06-03 04:26:47 +02:00
parent 5fa750fde3
commit d5cce11562
2 changed files with 23 additions and 37 deletions

View File

@ -14,46 +14,46 @@ import java.util.concurrent.CopyOnWriteArraySet;
import java.util.function.BiPredicate; import java.util.function.BiPredicate;
import java.util.function.Predicate; import java.util.function.Predicate;
public class EventNode<T extends Event, V> { public class EventNode<T extends Event> {
public static <E extends Event, V> EventNode<E, V> type(@NotNull EventFilter<E, V> filter) { public static <E extends Event, V> EventNode<E> type(@NotNull EventFilter<E, V> filter) {
return new EventNode<>(filter, (e, v) -> true); return new EventNode<>(filter, (e, v) -> true);
} }
public static EventNode<Event, ?> all() { public static EventNode<Event> all() {
return type(EventFilter.ALL); return type(EventFilter.ALL);
} }
public static <E extends Event, V> EventNode<E, V> predicate(@NotNull EventFilter<E, V> filter, public static <E extends Event, V> EventNode<E> predicate(@NotNull EventFilter<E, V> filter,
@NotNull BiPredicate<E, V> predicate) { @NotNull BiPredicate<E, V> predicate) {
return new EventNode<>(filter, predicate); return new EventNode<>(filter, (e, o) -> predicate.test(e, (V) o));
} }
public static <E extends Event, V> EventNode<E, V> predicateEvent(@NotNull EventFilter<E, V> filter, public static <E extends Event, V> EventNode<E> predicateEvent(@NotNull EventFilter<E, V> filter,
@NotNull Predicate<E> predicate) { @NotNull Predicate<E> predicate) {
return predicate(filter, (e, h) -> predicate.test(e)); return predicate(filter, (e, h) -> predicate.test(e));
} }
public static <E extends Event, V> EventNode<E, V> predicateValue(@NotNull EventFilter<E, V> filter, public static <E extends Event, V> EventNode<E> predicateValue(@NotNull EventFilter<E, V> filter,
@NotNull Predicate<V> predicate) { @NotNull Predicate<V> predicate) {
return predicate(filter, (e, h) -> predicate.test(h)); return predicate(filter, (e, h) -> predicate.test(h));
} }
private final Map<Class<? extends T>, List<EventListener<T>>> listenerMap = new ConcurrentHashMap<>(); private final Map<Class<? extends T>, List<EventListener<T>>> listenerMap = new ConcurrentHashMap<>();
private final Map<Object, RedirectionEntry<T>> redirectionMap = new ConcurrentHashMap<>(); private final Map<Object, RedirectionEntry<T>> redirectionMap = new ConcurrentHashMap<>();
private final Set<EventNode<T, ?>> children = new CopyOnWriteArraySet<>(); private final Set<EventNode<T>> children = new CopyOnWriteArraySet<>();
protected final EventFilter<T, V> filter; protected final EventFilter<T, ?> filter;
protected volatile BiPredicate<T, V> predicate; protected final BiPredicate<T, Object> predicate;
private volatile String name = "unknown"; private volatile String name = "unknown";
// Tree data // Tree data
private static final Object GLOBAL_CHILD_LOCK = new Object(); private static final Object GLOBAL_CHILD_LOCK = new Object();
private volatile EventNode<? super T, ?> parent; private volatile EventNode<? super T> parent;
private final Object lock = new Object(); private final Object lock = new Object();
private final Object2IntMap<Class<? extends T>> childEventMap = new Object2IntOpenHashMap<>(); private final Object2IntMap<Class<? extends T>> childEventMap = new Object2IntOpenHashMap<>();
protected EventNode(EventFilter<T, V> filter, BiPredicate<T, V> predicate) { protected EventNode(EventFilter<T, ?> filter, BiPredicate<T, Object> predicate) {
this.filter = filter; this.filter = filter;
this.predicate = predicate; this.predicate = predicate;
} }
@ -65,7 +65,8 @@ public class EventNode<T extends Event, V> {
* @return true to enter the node, false otherwise * @return true to enter the node, false otherwise
*/ */
protected boolean condition(@NotNull T event) { protected boolean condition(@NotNull T event) {
return predicate.test(event, filter.getHandler(event)); final var value = filter.getHandler(event);
return predicate.test(event, value);
} }
public void call(@NotNull T event) { public void call(@NotNull T event) {
@ -107,9 +108,9 @@ public class EventNode<T extends Event, V> {
this.children.forEach(eventNode -> eventNode.call(event)); this.children.forEach(eventNode -> eventNode.call(event));
} }
public void addChild(@NotNull EventNode<? extends T, ?> child) { public void addChild(@NotNull EventNode<? extends T> child) {
synchronized (GLOBAL_CHILD_LOCK) { synchronized (GLOBAL_CHILD_LOCK) {
final boolean result = this.children.add((EventNode<T, ?>) child); final boolean result = this.children.add((EventNode<T>) child);
if (result) { if (result) {
child.parent = this; child.parent = this;
// Increase listener count // Increase listener count
@ -123,7 +124,7 @@ public class EventNode<T extends Event, V> {
} }
} }
public void removeChild(@NotNull EventNode<? extends T, ?> child) { public void removeChild(@NotNull EventNode<? extends T> child) {
synchronized (GLOBAL_CHILD_LOCK) { synchronized (GLOBAL_CHILD_LOCK) {
final boolean result = this.children.remove(child); final boolean result = this.children.remove(child);
if (result) { if (result) {
@ -167,15 +168,7 @@ public class EventNode<T extends Event, V> {
} }
} }
public @NotNull BiPredicate<T, V> getPredicate() { public <E extends T, V2> void map(@NotNull EventFilter<E, V2> filter, @NotNull V2 value, @NotNull EventNode<E> node) {
return predicate;
}
public void setPredicate(@NotNull BiPredicate<T, V> predicate) {
this.predicate = predicate;
}
public <E extends T, V2> void map(@NotNull EventFilter<E, V2> filter, @NotNull V2 value, @NotNull EventNode<E, V2> node) {
RedirectionEntry<E> entry = new RedirectionEntry<>(); RedirectionEntry<E> entry = new RedirectionEntry<>();
entry.filter = filter; entry.filter = filter;
entry.node = node; entry.node = node;
@ -194,7 +187,7 @@ public class EventNode<T extends Event, V> {
this.name = name; this.name = name;
} }
public @NotNull Set<@NotNull EventNode<T, ?>> getChildren() { public @NotNull Set<@NotNull EventNode<T>> getChildren() {
return Collections.unmodifiableSet(children); return Collections.unmodifiableSet(children);
} }
@ -218,6 +211,6 @@ public class EventNode<T extends Event, V> {
private static class RedirectionEntry<E extends Event> { private static class RedirectionEntry<E extends Event> {
EventFilter<E, ?> filter; EventFilter<E, ?> filter;
EventNode<E, ?> node; EventNode<E> node;
} }
} }

View File

@ -149,13 +149,6 @@ public class PlayerInit {
.expirationCount(50) .expirationCount(50)
.build()); .build());
empty.addChild(node);
node.call(new PlayerTickEvent(null));
empty.call(new PlayerTickEvent(null));
empty.removeChild(node);
node.call(new PlayerTickEvent(null));
empty.call(new PlayerTickEvent(null));
var conditional = EventNode.predicateValue(EventFilter.PLAYER, Player::isCreative); var conditional = EventNode.predicateValue(EventFilter.PLAYER, Player::isCreative);
conditional.addListener(EventListener.of(PlayerMoveEvent.class, (event) -> conditional.addListener(EventListener.of(PlayerMoveEvent.class, (event) ->