add more direct listener methods

This commit is contained in:
WhatCats
2024-05-25 21:17:14 +02:00
parent 363669d2c6
commit 4251f2fd82
2 changed files with 62 additions and 0 deletions

View File

@@ -103,6 +103,58 @@ public abstract class AbstractPromise<T, F> implements Promise<T> {
return completion.getResult();
}
@Override
public @NotNull Promise<Void> thenRun(@NotNull ExceptionalRunnable task) {
return thenApply(result -> {
task.run();
return null;
});
}
@Override
public @NotNull Promise<Void> thenConsume(@NotNull ExceptionalConsumer<T> task) {
return thenApply(result -> {
task.accept(result);
return null;
});
}
@Override
public <V> @NotNull Promise<V> thenSupply(@NotNull ExceptionalSupplier<V> task) {
return thenApply(result -> task.get());
}
@Override
public <V> @NotNull Promise<V> thenApply(@NotNull ExceptionalFunction<T, V> task) {
Promise<V> promise = getFactory().unresolved();
addDirectListener(
res -> createRunnable(res, promise, task).run(),
promise::completeExceptionally
);
propagateCancel(promise, this);
return promise;
}
@Override
public <V> @NotNull Promise<V> thenCompose(@NotNull ExceptionalFunction<T, Promise<V>> task) {
Promise<V> promise = getFactory().unresolved();
thenApply(task).addDirectListener(
nestedPromise -> {
if (nestedPromise == null) {
promise.complete(null);
} else {
propagateResult(nestedPromise, promise);
propagateCancel(promise, nestedPromise);
}
},
promise::completeExceptionally
);
propagateCancel(promise, this);
return promise;
}
@Override
public @NotNull Promise<Void> thenRunSync(@NotNull ExceptionalRunnable task) {
return thenApplySync(result -> {

View File

@@ -19,6 +19,16 @@ public interface Promise<T> {
PromiseFactory getFactory();
@NotNull Promise<Void> thenRun(@NotNull ExceptionalRunnable task);
@NotNull Promise<Void> thenConsume(@NotNull ExceptionalConsumer<T> task);
<V> @NotNull Promise<V> thenSupply(@NotNull ExceptionalSupplier<V> task);
<V> @NotNull Promise<V> thenApply(@NotNull ExceptionalFunction<T, V> task);
<V> @NotNull Promise<V> thenCompose(@NotNull ExceptionalFunction<T, Promise<V>> task);
@NotNull Promise<Void> thenRunSync(@NotNull ExceptionalRunnable task);
@NotNull Promise<Void> thenRunDelayedSync(@NotNull ExceptionalRunnable task, long delay, @NotNull TimeUnit unit);