diff options
Diffstat (limited to 'dicore3/core/src/main/java/io/dico/dicore/task')
3 files changed, 247 insertions, 247 deletions
diff --git a/dicore3/core/src/main/java/io/dico/dicore/task/BaseTask.java b/dicore3/core/src/main/java/io/dico/dicore/task/BaseTask.java index b400cce..43db459 100644 --- a/dicore3/core/src/main/java/io/dico/dicore/task/BaseTask.java +++ b/dicore3/core/src/main/java/io/dico/dicore/task/BaseTask.java @@ -1,108 +1,108 @@ -package io.dico.dicore.task; - -import org.bukkit.Bukkit; -import org.bukkit.plugin.Plugin; - -import java.util.NoSuchElementException; - -public abstract class BaseTask<T> { - private boolean running = false; - private Integer taskId = null; - private long workTime = 5L; - private int workCount; - - public void start(Plugin plugin, int delay, int period, long workTime) { - doStartChecks(); - this.workTime = workTime; - workCount = 0; - running = true; - - if (delay == -1) { - run(); - if (!running) { - return; - } - delay = period; - } - - taskId = plugin.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, this::run, delay, period); - } - - public void startImmediately(Plugin plugin, int period, long workTime) { - start(plugin, -1, period, workTime); - } - - protected void doStartChecks() { - if (isRunning()) { - throw new IllegalStateException("Can't start when already running"); - } - } - - public void start(Plugin plugin) { - start(plugin, -1, 20, 5L); - } - - protected void onFinish(boolean early) { - } - - protected long getWorkTime() { - return workTime; - } - - protected abstract boolean process(T object); - - private void run() { - workCount++; - final long stop = System.currentTimeMillis() + getWorkTime(); - do { - if (!processNext()) { - return; - } - } while (System.currentTimeMillis() < stop); - } - - public int getTaskId() { - return running ? taskId : -1; - } - - public int getWorkCount() { - return workCount; - } - - public boolean isRunning() { - return running; - } - - protected abstract T supply() throws NoSuchElementException; - - private void cancelTask(boolean early) { - if (taskId != null) { - Bukkit.getScheduler().cancelTask(taskId); - } - running = false; - taskId = null; - onFinish(early); - } - - private boolean processNext() { - T object; - try { - object = supply(); - } catch (NoSuchElementException e) { - cancelTask(false); - return false; - } - - try { - if (process(object)) { - return true; - } - } catch (RuntimeException e) { - e.printStackTrace(); - } - - cancelTask(true); - return false; - } - -} +package io.dico.dicore.task;
+
+import org.bukkit.Bukkit;
+import org.bukkit.plugin.Plugin;
+
+import java.util.NoSuchElementException;
+
+public abstract class BaseTask<T> {
+ private boolean running = false;
+ private Integer taskId = null;
+ private long workTime = 5L;
+ private int workCount;
+
+ public void start(Plugin plugin, int delay, int period, long workTime) {
+ doStartChecks();
+ this.workTime = workTime;
+ workCount = 0;
+ running = true;
+
+ if (delay == -1) {
+ run();
+ if (!running) {
+ return;
+ }
+ delay = period;
+ }
+
+ taskId = plugin.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, this::run, delay, period);
+ }
+
+ public void startImmediately(Plugin plugin, int period, long workTime) {
+ start(plugin, -1, period, workTime);
+ }
+
+ protected void doStartChecks() {
+ if (isRunning()) {
+ throw new IllegalStateException("Can't start when already running");
+ }
+ }
+
+ public void start(Plugin plugin) {
+ start(plugin, -1, 20, 5L);
+ }
+
+ protected void onFinish(boolean early) {
+ }
+
+ protected long getWorkTime() {
+ return workTime;
+ }
+
+ protected abstract boolean process(T object);
+
+ private void run() {
+ workCount++;
+ final long stop = System.currentTimeMillis() + getWorkTime();
+ do {
+ if (!processNext()) {
+ return;
+ }
+ } while (System.currentTimeMillis() < stop);
+ }
+
+ public int getTaskId() {
+ return running ? taskId : -1;
+ }
+
+ public int getWorkCount() {
+ return workCount;
+ }
+
+ public boolean isRunning() {
+ return running;
+ }
+
+ protected abstract T supply() throws NoSuchElementException;
+
+ private void cancelTask(boolean early) {
+ if (taskId != null) {
+ Bukkit.getScheduler().cancelTask(taskId);
+ }
+ running = false;
+ taskId = null;
+ onFinish(early);
+ }
+
+ private boolean processNext() {
+ T object;
+ try {
+ object = supply();
+ } catch (NoSuchElementException e) {
+ cancelTask(false);
+ return false;
+ }
+
+ try {
+ if (process(object)) {
+ return true;
+ }
+ } catch (RuntimeException e) {
+ e.printStackTrace();
+ }
+
+ cancelTask(true);
+ return false;
+ }
+
+}
diff --git a/dicore3/core/src/main/java/io/dico/dicore/task/IteratorTask.java b/dicore3/core/src/main/java/io/dico/dicore/task/IteratorTask.java index e58814c..3ff7be7 100644 --- a/dicore3/core/src/main/java/io/dico/dicore/task/IteratorTask.java +++ b/dicore3/core/src/main/java/io/dico/dicore/task/IteratorTask.java @@ -1,120 +1,120 @@ -package io.dico.dicore.task; - -import java.util.*; -import java.util.function.BiConsumer; -import java.util.function.BiPredicate; -import java.util.function.Consumer; -import java.util.function.Predicate; - -public abstract class IteratorTask<T> extends BaseTask<T> { - - private Iterator<? extends T> iterator; - - public IteratorTask() { - } - - @SuppressWarnings("unchecked") - public IteratorTask(Iterable<? extends T> iterable, boolean clone) { - refresh(iterable, clone); - } - - public IteratorTask(Iterable<? extends T> iterable) { - this(iterable, false); - } - - public IteratorTask(Iterator<? extends T> iterator) { - refresh(iterator); - } - - @Override - protected void doStartChecks() { - super.doStartChecks(); - if (iterator == null) { - throw new IllegalStateException("An iterator must be supplied first"); - } - } - - protected final void refresh(Iterable<? extends T> iterable, boolean clone) { - if (clone) { - Collection<T> collection; - if (!(iterable instanceof Collection)) { - collection = new LinkedList<>(); - for (T next : iterable) { - collection.add(next); - } - } else { - collection = new ArrayList((Collection) iterable); - } - iterator = collection.iterator(); - } else { - iterator = iterable.iterator(); - } - } - - protected final void refresh(Iterator<? extends T> iterator) { - Objects.requireNonNull(iterator); - this.iterator = iterator; - } - - @Override - protected T supply() { - return iterator.next(); - } - - protected void remove() { - iterator.remove(); - } - - // One argument: The processed object - - public static <T> IteratorTask<T> create(Iterable<? extends T> iterable, Consumer<T> processor) { - return create(iterable, false, processor); - } - - public static <T> IteratorTask<T> create(Iterable<? extends T> iterable, boolean clone, Consumer<T> processor) { - return create(iterable, clone, object -> { - processor.accept(object); - return true; - }); - } - - public static <T> IteratorTask<T> create(Iterable<? extends T> iterable, Predicate<T> processor) { - return create(iterable, false, processor); - } - - public static <T> IteratorTask<T> create(Iterable<? extends T> iterable, boolean clone, Predicate<T> processor) { - return new IteratorTask<T>(iterable, clone) { - @Override - protected boolean process(T object) { - return processor.test(object); - } - }; - } - - // Two arguments: the processed object, and a runnable to remove it from the iterator. - - public static <T> IteratorTask<T> create(Iterable<? extends T> iterable, BiConsumer<T, Runnable> processor) { - return create(iterable, false, processor); - } - - public static <T> IteratorTask<T> create(Iterable<? extends T> iterable, boolean clone, BiConsumer<T, Runnable> processor) { - return create(iterable, clone, (object, runnable) -> { - processor.accept(object, runnable); - return true; - }); - } - - public static <T> IteratorTask<T> create(Iterable<? extends T> iterable, BiPredicate<T, Runnable> processor) { - return create(iterable, false, processor); - } - - public static <T> IteratorTask<T> create(Iterable<? extends T> iterable, boolean clone, BiPredicate<T, Runnable> processor) { - return new IteratorTask<T>(iterable, clone) { - @Override - protected boolean process(T object) { - return processor.test(object, this::remove); - } - }; - } - -} +package io.dico.dicore.task;
+
+import java.util.*;
+import java.util.function.BiConsumer;
+import java.util.function.BiPredicate;
+import java.util.function.Consumer;
+import java.util.function.Predicate;
+
+public abstract class IteratorTask<T> extends BaseTask<T> {
+
+ private Iterator<? extends T> iterator;
+
+ public IteratorTask() {
+ }
+
+ @SuppressWarnings("unchecked")
+ public IteratorTask(Iterable<? extends T> iterable, boolean clone) {
+ refresh(iterable, clone);
+ }
+
+ public IteratorTask(Iterable<? extends T> iterable) {
+ this(iterable, false);
+ }
+
+ public IteratorTask(Iterator<? extends T> iterator) {
+ refresh(iterator);
+ }
+
+ @Override
+ protected void doStartChecks() {
+ super.doStartChecks();
+ if (iterator == null) {
+ throw new IllegalStateException("An iterator must be supplied first");
+ }
+ }
+
+ protected final void refresh(Iterable<? extends T> iterable, boolean clone) {
+ if (clone) {
+ Collection<T> collection;
+ if (!(iterable instanceof Collection)) {
+ collection = new LinkedList<>();
+ for (T next : iterable) {
+ collection.add(next);
+ }
+ } else {
+ collection = new ArrayList((Collection) iterable);
+ }
+ iterator = collection.iterator();
+ } else {
+ iterator = iterable.iterator();
+ }
+ }
+
+ protected final void refresh(Iterator<? extends T> iterator) {
+ Objects.requireNonNull(iterator);
+ this.iterator = iterator;
+ }
+
+ @Override
+ protected T supply() {
+ return iterator.next();
+ }
+
+ protected void remove() {
+ iterator.remove();
+ }
+
+ // One argument: The processed object
+
+ public static <T> IteratorTask<T> create(Iterable<? extends T> iterable, Consumer<T> processor) {
+ return create(iterable, false, processor);
+ }
+
+ public static <T> IteratorTask<T> create(Iterable<? extends T> iterable, boolean clone, Consumer<T> processor) {
+ return create(iterable, clone, object -> {
+ processor.accept(object);
+ return true;
+ });
+ }
+
+ public static <T> IteratorTask<T> create(Iterable<? extends T> iterable, Predicate<T> processor) {
+ return create(iterable, false, processor);
+ }
+
+ public static <T> IteratorTask<T> create(Iterable<? extends T> iterable, boolean clone, Predicate<T> processor) {
+ return new IteratorTask<T>(iterable, clone) {
+ @Override
+ protected boolean process(T object) {
+ return processor.test(object);
+ }
+ };
+ }
+
+ // Two arguments: the processed object, and a runnable to remove it from the iterator.
+
+ public static <T> IteratorTask<T> create(Iterable<? extends T> iterable, BiConsumer<T, Runnable> processor) {
+ return create(iterable, false, processor);
+ }
+
+ public static <T> IteratorTask<T> create(Iterable<? extends T> iterable, boolean clone, BiConsumer<T, Runnable> processor) {
+ return create(iterable, clone, (object, runnable) -> {
+ processor.accept(object, runnable);
+ return true;
+ });
+ }
+
+ public static <T> IteratorTask<T> create(Iterable<? extends T> iterable, BiPredicate<T, Runnable> processor) {
+ return create(iterable, false, processor);
+ }
+
+ public static <T> IteratorTask<T> create(Iterable<? extends T> iterable, boolean clone, BiPredicate<T, Runnable> processor) {
+ return new IteratorTask<T>(iterable, clone) {
+ @Override
+ protected boolean process(T object) {
+ return processor.test(object, this::remove);
+ }
+ };
+ }
+
+}
diff --git a/dicore3/core/src/main/java/io/dico/dicore/task/VoidTask.java b/dicore3/core/src/main/java/io/dico/dicore/task/VoidTask.java index 1298c4c..6204bee 100644 --- a/dicore3/core/src/main/java/io/dico/dicore/task/VoidTask.java +++ b/dicore3/core/src/main/java/io/dico/dicore/task/VoidTask.java @@ -1,19 +1,19 @@ -package io.dico.dicore.task; - -import java.util.NoSuchElementException; - -public abstract class VoidTask extends BaseTask<Void> { - - @Override - protected final boolean process(Void object) { - return process(); - } - - @Override - protected final Void supply() throws NoSuchElementException { - return null; - } - - protected abstract boolean process(); - -} +package io.dico.dicore.task;
+
+import java.util.NoSuchElementException;
+
+public abstract class VoidTask extends BaseTask<Void> {
+
+ @Override
+ protected final boolean process(Void object) {
+ return process();
+ }
+
+ @Override
+ protected final Void supply() throws NoSuchElementException {
+ return null;
+ }
+
+ protected abstract boolean process();
+
+}
|