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 Resultwhere 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
divide(int a, int b) { if (b == 0) { return Result.err("Can't divide by zero!"); } else { return Result.ok(a / b); } }
-
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Modifier and Type Method Description voidaccept(ConsumerWithException<? super V> ok, ConsumerWithException<? super E> err)Terminal function that processes the result or the error<X> Result<X,E>asError()static <V,E>
Result<V,E>err(E error)Returns anErrcontaining the specifiederror.static <V> Result<V,java.lang.String>err(java.lang.String format, java.lang.Object... args)java.util.Optional<E>error()Returns the error of this instance as anOptional.<U> Result<U,E>flatMap(FunctionWithException<? super V,? extends Result<? extends U,? extends E>> mapper)FlatMap the contained value if this is anOkvalue.booleanisErr()booleanisOk()<U> Result<U,E>map(FunctionWithException<? super V,? extends U> mapper)Map the contained value if this is anOkvalue.<F> Result<V,F>mapErr(FunctionWithException<? super E,? extends F> mapper)Map the contained error if this is anErrvalue.static <V,E>
Result<V,E>of(V value, E error)static <V,E>
Result<V,E>ok(V value)Returns anOkcontaining the specifiedvalue.VorElse(V orElse)Returns the contained value if this is anOkvalue.VorElseGet(java.util.function.Supplier<? extends V> orElseSupplier)Returns the contained value if this is anOkvalue.<R extends java.lang.Throwable>
VorElseThrow(FunctionWithException<? super E,? extends R> throwableSupplier)Returns the contained value if this is anOkvalue.Result<V,E>recover(FunctionWithException<? super E,? extends V> recover)Recover the contained error if this is anErrvalue.Vunwrap()Returns the contained value if this is anOkvalue.Vunwrap(java.lang.String message)Express the expectation that this object is anOkvalue.<X extends java.lang.Throwable>
Vunwrap(java.util.function.Function<E,X> constructor)java.util.Optional<V>value()Returns the value of this instance as anOptional.
-
-
-
Method Detail
-
of
static <V,E> Result<V,E> of(V value, E error)
Returns anOkif thevalueparameter is non-nullor anErrotherwise. Either one ofvalueorerrormust not benull.- Type Parameters:
V- The value type of the Result.E- The error type of the Result.- Parameters:
value- If non-null, anOkresult is returned with the specified value.error- Ifvalueisnull, anErrresult is returned with the specified error.- Returns:
- An
Okif thevalueparameter is non-nullor anErrotherwise.
-
err
static <V> Result<V,java.lang.String> err(java.lang.String format, java.lang.Object... args)
-
isOk
boolean isOk()
- Returns:
trueif this instance represents anOkvalue,falseotherwise.
-
isErr
boolean isErr()
- Returns:
trueif this instance represents anErrvalue,falseotherwise.
-
value
java.util.Optional<V> value()
Returns the value of this instance as anOptional. ReturnsOptional.empty()if this is anErrinstance.- Returns:
- The value of this instance as an
Optional. ReturnsOptional.empty()if this is anErrinstance.
-
error
java.util.Optional<E> error()
Returns the error of this instance as anOptional. ReturnsOptional.empty()if this is anOkinstance.- Returns:
- The error of this instance as an
Optional. ReturnsOptional.empty()if this is anOkinstance.
-
unwrap
V unwrap()
Returns the contained value if this is anOkvalue. Otherwise throws aResultException.- Returns:
- The contained value
- Throws:
ResultException- If this is anErrinstance.
-
unwrap
V unwrap(java.lang.String message)
Express the expectation that this object is anOkvalue. Otherwise throws aResultExceptionwith the specified message.- Parameters:
message- The message to pass to a potential ResultException.- Throws:
ResultException- If this is anErrinstance.
-
orElse
V orElse(V orElse)
Returns the contained value if this is anOkvalue. Otherwise returns the specified alternate value.- Parameters:
orElse- The value to return if this is anErrinstance.- Returns:
- The contained value or the alternate value
-
orElseGet
V orElseGet(java.util.function.Supplier<? extends V> orElseSupplier)
Returns the contained value if this is anOkvalue. Otherwise returns the alternate value supplied by the specified supplier.- Parameters:
orElseSupplier- The supplier to supply an alternate value if this is anErrinstance. Must not benull.- Returns:
- The contained value or the alternate value
-
orElseThrow
<R extends java.lang.Throwable> V orElseThrow(FunctionWithException<? super E,? extends R> throwableSupplier) throws R extends java.lang.Throwable
Returns the contained value if this is anOkvalue. Otherwise throws the exception supplied by the specified function.
-
map
<U> Result<U,E> map(FunctionWithException<? super V,? extends U> mapper)
Map the contained value if this is anOkvalue. 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-nullvalue.- Returns:
- A result containing the mapped value if this is an
Okvalue. Otherwise this.
-
mapErr
<F> Result<V,F> mapErr(FunctionWithException<? super E,? extends F> mapper)
Map the contained error if this is anErrvalue. 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-nullerror.- Returns:
- A result containing the mapped error if this is an
Errvalue. 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 anOkvalue. 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-nullresult.- Returns:
- The flatmapped result if this is an
Okvalue. Otherwise this.
-
recover
Result<V,E> recover(FunctionWithException<? super E,? extends V> recover)
Recover the contained error if this is anErrvalue. 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-nullvalue.- Returns:
- A result containing the new value if this is an
Errvalue. Otherwise this.
-
accept
void accept(ConsumerWithException<? super V> ok, ConsumerWithException<? super E> err)
Terminal function that processes the result or the error- Parameters:
ok- the consumer called when okerr- the consumer called when not ok
-
-