Package org.javimmutable.collections
Class Computation<T>
java.lang.Object
org.javimmutable.collections.Computation<T>
- All Implemented Interfaces:
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 Summary
Modifier and TypeMethodDescriptionabstract Computation<T> apply(Proc1Throws<T, ? super Exception> proc) Add a step to the computation that executes the procedure on the current value without changing that value.abstract Tcall()Execute the computation and return the final value or throw any exception that terminated the computation.compute()Execute the computation and return aResultcontaining the final value or an exception that terminated the computation.static <T> Computation<T> Produce aComputationthat simply throws an exception.abstract <U> Computation<U> flatMap(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.abstract <U> Computation<U> map(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.static <T> Computation<T> of(Func0Throws<T, ? super Exception> func) Produce aComputationthat evaluations the function and returns its result.static <T> Computation<T> success(T value) Produce aComputationthat simply returns the value.
-
Method Details
-
map
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
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
Execute the computation and return aResultcontaining the final value or an exception that terminated the computation.- Returns:
- the outcome as a
Result
-
call
Execute the computation and return the final value or throw any exception that terminated the computation. -
success
Produce aComputationthat simply returns the value.- Type Parameters:
T- the type of value- Parameters:
value- the value to return- Returns:
- the computation
-
failure
Produce aComputationthat simply throws an exception.- Type Parameters:
T- the type of value- Parameters:
exception- the exception to throw- Returns:
- the computation
-
of
Produce aComputationthat evaluations the function and returns its result.- Type Parameters:
T- the type of the result- Parameters:
func- the function to evaluate- Returns:
- the computation
-