Class AwaitResult<T>
- Type Parameters:
T- the value type
Awaitable.allSettled() to
report each individual task's result without short-circuiting on
failure.
An AwaitResult is either a success
carrying a value, or a failure carrying a
Throwable.
This type follows the value object pattern: two instances are equal if and only if they have the same success/failure state and carry equal values or errors. Immutability is enforced — all fields are final and no mutating methods are exposed.
Functional composition is supported via map(Function),
enabling transformation chains without unwrapping:
AwaitResult<Integer> length = AwaitResult.success("hello").map(String::length)
Inspired by Kotlin's Result, Rust's Result<T, E>,
and C#'s pattern of structured success/error responses.
- Since:
- 6.0.0
-
Method Summary
Modifier and TypeMethodDescriptionbooleanCompares this result to another object for equality.static <T> AwaitResult<T>Creates a failure result with the given exception.getError()Returns the exception if failed.Returns the value if successful, or applies the given function to the error to produce a fallback value.getValue()Returns the value if successful.inthashCode()Returns a hash code consistent withequals(Object).booleanReturnstrueif this result represents a failed completion.booleanReturnstrueif this result represents a successful completion.<U> AwaitResult<U>Transforms a successful result's value using the given function.static <T> AwaitResult<T>Creates a successful result with the given value.toString()Returns a human-readable representation of this result:AwaitResult.Success[value]orAwaitResult.Failure[error].
-
Method Details
-
success
Creates a successful result with the given value.- Type Parameters:
T- the value type- Parameters:
value- the computation result (may benull)- Returns:
- a success result wrapping the value
-
failure
Creates a failure result with the given exception.- Type Parameters:
T- the value type (never actually used, since the result is a failure)- Parameters:
error- the exception that caused the failure; must not benull- Returns:
- a failure result wrapping the exception
- Throws:
NullPointerException- iferrorisnull
-
isSuccess
public boolean isSuccess()Returnstrueif this result represents a successful completion. -
isFailure
public boolean isFailure()Returnstrueif this result represents a failed completion. -
getValue
Returns the value if successful.- Returns:
- the computation result
- Throws:
IllegalStateException- if this result represents a failure
-
getError
Returns the exception if failed.- Returns:
- the exception that caused the failure
- Throws:
IllegalStateException- if this result represents a success
-
getOrElse
Returns the value if successful, or applies the given function to the error to produce a fallback value.- Parameters:
fallback- the function to apply to the error if this result is a failure; must not benull- Returns:
- the value, or the fallback function's result
-
map
Transforms a successful result's value using the given function. If this result is a failure, the error is propagated unchanged.This is the functor
mapoperation, enabling value transformation without explicit unwrapping:AwaitResult<String> name = AwaitResult.success("Groovy") AwaitResult<Integer> length = name.map(String::length) assert length.value == 6- Type Parameters:
U- the type of the mapped value- Parameters:
fn- the mapping function; must not benull- Returns:
- a new success result with the mapped value, or the original failure unchanged
- Throws:
NullPointerException- iffnisnull
-
equals
Compares this result to another object for equality.Two
AwaitResultinstances are equal if and only if they have the same success/failure state and carry equal values or errors. -
hashCode
public int hashCode()Returns a hash code consistent withequals(Object). -
toString
Returns a human-readable representation of this result:AwaitResult.Success[value]orAwaitResult.Failure[error].
-