Class Result<T>

java.lang.Object
org.javimmutable.collections.Result<T>
Type Parameters:
T - type of value being computed
Direct Known Subclasses:
Result.Failure, Result.Success

@Immutable public abstract class Result<T> extends Object
Container for the result of some computation. Contains either the computed value or some exception that was thrown when attempting to compute the value. Allows success or failure to be treated.
  • Method Details

    • success

      @Nonnull public static <T> Result<T> success(T value)
      Creates a successful Result containing the given value.
    • failure

      @Nonnull public static <T> Result<T> failure(@Nonnull Exception value)
      Creates a failure Result containing the exception that was thrown.
    • attempt

      @Nonnull public static <T> Result<T> attempt(Callable<T> func)
      Attempts to compute a value and returns an appropriate Result. Captures an Exception thrown and returns a failure(java.lang.Exception) or, if no exception was thrown, returns a success(T) containing the value.
      Parameters:
      func - the computation that should produce a result
      Returns:
      the success or failure result
    • get

      public abstract T get() throws Exception
      Gets the value or throws the exception. Used to unwrap the result so it can be handled using try/catch.
      Returns:
      the value if we are a successful result
      Throws:
      Exception - if we are a failure result
    • orElse

      public abstract T orElse(T defaultValue)
      Converts a failure result into a success result with a specified value. If we are a failure result return a success result containing the specified value. Otherwise return this.
    • orElseGet

      public abstract T orElseGet(java.util.function.Supplier<T> defaultValue)
      Converts a failure result into a success result with the value returned by a Supplier. If we are a failure result return a success result containing the value returned by the Supplier. Otherwise return this. Does not capture any runtime exception thrown by the supplier.
    • map

      @Nonnull public abstract <U> Result<U> map(Func1Throws<T,U,Exception> func)
      Replaces our successful result with a new value computed using the provided function. Simply returns this if we are a failure result.
    • flatMap

      @Nonnull public abstract <U> Result<U> flatMap(Func1<T,Result<U>> func)
      Replaces our successful result with a new value computed using the provided function. Simply returns this if we are a failure result.
    • mapFailure

      @Nonnull public abstract Result<T> mapFailure(Func1Throws<Exception,T,Exception> func)
      Replaces our failure result with a new value computed using the provided function. Simply returns this if we are a success result.
    • flatMapFailure

      @Nonnull public abstract Result<T> flatMapFailure(Func1Throws<Exception,Result<T>,Exception> func)
      Replaces our failure result with a new value computed using the provided function. Simply returns this if we are a success result.
    • apply

      @Nonnull public abstract Result<T> apply(@Nonnull Proc1Throws<T,? super Exception> proc)
      Does nothing if we are a failure result. Calls a function with our value if we are a success result. If the function throws an exception returns a new failure result containing that exception. Otherwise returns this.