feat: 添加 CompletableTask 功能
This commit is contained in:
@@ -0,0 +1,164 @@
|
||||
package cn.hamster3.mc.plugin.core.common.util.async;
|
||||
|
||||
import cn.hamster3.mc.plugin.core.common.api.CoreAPI;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
@SuppressWarnings({"unused", "CallToPrintStackTrace"})
|
||||
public class CompletableTask<T> {
|
||||
@NotNull
|
||||
private final List<Consumer<T>> onSuccess;
|
||||
@NotNull
|
||||
private final List<Consumer<Throwable>> onFailed;
|
||||
@NotNull
|
||||
private State state;
|
||||
private T value;
|
||||
private Throwable throwable;
|
||||
|
||||
public CompletableTask() {
|
||||
state = State.WAITING;
|
||||
onSuccess = new ArrayList<>();
|
||||
onFailed = new ArrayList<>();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static CompletableTask<Void> runAsync(@NotNull RunTask runTask) {
|
||||
CompletableTask<Void> task = new CompletableTask<>();
|
||||
CoreAPI.getInstance().getExecutorService().submit(() -> {
|
||||
try {
|
||||
runTask.run();
|
||||
task.success(null);
|
||||
} catch (Exception e) {
|
||||
task.failed(e);
|
||||
}
|
||||
});
|
||||
return task;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static <T> CompletableTask<T> supplyAsync(@NotNull SupplyTask<T> supplyTask) {
|
||||
CompletableTask<T> task = new CompletableTask<>();
|
||||
CoreAPI.getInstance().getExecutorService().submit(() -> {
|
||||
try {
|
||||
T call = supplyTask.call();
|
||||
task.success(call);
|
||||
} catch (Exception e) {
|
||||
task.failed(e);
|
||||
}
|
||||
});
|
||||
return task;
|
||||
}
|
||||
|
||||
public void success(@Nullable T value) {
|
||||
if (state != State.WAITING) {
|
||||
throw new IllegalStateException();
|
||||
}
|
||||
this.value = value;
|
||||
state = State.SUCCESS;
|
||||
synchronized (this) {
|
||||
notifyAll();
|
||||
}
|
||||
for (Consumer<T> success : onSuccess) {
|
||||
try {
|
||||
success.accept(value);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void failed(@Nullable Throwable throwable) {
|
||||
if (state != State.WAITING) {
|
||||
throw new IllegalStateException();
|
||||
}
|
||||
this.value = null;
|
||||
state = State.FAILED;
|
||||
this.throwable = throwable;
|
||||
synchronized (this) {
|
||||
notifyAll();
|
||||
}
|
||||
for (Consumer<Throwable> consumer : onFailed) {
|
||||
try {
|
||||
consumer.accept(throwable);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public CompletableTask<T> onSuccess(@NotNull Consumer<T> consumer) {
|
||||
if (state == State.SUCCESS) {
|
||||
try {
|
||||
consumer.accept(value);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
} else {
|
||||
onSuccess.add(consumer);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public CompletableTask<T> onFailed(@NotNull Consumer<Throwable> consumer) {
|
||||
if (state == State.FAILED) {
|
||||
try {
|
||||
consumer.accept(throwable);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
} else {
|
||||
onFailed.add(consumer);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public T get() throws Throwable {
|
||||
if (state == State.WAITING) {
|
||||
synchronized (this) {
|
||||
try {
|
||||
wait();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
switch (state) {
|
||||
case SUCCESS:
|
||||
return value;
|
||||
case FAILED:
|
||||
throw throwable;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public T getOrDefault(T defaultValue) {
|
||||
if (state == State.WAITING) {
|
||||
synchronized (this) {
|
||||
try {
|
||||
wait();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
switch (state) {
|
||||
case SUCCESS:
|
||||
return value;
|
||||
case FAILED:
|
||||
return defaultValue;
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
public enum State {
|
||||
WAITING,
|
||||
SUCCESS,
|
||||
FAILED
|
||||
}
|
||||
}
|
@@ -0,0 +1,5 @@
|
||||
package cn.hamster3.mc.plugin.core.common.util.async;
|
||||
|
||||
public interface RunTask {
|
||||
void run() throws Exception;
|
||||
}
|
@@ -0,0 +1,5 @@
|
||||
package cn.hamster3.mc.plugin.core.common.util.async;
|
||||
|
||||
public interface SupplyTask<T> {
|
||||
T call() throws Exception;
|
||||
}
|
Reference in New Issue
Block a user