
net.deechael.khl.task.KaiheilaRunnable Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of khl Show documentation
Show all versions of khl Show documentation
Java sdk for Kaiheila bot development
package net.deechael.khl.task;
import net.deechael.khl.core.KaiheilaObject;
import net.deechael.khl.gate.Gateway;
import org.jetbrains.annotations.NotNull;
/**
* This class is provided as an easy way to handle scheduling tasks.
*/
public abstract class KaiheilaRunnable extends KaiheilaObject implements Runnable {
private Task task;
public KaiheilaRunnable(Gateway gateway) {
super(gateway);
}
public synchronized boolean isCancelled() throws IllegalStateException {
checkScheduled();
return task.isCancelled();
}
public synchronized void cancel() throws IllegalStateException {
getKaiheilaBot().getScheduler().cancelTask(getTaskId());
}
@NotNull
public synchronized Task runTask() throws IllegalArgumentException, IllegalStateException {
checkNotYetScheduled();
return setupTask(getKaiheilaBot().getScheduler().runTask((Runnable) this));
}
@NotNull
public synchronized Task runTaskAsynchronously() throws IllegalArgumentException, IllegalStateException {
checkNotYetScheduled();
return setupTask(getKaiheilaBot().getScheduler().runTaskAsynchronously((Runnable) this));
}
@NotNull
public synchronized Task runTaskLater(long delay) throws IllegalArgumentException, IllegalStateException {
checkNotYetScheduled();
return setupTask(getKaiheilaBot().getScheduler().runTaskLater((Runnable) this, delay));
}
@NotNull
public synchronized Task runTaskLaterAsynchronously(long delay) throws IllegalArgumentException, IllegalStateException {
checkNotYetScheduled();
return setupTask(getKaiheilaBot().getScheduler().runTaskLaterAsynchronously((Runnable) this, delay));
}
@NotNull
public synchronized Task runTaskTimer(long delay, long period) throws IllegalArgumentException, IllegalStateException {
checkNotYetScheduled();
return setupTask(getKaiheilaBot().getScheduler().runTaskTimer((Runnable) this, delay, period));
}
@NotNull
public synchronized Task runTaskTimerAsynchronously(long delay, long period) throws IllegalArgumentException, IllegalStateException {
checkNotYetScheduled();
return setupTask(getKaiheilaBot().getScheduler().runTaskTimerAsynchronously((Runnable) this, delay, period));
}
public synchronized int getTaskId() throws IllegalStateException {
checkScheduled();
return task.getTaskId();
}
private void checkScheduled() {
if (task == null) {
throw new IllegalStateException("Not scheduled yet");
}
}
private void checkNotYetScheduled() {
if (task != null) {
throw new IllegalStateException("Already scheduled as " + task.getTaskId());
}
}
@NotNull
private Task setupTask(@NotNull final Task task) {
this.task = task;
return task;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy