Add annotations

This commit is contained in:
TheMode 2021-06-08 14:15:30 +02:00
parent 9705915e21
commit 5469190e64
2 changed files with 59 additions and 33 deletions

View File

@ -1,5 +1,6 @@
package net.minestom.server.event; package net.minestom.server.event;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.ArrayList; import java.util.ArrayList;
@ -14,11 +15,13 @@ public interface EventListener<T extends Event> {
@NotNull Result run(@NotNull T event); @NotNull Result run(@NotNull T event);
static <T extends Event> EventListener.Builder<T> builder(@NotNull Class<T> eventType) { @Contract(pure = true)
static <T extends Event> EventListener.@NotNull Builder<T> builder(@NotNull Class<T> eventType) {
return new EventListener.Builder<>(eventType); return new EventListener.Builder<>(eventType);
} }
static <T extends Event> EventListener<T> of(@NotNull Class<T> eventType, @NotNull Consumer<@NotNull T> listener) { @Contract(pure = true)
static <T extends Event> @NotNull EventListener<T> of(@NotNull Class<T> eventType, @NotNull Consumer<@NotNull T> listener) {
return new EventListener<>() { return new EventListener<>() {
@Override @Override
public @NotNull Class<T> getEventType() { public @NotNull Class<T> getEventType() {
@ -34,9 +37,7 @@ public interface EventListener<T extends Event> {
} }
class Builder<T extends Event> { class Builder<T extends Event> {
private final Class<T> eventType; private final Class<T> eventType;
private final List<Predicate<T>> filters = new ArrayList<>(); private final List<Predicate<T>> filters = new ArrayList<>();
private int expireCount; private int expireCount;
private Predicate<T> expireWhen; private Predicate<T> expireWhen;
@ -46,30 +47,37 @@ public interface EventListener<T extends Event> {
this.eventType = eventType; this.eventType = eventType;
} }
public EventListener.Builder<T> filter(Predicate<T> filter) { @Contract(value = "_ -> this")
public @NotNull EventListener.Builder<T> filter(Predicate<T> filter) {
this.filters.add(filter); this.filters.add(filter);
return this; return this;
} }
public EventListener.Builder<T> expireCount(int expireCount) { @Contract(value = "_ -> this")
public @NotNull EventListener.Builder<T> expireCount(int expireCount) {
this.expireCount = expireCount; this.expireCount = expireCount;
return this; return this;
} }
public EventListener.Builder<T> expireWhen(Predicate<T> expireWhen) { @Contract(value = "_ -> this")
public @NotNull EventListener.Builder<T> expireWhen(Predicate<T> expireWhen) {
this.expireWhen = expireWhen; this.expireWhen = expireWhen;
return this; return this;
} }
public EventListener.Builder<T> handler(Consumer<T> handler) { @Contract(value = "_ -> this")
public @NotNull EventListener.Builder<T> handler(Consumer<T> handler) {
this.handler = handler; this.handler = handler;
return this; return this;
} }
@Contract(value = "-> new", pure = true)
public @NotNull EventListener<T> build() { public @NotNull EventListener<T> build() {
AtomicInteger expirationCount = new AtomicInteger(this.expireCount); AtomicInteger expirationCount = new AtomicInteger(this.expireCount);
final boolean hasExpirationCount = expirationCount.get() > 0; final boolean hasExpirationCount = expirationCount.get() > 0;
final Predicate<T> expireWhen = this.expireWhen;
final var filters = new ArrayList<>(this.filters); final var filters = new ArrayList<>(this.filters);
final var handler = this.handler; final var handler = this.handler;
return new EventListener<>() { return new EventListener<>() {

View File

@ -3,6 +3,7 @@ package net.minestom.server.event;
import net.minestom.server.tag.Tag; import net.minestom.server.tag.Tag;
import net.minestom.server.tag.TagReadable; import net.minestom.server.tag.TagReadable;
import net.minestom.server.utils.validate.Check; import net.minestom.server.utils.validate.Check;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
@ -17,43 +18,50 @@ import java.util.function.Predicate;
public class EventNode<T extends Event> { public class EventNode<T extends Event> {
public static EventNode<Event> all(@NotNull String name) { @Contract(value = "_ -> new", pure = true)
public static @NotNull EventNode<Event> all(@NotNull String name) {
return type(name, EventFilter.ALL); return type(name, EventFilter.ALL);
} }
public static <E extends Event, V> EventNode<E> type(@NotNull String name, @Contract(value = "_, _, _ -> new", pure = true)
@NotNull EventFilter<E, V> filter, public static <E extends Event, V> @NotNull EventNode<E> type(@NotNull String name,
@NotNull BiPredicate<E, V> predicate) { @NotNull EventFilter<E, V> filter,
@NotNull BiPredicate<E, V> predicate) {
return create(name, filter, predicate); return create(name, filter, predicate);
} }
public static <E extends Event, V> EventNode<E> type(@NotNull String name, @Contract(value = "_, _ -> new", pure = true)
@NotNull EventFilter<E, V> filter) { public static <E extends Event, V> @NotNull EventNode<E> type(@NotNull String name,
@NotNull EventFilter<E, V> filter) {
return create(name, filter, null); return create(name, filter, null);
} }
public static <E extends Event, V> EventNode<E> event(@NotNull String name, @Contract(value = "_, _, _ -> new", pure = true)
@NotNull EventFilter<E, V> filter, public static <E extends Event, V> @NotNull EventNode<E> event(@NotNull String name,
@NotNull Predicate<E> predicate) { @NotNull EventFilter<E, V> filter,
@NotNull Predicate<E> predicate) {
return create(name, filter, (e, h) -> predicate.test(e)); return create(name, filter, (e, h) -> predicate.test(e));
} }
public static <E extends Event, V> EventNode<E> value(@NotNull String name, @Contract(value = "_, _, _ -> new", pure = true)
@NotNull EventFilter<E, V> filter, public static <E extends Event, V> @NotNull EventNode<E> value(@NotNull String name,
@NotNull Predicate<V> predicate) { @NotNull EventFilter<E, V> filter,
@NotNull Predicate<V> predicate) {
return create(name, filter, (e, h) -> predicate.test(h)); return create(name, filter, (e, h) -> predicate.test(h));
} }
public static <E extends Event> EventNode<E> tag(@NotNull String name, @Contract(value = "_, _, _ -> new", pure = true)
@NotNull EventFilter<E, ? extends TagReadable> filter, public static <E extends Event> @NotNull EventNode<E> tag(@NotNull String name,
@NotNull Tag<?> tag) { @NotNull EventFilter<E, ? extends TagReadable> filter,
@NotNull Tag<?> tag) {
return create(name, filter, (e, h) -> h.hasTag(tag)); return create(name, filter, (e, h) -> h.hasTag(tag));
} }
public static <E extends Event, V> EventNode<E> tag(@NotNull String name, @Contract(value = "_, _, _, _ -> new", pure = true)
@NotNull EventFilter<E, ? extends TagReadable> filter, public static <E extends Event, V> @NotNull EventNode<E> tag(@NotNull String name,
@NotNull Tag<V> tag, @NotNull EventFilter<E, ? extends TagReadable> filter,
@NotNull Predicate<@Nullable V> consumer) { @NotNull Tag<V> tag,
@NotNull Predicate<@Nullable V> consumer) {
return create(name, filter, (e, h) -> consumer.test(h.getTag(tag))); return create(name, filter, (e, h) -> consumer.test(h.getTag(tag)));
} }
@ -138,23 +146,28 @@ public class EventNode<T extends Event> {
} }
} }
@Contract(pure = true)
public @NotNull String getName() { public @NotNull String getName() {
return name; return name;
} }
@Contract(pure = true)
public int getPriority() { public int getPriority() {
return priority; return priority;
} }
public EventNode<T> setPriority(int priority) { @Contract(value = "_ -> this")
public @NotNull EventNode<T> setPriority(int priority) {
this.priority = priority; this.priority = priority;
return this; return this;
} }
@Contract(pure = true)
public @Nullable EventNode<? super T> getParent() { public @Nullable EventNode<? super T> getParent() {
return parent; return parent;
} }
@Contract(pure = true)
public @NotNull Set<@NotNull EventNode<T>> getChildren() { public @NotNull Set<@NotNull EventNode<T>> getChildren() {
return Collections.unmodifiableSet(children); return Collections.unmodifiableSet(children);
} }
@ -167,7 +180,8 @@ public class EventNode<T extends Event> {
return findChild(name, eventType); return findChild(name, eventType);
} }
public EventNode<T> addChild(@NotNull EventNode<? extends T> child) { @Contract(value = "_ -> this")
public @NotNull EventNode<T> addChild(@NotNull EventNode<? extends T> child) {
synchronized (GLOBAL_CHILD_LOCK) { synchronized (GLOBAL_CHILD_LOCK) {
Check.stateCondition(child.parent != null, "Node already has a parent"); Check.stateCondition(child.parent != null, "Node already has a parent");
Check.stateCondition(Objects.equals(parent, child), "Cannot have a child as parent"); Check.stateCondition(Objects.equals(parent, child), "Cannot have a child as parent");
@ -189,7 +203,8 @@ public class EventNode<T extends Event> {
return this; return this;
} }
public EventNode<T> removeChild(@NotNull EventNode<? extends T> child) { @Contract(value = "_ -> this")
public @NotNull EventNode<T> 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) {
@ -209,15 +224,18 @@ public class EventNode<T extends Event> {
return this; return this;
} }
public EventNode<T> addListener(@NotNull EventListener<? extends T> listener) { @Contract(value = "_ -> this")
public @NotNull EventNode<T> addListener(@NotNull EventListener<? extends T> listener) {
return addListener0(listener); return addListener0(listener);
} }
public <E extends T> EventNode<T> addListener(@NotNull Class<E> eventType, @NotNull Consumer<@NotNull E> listener) { @Contract(value = "_, _ -> this")
public <E extends T> @NotNull EventNode<T> addListener(@NotNull Class<E> eventType, @NotNull Consumer<@NotNull E> listener) {
return addListener0(EventListener.of(eventType, listener)); return addListener0(EventListener.of(eventType, listener));
} }
public EventNode<T> removeListener(@NotNull EventListener<? extends T> listener) { @Contract(value = "_ -> this")
public @NotNull EventNode<T> removeListener(@NotNull EventListener<? extends T> listener) {
synchronized (GLOBAL_CHILD_LOCK) { synchronized (GLOBAL_CHILD_LOCK) {
final var eventType = listener.getEventType(); final var eventType = listener.getEventType();
var entry = listenerMap.get(eventType); var entry = listenerMap.get(eventType);