Class Result<T>

    • 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
      • defer

        @Nonnull
        @CheckReturnValue
        public static <E> Result<E> defer​(@Nonnull
                                          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:
        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
      • onSuccess

        @Nonnull
        public Result<T> onSuccess​(@Nonnull
                                   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:
        IllegalArgumentException - If the callback 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:
        IllegalStateException - If the result is not successful
      • getFailure

        @Nullable
        public 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
                                Predicate<? super 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:
        IllegalArgumentException - If the provided predicate is null
        IllegalStateException - If the predicate returns true, the cause will be the wrapped exception