Package org.javimmutable.collections
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
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.
-
Nested Class Summary
-
Method Summary
Modifier and TypeMethodDescriptionapply
(Proc1Throws<T, ? super Exception> proc) Does nothing if we are a failure result.static <T> Result
<T> Attempts to compute a value and returns an appropriateResult
.static <T> Result
<T> Creates a failureResult
containing the exception that was thrown.abstract <U> Result
<U> Replaces our successful result with a new value computed using the provided function.flatMapFailure
(Func1Throws<Exception, Result<T>, Exception> func) Replaces our failure result with a new value computed using the provided function.abstract T
get()
Gets the value or throws the exception.abstract <U> Result
<U> map
(Func1Throws<T, U, Exception> func) Replaces our successful result with a new value computed using the provided function.mapFailure
(Func1Throws<Exception, T, Exception> func) Replaces our failure result with a new value computed using the provided function.abstract T
Converts a failure result into a success result with a specified value.abstract T
Converts a failure result into a success result with the value returned by aSupplier
.static <T> Result
<T> success
(T value) Creates a successfulResult
containing the given value.
-
Method Details
-
success
Creates a successfulResult
containing the given value. -
failure
Creates a failureResult
containing the exception that was thrown. -
attempt
Attempts to compute a value and returns an appropriateResult
. Captures anException
thrown and returns afailure(java.lang.Exception)
or, if no exception was thrown, returns asuccess(T)
containing the value.- Parameters:
func
- the computation that should produce a result- Returns:
- the success or failure result
-
get
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
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
Converts a failure result into a success result with the value returned by aSupplier
. If we are a failure result return a success result containing the value returned by theSupplier
. Otherwise return this. Does not capture any runtime exception thrown by the supplier. -
map
Replaces our successful result with a new value computed using the provided function. Simply returns this if we are a failure result. -
flatMap
Replaces our successful result with a new value computed using the provided function. Simply returns this if we are a failure result. -
mapFailure
Replaces our failure result with a new value computed using the provided function. Simply returns this if we are a success result. -
flatMapFailure
Replaces our failure result with a new value computed using the provided function. Simply returns this if we are a success result. -
apply
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.
-