Package aQute.bnd.result
Interface Result<V>
- Type Parameters:
V- The value type of the Result.
public interface Result<V>
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 where V is the original return type,
i.e. the "happy case" and String is the error type.
Example:
Instead of adding a throws declaration to a function, the return type of the function is instead set to Result
Example:
public Resultdivide(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 TypeMethodDescriptionvoidaccept(ConsumerWithException<? super V> ok, ConsumerWithException<? super String> err) Processes the result.<U> Result<U>asError()If anErr, return this coerced to the desired generic type.static <V> Result<V>err(CharSequence error) Returns anErrcontaining the specifiederror.static <V> Result<V>error()Returns the error of this instance as anOptional.<U> Result<U>flatMap(FunctionWithException<? super V, ? extends Result<? extends U>> mapper) FlatMap the contained value if this is anOkvalue.booleanisErr()Returnstrueif this instance represents anErrvalue,falseotherwise.booleanisOk()Returnstrueif this instance represents anOkvalue,falseotherwise.<U> Result<U>map(FunctionWithException<? super V, ? extends U> mapper) Map the contained value if this is anOkvalue.mapErr(FunctionWithException<? super String, ? extends CharSequence> mapper) Map the contained error if this is anErrvalue.static <V> Result<V>of(V value, CharSequence error) Returns anOkif thevalueparameter is non-nullor anErrotherwise.static <V> Result<V>ok(V value) Returns anOkcontaining the specifiedvalue.Returns the contained value if this is anOkvalue.orElseGet(SupplierWithException<? extends V> orElseSupplier) Returns the contained value if this is anOkvalue.orElseThrow(FunctionWithException<? super String, ? extends R> throwableSupplier) Returns the contained value if this is anOkvalue.recover(FunctionWithException<? super String, ? extends V> recover) Recover the contained error if this is anErrvalue.recoverWith(FunctionWithException<? super String, ? extends Result<? extends V>> recover) Recover the contained error if this is anErrvalue.unwrap()Returns the contained value if this is anOkvalue.unwrap(CharSequence message) Express the expectation that this object is anOkvalue.value()Returns the value of this instance as anOptional.
-
Method Details
-
of
Returns anOkif thevalueparameter is non-nullor anErrotherwise. At least one ofvalueorerrormust be non-null.- Type Parameters:
V- The value type of the Result.- Parameters:
value- Ifvalueis 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.
-
ok
Returns anOkcontaining the specifiedvalue.- Type Parameters:
V- The value type of the Result.- Parameters:
value- The value to contain in theOkresult. May benull.- Returns:
- An
Okresult.
-
err
Returns anErrcontaining the specifiederror.- Type Parameters:
V- The value type of the Result.- Parameters:
error- The error to contain in theErrresult. Must not benull.- Returns:
- An
Errresult.
-
err
-
isOk
boolean isOk()Returnstrueif this instance represents anOkvalue,falseotherwise.- Returns:
trueif this instance represents anOkvalue,falseotherwise.
-
isErr
boolean isErr()Returnstrueif this instance represents anErrvalue,falseotherwise.- Returns:
trueif this instance represents anErrvalue,falseotherwise.
-
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
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
Express the expectation that this object is anOkvalue. Otherwise throws aResultExceptionwith the specified message.- Parameters:
message- The message to pass to a potential ResultException. Must not benull.- Throws:
ResultException- If this is anErrinstance.
-
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
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 Throwable> V orElseThrow(FunctionWithException<? super String, ? extends R> throwableSupplier) throws RReturns the contained value if this is anOkvalue. Otherwise throws the exception supplied by the specified function.- Type Parameters:
R- The exception type.- Parameters:
throwableSupplier- The supplier to supply an exception if this is anErrinstance. Must not benull. The supplier must return a non-nullresult.- Returns:
- The contained value.
- Throws:
R- The exception returned by the throwableSupplier if this is anErrinstance.
-
map
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.- Returns:
- A result containing the mapped value if this is an
Okvalue. Otherwise this.
-
mapErr
Map the contained error if this is anErrvalue. Otherwise return this.- 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
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
Recover the contained error if this is anErrvalue. Otherwise return this.To recover with a recovery value of
null, therecoverWith(FunctionWithException)method must be used. The specified function forrecoverWith(FunctionWithException)can returnResult.ok(null)to supply the desirednullvalue.- Parameters:
recover- The function to recover the contained error into a new value. Must not benull.- Returns:
- A result containing the new non-
nullvalue if this is anErrvalue. Otherwise this if this is anOkvalue or the recover function returnednull.
-
recoverWith
Recover the contained error if this is anErrvalue. Otherwise return this.- Parameters:
recover- The function to recover the contained error into a new result. Must not benull. The function must return a non-nullvalue.- Returns:
- A result if this is an
Errvalue. Otherwise this.
-
accept
Processes the result.- Parameters:
ok- The consumer called this result is anOkvalue. Must not benull.err- The consumer called when this result is anErrvalue. Must not benull.
-
asError
If anErr, return this coerced to the desired generic type.- Type Parameters:
U- The desired generic type of theErr.- Returns:
- this
- Throws:
ResultException- If this is anOkinstance.
-