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 T
call()
Execute the computation and return the final value or throw any exception that terminated the computation.compute()
Execute the computation and return aResult
containing the final value or an exception that terminated the computation.static <T> Computation
<T> Produce aComputation
that 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 aComputation
that evaluations the function and returns its result.static <T> Computation
<T> success
(T value) Produce aComputation
that 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 aResult
containing 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 aComputation
that simply returns the value.- Type Parameters:
T
- the type of value- Parameters:
value
- the value to return- Returns:
- the computation
-
failure
Produce aComputation
that simply throws an exception.- Type Parameters:
T
- the type of value- Parameters:
exception
- the exception to throw- Returns:
- the computation
-
of
Produce aComputation
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
-