diff --git a/src/main/java/dev/fiki/forgehax/api/task/IListener.java b/src/main/java/dev/fiki/forgehax/api/task/IListener.java deleted file mode 100644 index 98f36651e..000000000 --- a/src/main/java/dev/fiki/forgehax/api/task/IListener.java +++ /dev/null @@ -1,23 +0,0 @@ -package dev.fiki.forgehax.api.task; - -import dev.fiki.forgehax.api.common.PriorityEnum; - -/** - * Created on 8/5/2017 by fr1kin - */ -public interface IListener { - - boolean register(T func, PriorityEnum priority); - - boolean register(T func); - - boolean registerTemporary(T func, PriorityEnum priority); - - boolean registerTemporary(T func); - - void unregister(T func); - - boolean registerListener(SimpleManagerContainer.Listener listener); - - boolean unregisterListener(SimpleManagerContainer.Listener listener); -} diff --git a/src/main/java/dev/fiki/forgehax/api/task/SimpleManagerContainer.java b/src/main/java/dev/fiki/forgehax/api/task/SimpleManagerContainer.java deleted file mode 100644 index 45c94536f..000000000 --- a/src/main/java/dev/fiki/forgehax/api/task/SimpleManagerContainer.java +++ /dev/null @@ -1,214 +0,0 @@ -package dev.fiki.forgehax.api.task; - -import com.google.common.collect.Lists; -import dev.fiki.forgehax.api.common.PriorityEnum; - -import java.util.Collections; -import java.util.List; -import java.util.Objects; -import java.util.Optional; -import java.util.function.Predicate; -import java.util.stream.Collectors; - -public class SimpleManagerContainer { - - private final List> functions = Lists.newArrayList(); - private final List> listeners = Lists.newArrayList(); - - public SimpleManagerContainer() { - } - - private Optional> get(T func) { - synchronized (functions) { - return functions - .stream() - .filter(member -> member.getFunction() == func) // compare references - .findFirst(); - } - } - - private Optional> find(Predicate> predicate) { - synchronized (functions) { - return functions.stream().filter(predicate).findFirst(); - } - } - - private boolean register(T func, PriorityEnum priority, boolean once) { - synchronized (functions) { - if (get(func).isPresent()) { - throw new IllegalArgumentException("function already registered"); - } - synchronized (listeners) { - listeners.forEach(l -> l.onRegister(func)); - } - boolean r = functions.add(new Member<>(func, priority, once)); - Collections.sort(functions); - return r; - } - } - - public boolean register(T func, PriorityEnum priority) { - return register(func, priority, false); - } - - public boolean register(T func) { - return register(func, PriorityEnum.DEFAULT); - } - - public boolean registerTemporary(T func, PriorityEnum priority) { - return register(func, priority, true); - } - - public boolean registerTemporary(T func) { - return registerTemporary(func, PriorityEnum.DEFAULT); - } - - private void unregister(Member member) { - synchronized (functions) { - synchronized (listeners) { - listeners.forEach(l -> l.onUnregister(member.getFunction())); - } - functions.remove(member); - } - } - - public void unregister(T func) { - synchronized (functions) { - get(func).ifPresent(this::unregister); - } - } - - public boolean registerListener(Listener listener) { - synchronized (listeners) { - return listeners.add(listener); - } - } - - public boolean unregisterListener(Listener listener) { - synchronized (listeners) { - return listeners.remove(listener); - } - } - - public List functions() { - synchronized (functions) { - return functions.stream().map(Member::getFunction).collect(Collectors.toList()); - } - } - - private void setRunning(Member member, boolean state) { - synchronized (listeners) { - if (state) { - listeners.forEach(l -> l.onFunctionStarted(member.getFunction())); - member.setRunning(true); - } else { - listeners.forEach(l -> l.onFunctionStarted(member.getFunction())); - member.setRunning(false); - } - } - } - - public void begin(T function) { - synchronized (functions) { - Member member = get(function).orElse(null); - - if (member == null) { - return; - } - - find(Member::isRunning) - .ifPresent(m -> { - synchronized (listeners) { - listeners.forEach(l -> l.onFunctionStopped(m.getFunction())); - } - m.setRunning(false); - }); - - setRunning(member, true); - } - } - - public void finish(T function) { - synchronized (functions) { - Member member = get(function).orElse(null); - - if (member == null || !member.isRunning()) { - return; - } - - setRunning(member, false); - - if (member.isOnce()) { - unregister(member); - } - } - } - - protected static class Member implements Comparable { - - private final E function; - private final PriorityEnum priority; - private final boolean once; - - private boolean running = false; - - private Member(E function, PriorityEnum priority, boolean once) { - Objects.requireNonNull(function); - Objects.requireNonNull(priority); - this.function = function; - this.priority = priority; - this.once = once; - } - - public E getFunction() { - return function; - } - - public PriorityEnum getPriority() { - return priority; - } - - private boolean isOnce() { - return once; - } - - public boolean isRunning() { - return running; - } - - private void setRunning(boolean running) { - this.running = running; - } - - @Override - public int compareTo(Member o) { - return (isOnce() || o.isOnce()) - ? Boolean.compare(isOnce(), o.isOnce()) - : getPriority().compareTo(o.getPriority()); - } - - @Override - public int hashCode() { - return System.identityHashCode(getFunction()); - } - - @Override - public boolean equals(Object obj) { - return this == obj - || (obj instanceof SimpleManagerContainer.Member - && this.getFunction() - == ((Member) obj).getFunction()); // compare references, don't use equals() - } - } - - public interface Listener { - - void onRegister(E function); - - void onUnregister(E function); - - void onFunctionStarted(E function); - - void onFunctionStopped(E function); - } -} diff --git a/src/main/java/dev/fiki/forgehax/api/task/TaskChain.java b/src/main/java/dev/fiki/forgehax/api/task/TaskChain.java deleted file mode 100644 index 227126ce2..000000000 --- a/src/main/java/dev/fiki/forgehax/api/task/TaskChain.java +++ /dev/null @@ -1,140 +0,0 @@ -package dev.fiki.forgehax.api.task; - -import com.google.common.collect.Lists; -import com.google.common.collect.Queues; - -import java.util.Collection; -import java.util.Iterator; -import java.util.List; -import java.util.Queue; - -public interface TaskChain extends Iterator { - - TaskChain EMPTY = - new TaskChain() { - @Override - public TaskChain then(Object task) { - throw new UnsupportedOperationException(); - } - - @Override - public TaskChain thenLast(Object task) { - throw new UnsupportedOperationException(); - } - - @Override - public boolean hasNext() { - return false; - } - - @Override - public Object next() { - return null; - } - }; - - static Builder builder() { - return new Builder<>(); - } - - static TaskChain singleton(final T taskIn) { - return new TaskChain() { - @Override - public TaskChain then(T task) { - throw new UnsupportedOperationException(); - } - - @Override - public TaskChain thenLast(T task) { - throw new UnsupportedOperationException(); - } - - T task = taskIn; - - @Override - public boolean hasNext() { - return task != null; - } - - @Override - public T next() { - T ret = task; - task = null; - return ret; - } - }; - } - - static TaskChain empty() { - return (TaskChain) EMPTY; - } - - TaskChain then(E task); - - TaskChain thenLast(E task); - - default boolean isEmpty() { - return !hasNext(); - } - - // not really needed anymore - class Builder { - - private final Queue queue = Queues.newArrayDeque(); - - public Builder then(T task) { - queue.add(task); - return this; - } - - public Builder addAll(Collection tsks) { - queue.addAll(tsks); - return this; - } - - public Builder collect(TaskChain ts) { - while (ts.hasNext()) { - queue.add(ts.next()); - } - return this; - } - - public TaskChain build() { - return new DynamicTaskChain<>(queue); - } - } - - class DynamicTaskChain implements TaskChain { - - private final List tasks = Lists.newArrayList(); - - private DynamicTaskChain() { - } - - private DynamicTaskChain(Collection collection) { - tasks.addAll(collection); - } - - @Override - public TaskChain then(T task) { - tasks.add(0, task); // add to head - return this; - } - - @Override - public TaskChain thenLast(T task) { - tasks.add(task); // add to tail - return null; - } - - @Override - public boolean hasNext() { - return !tasks.isEmpty(); - } - - @Override - public T next() { - return tasks.remove(0); - } - } -}