Class Computation<T>

java.lang.Object
org.javimmutable.collections.Computation<T>
All Implemented Interfaces:
Callable<T>

@Immutable public abstract class Computation<T> extends Object implements Callable<T>
A deferred computation. Allows multiple processing steps to be queued into an immutable object for later evaluation. Nothing is done until the compute() method is called. An exception at any step stops the computation at that point.
  • Method Details

    • map

      @Nonnull public abstract <U> Computation<U> map(@Nonnull Func1Throws<T,U,? super Exception> func)
      Add a step to the computation that applies the function to the current value to produce a new value.
      Type Parameters:
      U - the type of the new value
      Parameters:
      func - function to apply
      Returns:
      the new computation
    • flatMap

      @Nonnull public abstract <U> Computation<U> flatMap(@Nonnull Func1Throws<T,Computation<U>,? super Exception> func)
      Add a step to the computation that applies the function to the current value to produce a new computation.
      Type Parameters:
      U - the type of the new value
      Parameters:
      func - function to apply
      Returns:
      the new computation
    • apply

      @Nonnull public abstract Computation<T> apply(@Nonnull Proc1Throws<T,? super Exception> proc)
      Add a step to the computation that executes the procedure on the current value without changing that value. If the procedure throws it will terminate the computation at that point.
      Parameters:
      proc - the procedure to apply
      Returns:
      the new computation
    • compute

      @Nonnull public Result<T> compute()
      Execute the computation and return a Result containing the final value or an exception that terminated the computation.
      Returns:
      the outcome as a Result
    • call

      public abstract T call() throws Exception
      Execute the computation and return the final value or throw any exception that terminated the computation.
      Specified by:
      call in interface Callable<T>
      Returns:
      the computed value
      Throws:
      Exception - if the computation terminates due to an exception
    • success

      @Nonnull public static <T> Computation<T> success(T value)
      Produce a Computation that simply returns the value.
      Type Parameters:
      T - the type of value
      Parameters:
      value - the value to return
      Returns:
      the computation
    • failure

      @Nonnull public static <T> Computation<T> failure(Exception exception)
      Produce a Computation that simply throws an exception.
      Type Parameters:
      T - the type of value
      Parameters:
      exception - the exception to throw
      Returns:
      the computation
    • of

      @Nonnull public static <T> Computation<T> of(@Nonnull Func0Throws<T,? super Exception> func)
      Produce a Computation that evaluations the function and returns its result.
      Type Parameters:
      T - the type of the result
      Parameters:
      func - the function to evaluate
      Returns:
      the computation