mirror of
https://github.com/tommyskeff/futur4j.git
synced 2026-01-18 07:16:45 +00:00
add more direct listener methods
This commit is contained in:
@@ -103,6 +103,58 @@ public abstract class AbstractPromise<T, F> implements Promise<T> {
|
|||||||
return completion.getResult();
|
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
|
@Override
|
||||||
public @NotNull Promise<Void> thenRunSync(@NotNull ExceptionalRunnable task) {
|
public @NotNull Promise<Void> thenRunSync(@NotNull ExceptionalRunnable task) {
|
||||||
return thenApplySync(result -> {
|
return thenApplySync(result -> {
|
||||||
|
|||||||
@@ -19,6 +19,16 @@ public interface Promise<T> {
|
|||||||
|
|
||||||
PromiseFactory getFactory();
|
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> thenRunSync(@NotNull ExceptionalRunnable task);
|
||||||
|
|
||||||
@NotNull Promise<Void> thenRunDelayedSync(@NotNull ExceptionalRunnable task, long delay, @NotNull TimeUnit unit);
|
@NotNull Promise<Void> thenRunDelayedSync(@NotNull ExceptionalRunnable task, long delay, @NotNull TimeUnit unit);
|
||||||
|
|||||||
Reference in New Issue
Block a user