public final class Result<T,E extends java.lang.Throwable> extends java.lang.Object implements java.lang.Iterable<T>
This object is used when chaining computations (e.g. using map-filter operations) that can
produce an error. Instead of raising an exception, the operation can use this object to
encapsulate the result. This is similar to Optional
, but also encapsulate the type of
error in the form of a Throwable
instance that can be raised later.
This is similar to the Either
or Result
type in other functional languages (e.g.
Haskell,
Rust or
Scala)
Typical usage:
empty()
instead of returning null
,
error(java.lang.Throwable)
or fail(java.lang.String)
instead of
throwing an exception,
ok(java.lang.Object)
to return a normal value.
Modifier and Type | Method and Description |
---|---|
Result<?,?> |
and(Result<?,?> other)
Conjunctive chaining.
|
Result<? extends java.lang.Object,? extends java.lang.Throwable> |
andThen(FunctionReference mapper)
Same as
map or flatMap depending on the type returned by mapper . |
Result<?,?> |
apply(Result<?,?> other)
Apply the function contained is this result to the given result.
|
Tuple |
destruct()
Return a
Tuple representing this Result . |
java.lang.Object |
either(FunctionReference mapping,
FunctionReference recover)
Case analysis for the result.
|
java.lang.Object |
either(FunctionReference mapping,
FunctionReference recover,
FunctionReference def)
Three way case analysis for the result.
|
static <T,E extends java.lang.Throwable> |
empty()
Returns an empty
Result . |
boolean |
equals(java.lang.Object o)
Indicate whether some other object is equal to this
Result . |
static <T,E extends java.lang.Throwable> |
error(E throwable)
Returns a failed
Result . |
static <T> Result<T,java.lang.RuntimeException> |
fail(java.lang.String message)
Returns a failed
Result . |
Result<T,E> |
filter(java.util.function.Predicate<? super T> predicate)
If a value is present and matches the given predicate, return a
Result describing the
value, otherwise return an empty Result . |
<U,X extends java.lang.Throwable> |
flatMap(java.util.function.Function<? super T,Result<U,X>> mapper)
If a value is present, apply the provided
Result -bearing mapping function to it,
otherwise return the Result itself. |
Result<java.lang.Object,java.lang.Throwable> |
flatMap(FunctionReference mapper)
Golo compatible version of
flatMap . |
Result<?,?> |
flattened()
Remove one level of result.
|
T |
get()
If a value is present, returns the value, if empty throws
NoSuchElementException ,
otherwise throws the contained error. |
int |
hashCode() |
boolean |
isEmpty() |
boolean |
isError() |
boolean |
isError(java.lang.Class<?> type) |
boolean |
isValue() |
boolean |
isValue(java.lang.Object val) |
java.util.Iterator<T> |
iterator() |
<U,X extends java.lang.Throwable> |
map(java.util.function.Function<? super T,? extends U> mapper)
If a value is present, apply the provided mapping function to it, otherwise return the
Result itself. |
Result<java.lang.Object,java.lang.Throwable> |
map(FunctionReference mapper)
Golo compatible version of
map . |
<X extends java.lang.Throwable> |
mapError(java.util.function.Function<? super E,? extends X> mapper)
If this result is an error, apply the provided mapping function to the contained error,
otherwise return the
Result itself. |
static Result<java.lang.Object,java.lang.Throwable> |
of(java.lang.Object value)
Dynamic polymorphic constructor.
|
static <T,E extends java.lang.Throwable> |
ok(T value)
Returns a valid
Result . |
static <T,E extends java.lang.Throwable> |
option(java.util.Optional<T> opt)
Construct a
Result from a Optional . |
static <T> Result<T,java.util.NoSuchElementException> |
option(java.util.Optional<T> opt,
java.lang.String message)
Construct a
Result from a Optional . |
Result<?,?> |
or(Result<?,?> other)
Disjunctive chaining.
|
T |
orElse(T other)
Return the value of present, otherwise return
other . |
java.lang.Object |
orElseGet(FunctionReference fun)
Return the value of present, otherwise return the result of the invocation of
fun . |
java.lang.Object |
reduce(java.lang.Object init,
FunctionReference func)
Reduce
this using func with init as initial value. |
java.util.List<E> |
toErrorList()
Convert this
Result into a List of error. |
java.util.List<T> |
toList()
Convert this
Result into a List of values. |
java.util.Optional<T> |
toOptional()
Convert this
Result into a Optional describing its value. |
java.util.Optional<E> |
toOptionalError()
Convert this
Result into a Optional describing its error. |
java.lang.String |
toString() |
public static Result<java.lang.Object,java.lang.Throwable> of(java.lang.Object value)
Dynamically dispatch on empty()
, error(java.lang.Throwable)
,
option(java.util.Optional)
or ok(java.lang.Object)
depending on the
value
type. This is mainly useful in Golo code.
value
- the value to encapsulateResult
representing the valuepublic static <T,E extends java.lang.Throwable> Result<T,E> empty()
Result
.
Represents a successful computation that returns nothing.
Result
public static <T,E extends java.lang.Throwable> Result<T,E> ok(T value)
Result
.
Represents a successful computation's result.
T
- the class of the valuevalue
- the possibly-null value to describeResult
containing the value if the specified value is non-null,
otherwise an empty Result
public static <T,E extends java.lang.Throwable> Result<T,E> error(E throwable)
Result
.
Represent a computation that failed.
E
- the class of the throwablethrowable
- the error that occurredResult
containing the throwablepublic static <T,E extends java.lang.Throwable> Result<T,E> option(java.util.Optional<T> opt)
Result
from a Optional
.T
- the class of the valueopt
- the Optional
representing the possibly present valueResult
containing the value if isPresent()
is true
,
otherwise an empty Result
public static <T> Result<T,java.util.NoSuchElementException> option(java.util.Optional<T> opt, java.lang.String message)
Result
from a Optional
.T
- the class of the valueopt
- the Optional
representing the possibly present valuemessage
- a message used to create an error if the Optional
is emptyResult
containing the value if isPresent()
is true
,
otherwise an error containing NoSuchElementException
public static <T> Result<T,java.lang.RuntimeException> fail(java.lang.String message)
Result
.
Represent a computation that failed. This is similar to error(java.lang.Throwable)
but only the message is provided, and a RuntimeException
is automatically created.
message
- the message representing the errorResult
containing a RuntimeException
public T get() throws E extends java.lang.Throwable, java.util.NoSuchElementException
NoSuchElementException
,
otherwise throws the contained error.Result
java.util.NoSuchElementException
- if the Result
is emptyE
- if the Result
is an errorE extends java.lang.Throwable
public java.util.Optional<T> toOptional()
Result
into a Optional
describing its value.Optional
containing the value of this Result
,
or an empty Optional
if isValue()
is false
public java.util.List<T> toList()
Result
into a List
of values.public java.util.List<E> toErrorList()
Result
into a List
of error.public java.util.Iterator<T> iterator()
iterator
in interface java.lang.Iterable<T>
public java.util.Optional<E> toOptionalError()
Result
into a Optional
describing its error.Optional
containing the error of this Result
,
or an empty Optional
if isError()
is false
public T orElse(T other)
other
.other
- the value to return if is emptyother
public java.lang.Object orElseGet(FunctionReference fun) throws java.lang.Throwable
fun
.fun
- the function to invoke if is empty (may return a default value or throw an
exception)fun
java.lang.Throwable
public boolean isEmpty()
true
if there is neither a value nor an error, otherwise false
public boolean isError()
true
if there is an error (and no value), otherwise false
public boolean isError(java.lang.Class<?> type)
type
- the class to test the error fortrue
if the present error is an instance of type
public boolean isValue()
true
if there is a value (and no error), otherwise false
public boolean isValue(java.lang.Object val)
val
- the value to test for presencetrue
if the present value is equal to val
public <U,X extends java.lang.Throwable> Result<U,X> map(java.util.function.Function<? super T,? extends U> mapper)
Result
itself. If the application succeed with a value, return a Result
containing it, if the result is null, return an empty Result
, otherwise return a
Result
capturing the Throwable
that was thrown.U
- The type of the result of the mapping functionmapper
- a mapping function to apply to the value, if presentResult
describing the result of applying the mapping function to the value of
this Result
java.lang.NullPointerException
- if the mapping function is nullpublic <X extends java.lang.Throwable> Result<T,X> mapError(java.util.function.Function<? super E,? extends X> mapper)
Result
itself.
If the application succeed with a value, return a Result
containing it, if the result is null, return an empty Result
, otherwise return a
Result
capturing the Throwable
that was thrown.X
- The type of the result of the mapping functionmapper
- a mapping function to apply to the error, if presentResult
describing the result of applying the mapping function to the error of
this Result
java.lang.NullPointerException
- if the mapping function is nullpublic <U,X extends java.lang.Throwable> Result<U,X> flatMap(java.util.function.Function<? super T,Result<U,X>> mapper)
Result
-bearing mapping function to it,
otherwise return the Result
itself.
If the application succeed, return its result, otherwise return a
Result
capturing the Throwable
that was thrown.U
- The type of the value of the Result
returned by the mapping functionmapper
- a mapping function to apply to the value, if presentResult
java.lang.NullPointerException
- if the mapping function is null
or if it returns null
public Result<java.lang.Object,java.lang.Throwable> flatMap(FunctionReference mapper)
flatMap
.
See issue 277public Result<?,?> flattened()
This is actually equivalent to flatMap(identity)
(or r.flatMap(f)
is equivalent to r.map(f).flattened()
)
For instance:
ok(ok(42)).flattened() == ok(42)
fail("error").flattened() == fail("error")
empty().flattened() == empty()
java.lang.ClassCastException
- when the result does not contain a result.public Result<? extends java.lang.Object,? extends java.lang.Throwable> andThen(FunctionReference mapper)
map
or flatMap
depending on the type returned by mapper
.
This is a generic version for map
and flatMap
:
if mapper
returns a Result
, it's equivalent to flatMap
,
otherwise, it's equivalent to map
.
This allows code such as:
Ok(21): andThen(|x| -> x + 1): andThen(|x| -> Ok(2 * x)) == Ok(42)
public java.lang.Object either(FunctionReference mapping, FunctionReference recover) throws java.lang.Throwable
If the result is a value, apply the first function to it; if it is an error, apply the second function to it.
Note that if the result is empty, i.e. the value is null
,
the mapping
function is applied to null
.
mapping
- the function to apply to the contained valuerecover
- the function to apply to the contained errorjava.lang.Throwable
public java.lang.Object either(FunctionReference mapping, FunctionReference recover, FunctionReference def) throws java.lang.Throwable
If the result is a value, apply the first function to it; if it is an error, apply the second function to it; if it is empty, invoke the third function.
mapping
- the function to apply to the contained valuerecover
- the function to apply to the contained errordef
- the function to invoke if the result is empty (takes no arguments)java.lang.Throwable
public Result<java.lang.Object,java.lang.Throwable> map(FunctionReference mapper)
map
.
See issue 277public Result<T,E> filter(java.util.function.Predicate<? super T> predicate)
Result
describing the
value, otherwise return an empty Result
. If the Result
is empty or is an error,
return the Result
itself.predicate
- a predicate to apply to the value, if presentResult
describing the value of this Result
if it maches the predicatejava.lang.NullPointerException
- if the predicate is nullpublic java.lang.Object reduce(java.lang.Object init, FunctionReference func) throws java.lang.Throwable
this
using func
with init
as initial value.
For instance:
Result.ok("b"): reduce("a", |x, y| -> x + y) == "ab"
Result.empty(): reduce(42, |x, y| -> x + y) == 42
Result.fail("error"): reduce(42, |x, y| -> x + y) == 42
init
- the initial valuefunc
- the aggregation functionjava.lang.Throwable
public Result<?,?> apply(Result<?,?> other) throws java.lang.Throwable
If the function has several parameters, a result containing a partialized version is returned, that can be `apply`ed to subsequent results. This makes `Result` an “applicative functor”.
java.lang.Throwable
public Result<?,?> and(Result<?,?> other)
other
- the other resultother
if this result is a value, otherwise this
public Result<?,?> or(Result<?,?> other)
other
- the other resultother
if this result is an error, otherwise this
public boolean equals(java.lang.Object o)
Result
.
The other object is considered equal if:
Result
and;
equals()
or;
equals
in class java.lang.Object
o
- an object to be tested for equalitytrue
if the other object is equal to this object, otherwise false
public int hashCode()
hashCode
in class java.lang.Object
public java.lang.String toString()
toString
in class java.lang.Object
public Tuple destruct()
Tuple
representing this Result
.
Return a 2-tuple containing the error and the value contained by this Result
, so
that it can be used in a destructuring golo assignment. The first value is the error, and the
second is the correct value (mnemonic: “right” also means “correct”). For instance:
let e, v = Result.ok(42) # e is null and v is 42
let e, v = Result.empty() # e is null and v is null
let e, v = Result.fail("error") # e is RuntimeException("error") and v is null
This allows to deal with error in the same way as Go does for instance.Result