Interface Result<T extends @UnknownNullability Object>
- Type Parameters:
T- the type, can be nullable.
- All Known Implementing Classes:
Result.Error, Result.Ok
public sealed interface Result<T extends @UnknownNullability Object>
permits Result.Ok<T>, Result.Error<T>
Results are used in
They have two states
To construct simply just do
You should not rely on the identity of results as they are value candidates.
Encoder and Decoder to primarily function as a way of passing back exceptions as values.
They have two states
Result.Ok and Result.Error, you can use pattern matching to extract the values
or use some of the helper methods provided like orElseThrow() or mapResult(Function).
To construct simply just do
new Result.Ok<>(value) and new Result.Error<>("Error message!")
You should not rely on the identity of results as they are value candidates.
-
Nested Class Summary
Nested Classes -
Method Summary
Modifier and TypeMethodDescriptiondefault <S> Result.Error<S> cast()Casts the error to any type if present.default <S extends @UnknownNullability Object>
Result<S> Map theResult.Okresult into the mapper function that creates a new result.Maps theResult.Errorresult to the mapper function and creates a newResult.Errorresult Otherwise, returnsResult.Ok.default <S extends @UnknownNullability Object>
Result<S> default @UnknownNullability TorElse(@UnknownNullability T other) If the resultant is notResult.Ok, returns the other valuedefault TAttempts to get the value insideResult.Okor throws.default TorElseThrow(String message) Attempts to get the value insideResult.Okor throws.
-
Method Details
-
map
@Contract(pure=true) default <S extends @UnknownNullability Object> Result<S> map(Function<T, Result<S>> mapper) Map theResult.Okresult into the mapper function that creates a new result. Otherwise, returns the error.- Type Parameters:
S- the type of the result.- Parameters:
mapper- the new result- Returns:
- the new result or the error.
-
mapResult
@Contract(pure=true) default <S extends @UnknownNullability Object> Result<S> mapResult(Function<T, S> mapper) Maps theResult.Okresult to the mapper function and creates a newResult.Okresult Otherwise, returns the error.
Similar tomap(Function)but instead constructs the result instead.- Type Parameters:
S- the type of the result.- Parameters:
mapper- the new result- Returns:
- the new result or the error.
-
mapError
Maps theResult.Errorresult to the mapper function and creates a newResult.Errorresult Otherwise, returnsResult.Ok.
Similar tomap(Function)but instead constructs the result instead.- Parameters:
mapper- the new result- Returns:
- the new result or the error.
-
orElse
If the resultant is notResult.Ok, returns the other value- Parameters:
other- value to be returned- Returns:
- the resultant
-
orElseThrow
Attempts to get the value insideResult.Okor throws.- Returns:
- the value
- Throws:
IllegalStateException- if this instance ofResult.Error
-
orElseThrow
Attempts to get the value insideResult.Okor throws.- Parameters:
message- the message prefix- Returns:
- the value
- Throws:
IllegalStateException- if this instance ofResult.Error
-
cast
Casts the error to any type if present.
Useful to return the error if it is not the correct type.- Type Parameters:
S- the new result type- Returns:
- the error
- Throws:
ClassCastException- if the result is notResult.Error
-