package dev.tommyjs.futur.promise; import dev.tommyjs.futur.function.ExceptionalConsumer; import dev.tommyjs.futur.function.ExceptionalFunction; import dev.tommyjs.futur.function.ExceptionalRunnable; import dev.tommyjs.futur.function.ExceptionalSupplier; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.util.concurrent.CancellationException; import java.util.concurrent.CompletableFuture; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; import java.util.concurrent.atomic.AtomicReference; import java.util.function.Consumer; public interface Promise { PromiseFactory getFactory(); @NotNull Promise thenRunSync(@NotNull ExceptionalRunnable task); @NotNull Promise thenRunDelayedSync(@NotNull ExceptionalRunnable task, long delay, @NotNull TimeUnit unit); @NotNull Promise thenConsumeSync(@NotNull ExceptionalConsumer task); @NotNull Promise thenConsumeDelayedSync(@NotNull ExceptionalConsumer task, long delay, @NotNull TimeUnit unit); @NotNull Promise thenSupplySync(@NotNull ExceptionalSupplier task); @NotNull Promise thenSupplyDelayedSync(@NotNull ExceptionalSupplier task, long delay, @NotNull TimeUnit unit); @NotNull Promise thenApplySync(@NotNull ExceptionalFunction task); @NotNull Promise thenApplyDelayedSync(@NotNull ExceptionalFunction task, long delay, @NotNull TimeUnit unit); @NotNull Promise thenComposeSync(@NotNull ExceptionalFunction> task); @NotNull Promise thenRunAsync(@NotNull ExceptionalRunnable task); @NotNull Promise thenRunDelayedAsync(@NotNull ExceptionalRunnable task, long delay, @NotNull TimeUnit unit); @NotNull Promise thenConsumeAsync(@NotNull ExceptionalConsumer task); @NotNull Promise thenConsumeDelayedAsync(@NotNull ExceptionalConsumer task, long delay, @NotNull TimeUnit unit); @NotNull Promise thenSupplyAsync(@NotNull ExceptionalSupplier task); @NotNull Promise thenSupplyDelayedAsync(@NotNull ExceptionalSupplier task, long delay, @NotNull TimeUnit unit); @NotNull Promise thenPopulateReference(@NotNull AtomicReference reference); @NotNull Promise thenApplyAsync(@NotNull ExceptionalFunction task); @NotNull Promise thenApplyDelayedAsync(@NotNull ExceptionalFunction task, long delay, @NotNull TimeUnit unit); @NotNull Promise thenComposeAsync(@NotNull ExceptionalFunction> task); @NotNull Promise erase(); default @NotNull Promise logExceptions() { return logExceptions("Exception caught in promise chain"); } @NotNull Promise logExceptions(@NotNull String message); /** * @apiNote Direct listeners run on the same thread as the completion. */ @NotNull Promise addDirectListener(@NotNull PromiseListener listener); @NotNull Promise addDirectListener(@Nullable Consumer successHandler, @Nullable Consumer errorHandler); /** * @apiNote Async listeners are run in parallel. */ @NotNull Promise addAsyncListener(@NotNull AsyncPromiseListener listener); /** * @apiNote Same as addAsyncListener. */ default @NotNull Promise addListener(@NotNull AsyncPromiseListener listener) { return addAsyncListener(listener); } @NotNull Promise addAsyncListener(@Nullable Consumer successHandler, @Nullable Consumer errorHandler); @NotNull Promise onSuccess(@NotNull Consumer listener); @NotNull Promise onError(@NotNull Consumer listener); @NotNull Promise onError(@NotNull Class clazz, @NotNull Consumer listener); @NotNull Promise onCancel(@NotNull Consumer listener); /** * @deprecated Use maxWaitTime instead */ @Deprecated @NotNull Promise timeout(long time, @NotNull TimeUnit unit); /** * @deprecated Use maxWaitTime instead */ @Deprecated default @NotNull Promise timeout(long ms) { return timeout(ms, TimeUnit.MILLISECONDS); } @NotNull Promise maxWaitTime(long time, @NotNull TimeUnit unit); default @NotNull Promise maxWaitTime(long ms) { return maxWaitTime(ms, TimeUnit.MILLISECONDS); } void cancel(@Nullable String reason); default void cancel() { cancel(null); } void complete(@Nullable T result); void completeExceptionally(@NotNull Throwable result); T join(long timeout) throws TimeoutException; @Nullable PromiseCompletion getCompletion(); boolean isCompleted(); @NotNull CompletableFuture toFuture(); }