Package aQute.bnd.service.result
Interface Result<V,E>
- Type Parameters:
V
- The value type of the Result.E
- The error type of the Result.
public interface Result<V,E>
The Result type is an alternative way of chaining together functions in a
functional programming style while hiding away error handling structures such
as try-catch-blocks and conditionals.
Instead of adding a throws declaration to a function, the return type of the function is instead set to Result<V, E> where V is the original return type, i.e. the "happy case" and E is the error type, usually the Exception type or a String if an error explanation is sufficient.
Example:
Instead of adding a throws declaration to a function, the return type of the function is instead set to Result<V, E> where V is the original return type, i.e. the "happy case" and E is the error type, usually the Exception type or a String if an error explanation is sufficient.
Example:
public Result<Float, String> divide(int a, int b) { if (b == 0) { return Result.err("Can't divide by zero!"); } else { return Result.ok(a / b); } }
-
Method Summary
Modifier and TypeMethodDescriptionvoid
accept
(ConsumerWithException<? super V> ok, ConsumerWithException<? super E> err) Terminal function that processes the result or the errorasError()
static <V,
E> Result<V, E> err
(E error) Returns anErr
containing the specifiederror
.error()
Returns the error of this instance as anOptional
.flatMap
(FunctionWithException<? super V, ? extends Result<? extends U, ? extends E>> mapper) FlatMap the contained value if this is anOk
value.boolean
isErr()
boolean
isOk()
map
(FunctionWithException<? super V, ? extends U> mapper) Map the contained value if this is anOk
value.mapErr
(FunctionWithException<? super E, ? extends F> mapper) Map the contained error if this is anErr
value.static <V,
E> Result<V, E> of
(V value, E error) static <V,
E> Result<V, E> ok
(V value) Returns anOk
containing the specifiedvalue
.Returns the contained value if this is anOk
value.Returns the contained value if this is anOk
value.orElseThrow
(FunctionWithException<? super E, ? extends R> throwableSupplier) Returns the contained value if this is anOk
value.recover
(FunctionWithException<? super E, ? extends V> recover) Recover the contained error if this is anErr
value.unwrap()
Returns the contained value if this is anOk
value.Express the expectation that this object is anOk
value.value()
Returns the value of this instance as anOptional
.
-
Method Details
-
of
Returns anOk
if thevalue
parameter is non-null
or anErr
otherwise. Either one ofvalue
orerror
must not benull
.- Type Parameters:
V
- The value type of the Result.E
- The error type of the Result.- Parameters:
value
- If non-null
, anOk
result is returned with the specified value.error
- Ifvalue
isnull
, anErr
result is returned with the specified error.- Returns:
- An
Ok
if thevalue
parameter is non-null
or anErr
otherwise.
-
ok
Returns anOk
containing the specifiedvalue
. -
err
Returns anErr
containing the specifiederror
. -
err
-
isOk
boolean isOk()- Returns:
true
if this instance represents anOk
value,false
otherwise.
-
isErr
boolean isErr()- Returns:
true
if this instance represents anErr
value,false
otherwise.
-
value
-
error
-
unwrap
V unwrap()Returns the contained value if this is anOk
value. Otherwise throws aResultException
.- Returns:
- The contained value
- Throws:
ResultException
- If this is anErr
instance.
-
unwrap
Express the expectation that this object is anOk
value. Otherwise throws aResultException
with the specified message.- Parameters:
message
- The message to pass to a potential ResultException.- Throws:
ResultException
- If this is anErr
instance.
-
orElse
Returns the contained value if this is anOk
value. Otherwise returns the specified alternate value.- Parameters:
orElse
- The value to return if this is anErr
instance.- Returns:
- The contained value or the alternate value
-
orElseGet
Returns the contained value if this is anOk
value. Otherwise returns the alternate value supplied by the specified supplier.- Parameters:
orElseSupplier
- The supplier to supply an alternate value if this is anErr
instance. Must not benull
.- Returns:
- The contained value or the alternate value
-
orElseThrow
<R extends Throwable> V orElseThrow(FunctionWithException<? super E, ? extends R> throwableSupplier) throws RReturns the contained value if this is anOk
value. Otherwise throws the exception supplied by the specified function. -
map
Map the contained value if this is anOk
value. Otherwise return this.- Type Parameters:
U
- The new value type.- Parameters:
mapper
- The function to map the contained value into a new value. Must not benull
. The function must return a non-null
value.- Returns:
- A result containing the mapped value if this is an
Ok
value. Otherwise this.
-
mapErr
Map the contained error if this is anErr
value. Otherwise return this.- Type Parameters:
F
- The new error type.- Parameters:
mapper
- The function to map the contained error into a new error. Must not benull
. The function must return a non-null
error.- Returns:
- A result containing the mapped error if this is an
Err
value. Otherwise this.
-
flatMap
<U> Result<U,E> flatMap(FunctionWithException<? super V, ? extends Result<? extends U, ? extends E>> mapper) FlatMap the contained value if this is anOk
value. Otherwise return this.- Type Parameters:
U
- The new value type.- Parameters:
mapper
- The function to flatmap the contained value into a new result. Must not benull
. The function must return a non-null
result.- Returns:
- The flatmapped result if this is an
Ok
value. Otherwise this.
-
recover
Recover the contained error if this is anErr
value. Otherwise return this.- Parameters:
recover
- The function to recover the contained error into a new value. Must not benull
. The function must return a non-null
value.- Returns:
- A result containing the new value if this is an
Err
value. Otherwise this.
-
accept
Terminal function that processes the result or the error- Parameters:
ok
- the consumer called when okerr
- the consumer called when not ok
-
asError
-
unwrap
- Throws:
X extends Throwable
-