Class Result<T>

  • Type Parameters:
    T - The success type

    public class Result<T>
    extends java.lang.Object
    Represents a computation or task result.
    This result may be a failure or success.

    This is a value type and does not implement Object.equals(Object) or Object.hashCode()!

    Since:
    4.2.1
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static <E> Result<E> defer​(java.util.function.Supplier<? extends E> supplier)
      Creates a result instance from the provided supplier.
      Result<T> expect​(java.util.function.Predicate<? super java.lang.Throwable> predicate)
      Throws the wrapped exception if the provided predicate returns true.
      static <E> Result<E> failure​(java.lang.Throwable error)
      Creates a failure result.
      <U> Result<U> flatMap​(java.util.function.Function<? super T,​? extends Result<U>> function)
      Composite function to convert a result value to another result.
      T get()
      Unwraps the success value of this result.
      java.lang.Throwable getFailure()
      Unwraps the error for this result.
      boolean isFailure()
      True if this result is a failure.
      boolean isSuccess()
      True if this result is a success.
      <U> Result<U> map​(java.util.function.Function<? super T,​? extends U> function)
      Composite function to convert a result value to another value.
      Result<T> onFailure​(java.util.function.Consumer<? super java.lang.Throwable> callback)
      Passive error handler.
      Result<T> onSuccess​(java.util.function.Consumer<? super T> callback)
      Passive success handler.
      static <E> Result<E> success​(E value)
      Creates a successful result.
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Method Detail

      • success

        @Nonnull
        @CheckReturnValue
        public static <E> Result<E> success​(@Nullable
                                            E value)
        Creates a successful result.
        Type Parameters:
        E - The success type
        Parameters:
        value - The success value
        Returns:
        Result
      • failure

        @Nonnull
        @CheckReturnValue
        public static <E> Result<E> failure​(@Nonnull
                                            java.lang.Throwable error)
        Creates a failure result.
        Type Parameters:
        E - The success type
        Parameters:
        error - The failure throwable
        Returns:
        Result
        Throws:
        java.lang.IllegalArgumentException - If the provided error is null
      • defer

        @Nonnull
        @CheckReturnValue
        public static <E> Result<E> defer​(@Nonnull
                                          java.util.function.Supplier<? extends E> supplier)
        Creates a result instance from the provided supplier.
        If the supplier throws an exception, a failure result is returned.
        Type Parameters:
        E - The success type
        Parameters:
        supplier - The supplier
        Returns:
        Result instance with the supplied value or exception failure
        Throws:
        java.lang.IllegalArgumentException - If the supplier is null
      • isFailure

        public boolean isFailure()
        True if this result is a failure.
        Use getFailure() or expect(Predicate) to handle failures.
        Returns:
        True, if this is a failure result
      • isSuccess

        public boolean isSuccess()
        True if this result is a success.
        Use get() or map(Function) to handle success values.
        Returns:
        True, if this is a successful result
      • onFailure

        @Nonnull
        public Result<T> onFailure​(@Nonnull
                                   java.util.function.Consumer<? super java.lang.Throwable> callback)
        Passive error handler.
        This will apply the provided callback if isFailure() is true and return the same result for further chaining.
        Parameters:
        callback - The passive callback
        Returns:
        The same result instance
        Throws:
        java.lang.IllegalArgumentException - If the callback is null
      • onSuccess

        @Nonnull
        public Result<T> onSuccess​(@Nonnull
                                   java.util.function.Consumer<? super T> callback)
        Passive success handler.
        This will apply the provided callback if isSuccess() is true and return the same result for further chaining.
        Parameters:
        callback - The passive callback
        Returns:
        The same result instance
        Throws:
        java.lang.IllegalArgumentException - If the callback is null
      • map

        @Nonnull
        @CheckReturnValue
        public <U> Result<U> map​(@Nonnull
                                 java.util.function.Function<? super T,​? extends U> function)
        Composite function to convert a result value to another value.
        This will only apply the function is isSuccess() is true.
        Type Parameters:
        U - The result type
        Parameters:
        function - The conversion function
        Returns:
        The mapped result
        Throws:
        java.lang.IllegalArgumentException - If the provided function is null
        See Also:
        flatMap(Function)
      • flatMap

        @Nonnull
        @CheckReturnValue
        public <U> Result<U> flatMap​(@Nonnull
                                     java.util.function.Function<? super T,​? extends Result<U>> function)
        Composite function to convert a result value to another result.
        This will only apply the function is isSuccess() is true.
        Type Parameters:
        U - The result type
        Parameters:
        function - The conversion function
        Returns:
        The mapped result
        Throws:
        java.lang.IllegalArgumentException - If the provided function is null
      • get

        public T get()
        Unwraps the success value of this result.
        This only works if isSuccess() is true and throws otherwise.
        Returns:
        The result value
        Throws:
        java.lang.IllegalStateException - If the result is not successful
      • getFailure

        @Nullable
        public java.lang.Throwable getFailure()
        Unwraps the error for this result.
        This will be null if isFailure() is false.
        Returns:
        The error or null
      • expect

        @Nonnull
        public Result<T> expect​(@Nonnull
                                java.util.function.Predicate<? super java.lang.Throwable> predicate)
        Throws the wrapped exception if the provided predicate returns true.
        This will never provide a null error to the predicate. A successful result will never throw.
        Parameters:
        predicate - The test predicate
        Returns:
        The same result instance
        Throws:
        java.lang.IllegalArgumentException - If the provided predicate is null
        java.lang.IllegalStateException - If the predicate returns true, the cause will be the wrapped exception
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object