personthecat.catlib.event.LibEvent Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of catlib-quilt Show documentation
Show all versions of catlib-quilt Show documentation
Utilities for serialization, commands, noise generation, IO, and some new data types.
The newest version!
package personthecat.catlib.event;
import personthecat.catlib.data.collections.NonRecursiveObserverSet;
import personthecat.catlib.data.collections.ObserverSet;
import personthecat.catlib.data.collections.SimpleObserverSet;
import java.util.function.Function;
public class LibEvent {
private final ObserverSet listeners;
private final T invoker;
private final T emptyInvoker;
private LibEvent(final ObserverSet listeners, final T invoker, final T emptyInvoker) {
this.listeners = listeners;
this.invoker = invoker;
this.emptyInvoker = emptyInvoker;
}
public static LibEvent create(final Function, T> event) {
return create(new SimpleObserverSet<>(), event);
}
public static LibEvent nonRecursive(final Function, T> event) {
return create(new NonRecursiveObserverSet<>(), event);
}
private static LibEvent create(final ObserverSet listeners, final Function, T> event) {
final T invoker = event.apply(listeners);
return new LibEvent<>(listeners, invoker, invoker);
}
public LibEvent register(final T listener) {
if (!this.listeners.contains(listener)) {
this.listeners.add(listener);
}
return this;
}
public boolean isRegistered(final T listener) {
return this.listeners.contains(listener);
}
public void deregister(final T listener) {
this.listeners.remove(listener);
}
public T invoker() {
return this.invoker;
}
public boolean isEmpty() {
return this.listeners.isEmpty();
}
public void clear() {
this.listeners.clear();
}
}