Module net.minestom.server
Package net.minestom.server.codec
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
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!")
-
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.Ok
result into the mapper function that creates a new result.Maps theResult.Error
result to the mapper function and creates a newResult.Error
result Otherwise, returnsResult.Ok
.default <S extends @UnknownNullability Object>
Result<S> default @UnknownNullability T
orElse
(@UnknownNullability T other) If the resultant is notResult.Ok
, returns the other valuedefault @UnknownNullability T
Attempts to get the value insideResult.Ok
or throws.default @UnknownNullability T
orElseThrow
(String message) Attempts to get the value insideResult.Ok
or throws.
-
Method Details
-
map
@Contract(pure=true) default <S extends @UnknownNullability Object> Result<S> map(Function<T, Result<S>> mapper) Map theResult.Ok
result 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.Ok
result to the mapper function and creates a newResult.Ok
result 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.Error
result to the mapper function and creates a newResult.Error
result 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.Ok
or throws.- Returns:
- the value
- Throws:
IllegalStateException
- if this instance ofResult.Error
-
orElseThrow
Attempts to get the value insideResult.Ok
or 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
-