feat: 添加 CompletableTask 功能

This commit is contained in:
2024-04-05 16:18:01 +08:00
parent f3e4becf35
commit 271328ef6f
5 changed files with 179 additions and 5 deletions

View File

@@ -26,9 +26,9 @@ repositories {
dependencies {
// 对于 Bukkit 插件
compileOnly("cn.hamster3.mc.plugin:core-bukkit:1.3.3")
compileOnly("cn.hamster3.mc.plugin:core-bukkit:1.3.4-SNAPSHOT")
// 对于 BungeeCord 插件
compileOnly("cn.hamster3.mc.plugin:core-bungee:1.3.3")
compileOnly("cn.hamster3.mc.plugin:core-bungee:1.3.4-SNAPSHOT")
}
```
@@ -54,13 +54,13 @@ dependencies {
<dependency>
<groupId>cn.hamster3.mc.plugin</groupId>
<artifactId>core-bukkit</artifactId>
<version>1.3.3</version>
<version>1.3.4-SNAPSHOT</version>
</dependency>
<!--对于 BungeeCord 插件-->
<dependency>
<groupId>cn.hamster3.mc.plugin</groupId>
<artifactId>core-bungee</artifactId>
<version>1.3.3</version>
<version>1.3.4-SNAPSHOT</version>
</dependency>
</dependencies>
</project>

View File

@@ -5,7 +5,7 @@ plugins {
}
group = "cn.hamster3.mc.plugin"
version = "1.3.3"
version = "1.3.4-SNAPSHOT"
description = "叁只仓鼠的 Minecraft 插件开发通用工具包"
subprojects {

View File

@@ -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
}
}

View File

@@ -0,0 +1,5 @@
package cn.hamster3.mc.plugin.core.common.util.async;
public interface RunTask {
void run() throws Exception;
}

View File

@@ -0,0 +1,5 @@
package cn.hamster3.mc.plugin.core.common.util.async;
public interface SupplyTask<T> {
T call() throws Exception;
}