Class SettablePromise<T>

  • Type Parameters:
    T - result type
    All Implemented Interfaces:
    AsyncComputation<T>, Callback<T>, Promisable<T>, Promise<T>

    public final class SettablePromise<T>
    extends Object
    implements Callback<T>
    Represents a Promise which can be completed or completedExceptionally manually at once or later in the future.

    Can be used as root Promise to start execution of chain of Promises or when you want wrap your actions in Promise.

    • Field Detail

      • result

        protected T result
      • exception

        @Nullable
        protected @Nullable Throwable exception
      • next

        @Nullable
        protected @Nullable Callback<? super T> next
    • Constructor Detail

      • SettablePromise

        public SettablePromise()
    • Method Detail

      • accept

        public void accept​(T result,
                           @Nullable
                           @Nullable Throwable e)
        Accepts the provided values and performs this operation on them. If the Throwable e is null, provided result will be set to this SettablePromise.

        Otherwise, Throwable e will be set.

        Specified by:
        accept in interface Callback<T>
        Parameters:
        result - a value to be set to this SettablePromise if e is null
        e - a Throwable, which will be set to this SettablePromise if not null
      • set

        public void set​(T result)
        Sets the result of this SettablePromise and completes it. AssertionError is thrown when you try to set result for an already completed Promise.
      • setException

        public void setException​(@NotNull
                                 @NotNull Throwable e)
        Sets exception and completes this SettablePromise exceptionally. AssertionError is thrown when you try to set exception for an already completed Promise.
        Parameters:
        e - exception
      • trySet

        public boolean trySet​(T result)
        Tries to set provided result for this SettablePromise if it is not completed yet. Otherwise does nothing.
      • trySetException

        public boolean trySetException​(@NotNull
                                       @NotNull Throwable e)
        Tries to set provided e exception for this SettablePromise if it is not completed yet. Otherwise does nothing.
      • trySet

        public boolean trySet​(T result,
                              @Nullable
                              @Nullable Throwable e)
        Tries to set result or exception for this SettablePromise if it not completed yet. Otherwise does nothing.
      • post

        public void post​(T result)
      • postException

        public void postException​(@NotNull
                                  @NotNull Throwable e)
      • post

        public void post​(T result,
                         @Nullable
                         @Nullable Throwable e)
      • tryPost

        public void tryPost​(T result)
      • tryPostException

        public void tryPostException​(@NotNull
                                     @NotNull Throwable e)
      • tryPost

        public void tryPost​(T result,
                            @Nullable
                            @Nullable Throwable e)
      • describe

        public String describe()
      • reset

        public void reset()
      • resetCallbacks

        public void resetCallbacks()
      • isComplete

        public final boolean isComplete()
        Specified by:
        isComplete in interface Promise<T>
      • isResult

        public final boolean isResult()
        Specified by:
        isResult in interface Promise<T>
      • isException

        public final boolean isException()
        Specified by:
        isException in interface Promise<T>
      • getResult

        public T getResult()
        Specified by:
        getResult in interface Promise<T>
      • getTry

        public io.activej.common.collection.Try<T> getTry()
        Specified by:
        getTry in interface Promise<T>
      • complete

        protected void complete​(@Nullable
                                T value,
                                @Nullable
                                @Nullable Throwable e)
      • complete

        protected void complete​(@Nullable
                                T value)
      • completeExceptionally

        protected void completeExceptionally​(@Nullable
                                             @Nullable Throwable e)
      • tryComplete

        protected boolean tryComplete​(@Nullable
                                      T value,
                                      @Nullable
                                      @Nullable Throwable e)
      • tryComplete

        protected boolean tryComplete​(@Nullable
                                      T value)
      • tryCompleteExceptionally

        protected boolean tryCompleteExceptionally​(@NotNull
                                                   @NotNull Throwable e)
      • next

        @NotNull
        public <U,​P extends Callback<? super T> & Promise<U>> @NotNull Promise<U> next​(@NotNull
                                                                                             P promise)
        Description copied from interface: Promise
        Executes given promise after execution of this Promise completes.
        Specified by:
        next in interface Promise<T>
        Type Parameters:
        U - type of result
        Parameters:
        promise - given promise
        Returns:
        subscribed Promise
      • subscribe

        protected void subscribe​(@NotNull
                                 @NotNull Callback<? super T> callback)
      • map

        @NotNull
        public <U> @NotNull Promise<U> map​(@NotNull
                                           @NotNull Function<? super T,​? extends U> fn)
        Description copied from interface: Promise
        Returns a new Promise which is executed with this Promise's result as the argument to the provided function when this Promise completes successfully.
        Specified by:
        map in interface Promise<T>
        Parameters:
        fn - function to be applied to this Promise when it completes successfully
        Returns:
        new Promise which is the result of function applied to the result of this Promise
        See Also:
        CompletionStage.thenApply(Function)
      • mapEx

        @NotNull
        public <U> @NotNull Promise<U> mapEx​(@NotNull
                                             @NotNull BiFunction<? super T,​Throwable,​? extends U> fn)
        Description copied from interface: Promise
        Returns a new Promise which is executed with this Promise's result as the argument to the provided function when this Promise completes either successfully (when exception is null) or with an exception.
        Specified by:
        mapEx in interface Promise<T>
        Parameters:
        fn - function to be applied to this Promise when it completes either successfully or with an exception
        Returns:
        new Promise which is the result of function applied to the result of this Promise
      • then

        @NotNull
        public <U> @NotNull Promise<U> then​(@NotNull
                                            @NotNull Function<? super T,​? extends Promise<? extends U>> fn)
        Description copied from interface: Promise
        Returns a new Promise which, when this Promise completes successfully, is executed with this Promise's result as the argument to the supplied function.
        Specified by:
        then in interface Promise<T>
        Parameters:
        fn - to be applied
      • then

        @NotNull
        public <U> @NotNull Promise<U> then​(@NotNull
                                            @NotNull Supplier<? extends Promise<? extends U>> fn)
        Specified by:
        then in interface Promise<T>
      • thenEx

        @NotNull
        public <U> @NotNull Promise<U> thenEx​(@NotNull
                                              @NotNull BiFunction<? super T,​Throwable,​? extends Promise<? extends U>> fn)
        Description copied from interface: Promise
        Returns a new Promise which, when this Promise completes either successfully (if exception is null) or exceptionally (if exception is not null), is executed with this Promise's result as the argument to the supplied function.
        Specified by:
        thenEx in interface Promise<T>
        Parameters:
        fn - to be applied to the result of this Promise
        Returns:
        new Promise
      • whenComplete

        @NotNull
        public @NotNull Promise<T> whenComplete​(@NotNull
                                                @NotNull Callback<? super T> action)
        Description copied from interface: Promise
        Subscribes given action to be executed after this Promise completes and returns a new Promise.
        Specified by:
        whenComplete in interface Promise<T>
        Parameters:
        action - to be executed
      • whenComplete

        @NotNull
        public @NotNull Promise<T> whenComplete​(@NotNull
                                                @NotNull Runnable action)
        Description copied from interface: Promise
        Subscribes given action to be executed after this Promise completes and returns a new Promise.
        Specified by:
        whenComplete in interface Promise<T>
        Parameters:
        action - to be executed
      • whenResult

        @NotNull
        public @NotNull Promise<T> whenResult​(Consumer<? super T> action)
        Description copied from interface: Promise
        Subscribes given action to be executed after this Promise completes successfully and returns a new Promise.
        Specified by:
        whenResult in interface Promise<T>
        Parameters:
        action - to be executed
      • whenException

        public Promise<T> whenException​(@NotNull
                                        @NotNull Consumer<Throwable> action)
        Description copied from interface: Promise
        Subscribes given action to be executed after this Promise completes exceptionally and returns a new Promise.
        Specified by:
        whenException in interface Promise<T>
        Parameters:
        action - to be executed
      • async

        @NotNull
        public @NotNull Promise<T> async()
        Description copied from interface: Promise
        Ensures that Promise completes asynchronously: if this Promise is already completed, its completion will be posted to next eventloop tick. Otherwise, does nothing.
        Specified by:
        async in interface Promise<T>
      • combine

        @NotNull
        public <U,​V> @NotNull Promise<V> combine​(@NotNull
                                                       @NotNull Promise<? extends U> other,
                                                       @NotNull
                                                       @NotNull BiFunction<? super T,​? super U,​? extends V> fn)
        Description copied from interface: Promise
        Returns a new Promise that, when this and the other given Promise both complete, is executed with the two results as arguments to the supplied function.
        Specified by:
        combine in interface Promise<T>
        Parameters:
        other - the other Promise
        fn - the function to use to compute the value of the returned Promise
        Returns:
        new Promise
      • both

        @NotNull
        public @NotNull Promise<Void> both​(@NotNull
                                           @NotNull Promise<?> other)
        Description copied from interface: Promise
        Returns a new Promise when both this and provided other Promises complete.
        Specified by:
        both in interface Promise<T>
        Parameters:
        other - the other Promise
        Returns:
        Promise of null when both this and other Promise complete
      • recycleToVoid

        @Nullable
        protected static @Nullable Void recycleToVoid​(Object item)
      • either

        @NotNull
        public @NotNull Promise<T> either​(@NotNull
                                          @NotNull Promise<? extends T> other)
        Description copied from interface: Promise
        Returns the Promise which was completed first.
        Specified by:
        either in interface Promise<T>
        Parameters:
        other - the other Promise
        Returns:
        the first completed Promise
      • toTry

        @NotNull
        public @NotNull Promise<io.activej.common.collection.Try<T>> toTry()
        Description copied from interface: Promise
        Returns Promise that always completes successfully with result or exception wrapped in Try.
        Specified by:
        toTry in interface Promise<T>
      • toVoid

        @NotNull
        public @NotNull Promise<Void> toVoid()
        Description copied from interface: Promise
        Waits for result and discards it.
        Specified by:
        toVoid in interface Promise<T>