Package net.arcadiusmc.delphi.util
Interface Result<T,E>
- Type Parameters:
T- Value typeE- Error type
public sealed interface Result<T,E>
Monad that can either be an error or a success and holds either a
value or an error.
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T,E> Result <T, E> err(E err) Creates an erroneous result out of the specifiederrerror.static <T1,T2, E>
Result<T2, E> Casting function to translate an erroneous result from one value type to anothererror()Gets the result's error.If this is a successful result, returns the mapper function's result.Creates an erroneous result with the specified message and arguments.Gets the result's value or throws aResultException.getOrThrow(Function<E, X> factory) Gets the result's value or throws an exception specified by thefactoryfunction.If this is an erroneous result, the specifiedconsumerfunction is applied to the result's error value.If this a successful result, the specifiedconsumerfunction is applied to the result's value.static <T> Result<T, DelphiException> ioError(IOException exc) Wraps an IO error in a result.booleanisError()Tests if this result is erroneous.booleanTests if this result is successful.If a value is present, returns a new result with the return value of the specifiedmapperfunction, otherwise, if this is an erroneous result, then nothing happens and this result is cast to the mapped type.If this is an erroneous result, returns the mapper functions result as a new error result.nothing()Get the successful 'nothing' result.static <T,E> Result <T, E> ok(T value) Creates a successful result with the specified non-nullvalue.If the result is an erroneous result, thedefaultValueis returned, otherwise the result's value is returned.If the result is an erroneous result, thegetterfunction is called to get the return value.value()Gets the value of the result.
-
Method Details
-
err
Creates an erroneous result out of the specifiederrerror.- Parameters:
err- Error value- Returns:
- Created result
- Throws:
NullPointerException- Iferrisnull
-
err
Casting function to translate an erroneous result from one value type to another- Parameters:
res- Result to cast- Returns:
- Casted result
- Throws:
IllegalArgumentException- If the specified result is non-erroneous
-
formatted
Creates an erroneous result with the specified message and arguments.Them message is formatted by calling
String.format(String, Object...)- Parameters:
string- Base string formatargs- Format arguments- Returns:
- Created result
-
ok
Creates a successful result with the specified non-nullvalue.- Parameters:
value- Value- Returns:
- Created result
- Throws:
NullPointerException- Ifvalueisnull
-
ioError
Wraps an IO error in a result.
Returned results Exception Types Returned error code NoSuchFileExceptionDelphiException.ERR_NO_FILEAccessDeniedExceptionDelphiException.ERR_ACCESS_DENIEDAny other IO exception type DelphiException.ERR_IO_ERROR- Parameters:
exc- Exception to wrap- Returns:
- Error result
-
nothing
Get the successful 'nothing' result. A result which is successful but has no return value.- Returns:
- Empty successful result.
-
value
Gets the value of the result.- Returns:
- An optional containing the value of this result, if it's not an error, or an empty optional if this is an erroneous result.
-
error
Gets the result's error.- Returns:
- An optional containing the error of this result, or an empty optional, if this is a successful result.
-
map
If a value is present, returns a new result with the return value of the specifiedmapperfunction, otherwise, if this is an erroneous result, then nothing happens and this result is cast to the mapped type.If the mapper function returns
null, aNullPointerExceptionis thrown.- Parameters:
mapper- Mapping function- Returns:
- Mapped result.
- Throws:
NullPointerException- Ifmapperisnull, or if the result of the mapper function isnull.
-
flatMap
If this is a successful result, returns the mapper function's result. Otherwise, nothing happens and this result is cast to the mapped type and returned.If the mapper function return
null, aNullPointerExceptionis thrown.- Parameters:
mapper- Mapping function- Returns:
- Mapped result
- Throws:
NullPointerException- Ifmapperisnull, or if the result of the mapper function isnull.
-
mapError
If this is an erroneous result, returns the mapper functions result as a new error result. Otherwise, nothing happens and this result is returned.If the mapper function returns
null, aNullPointerExceptionis thrown.- Parameters:
mapper- Error mapping function- Returns:
- Error-mapped result
- Throws:
NullPointerException- Ifmapperisnull, or if the result of the mapper function isnull.
-
isError
boolean isError()Tests if this result is erroneous.- Returns:
true, if this result has a presenterror(),falseotherwise.
-
isSuccess
boolean isSuccess()Tests if this result is successful.- Returns:
true, if this result has a presentvalue().falseotherwise
-
ifError
If this is an erroneous result, the specifiedconsumerfunction is applied to the result's error value. Otherwise, nothing happens.- Parameters:
consumer- Error consumer function- Returns:
this
-
ifSuccess
If this a successful result, the specifiedconsumerfunction is applied to the result's value. Otherwise, nothing happens.- Parameters:
consumer- Value consumer function- Returns:
this
-
getOrThrow
Gets the result's value or throws aResultException.If the result's error is a
Throwablethen the thrownResultExceptionwill have the throwable as its cause (Accessible withThrowable.getCause()).
If the result's error is a string, then it will be used as theResultException's message.
Otherwise, a result exception will be thrown with aResultException.getErrorObject()of this result's error value.- Returns:
- The result's value
- Throws:
ResultException- If the result is an erroneous result.
-
getOrThrow
Gets the result's value or throws an exception specified by thefactoryfunction.- Parameters:
factory- Exception factory- Returns:
- The result's value
- Throws:
X- If the result is an erroneous result.
-
orElse
If the result is an erroneous result, thedefaultValueis returned, otherwise the result's value is returned.- Parameters:
defaultValue- Default value- Returns:
- Result value, or the specified fallback value, if the result is erroneous.
-
orElseGet
If the result is an erroneous result, thegetterfunction is called to get the return value. Otherwise, the result's value is returned.- Parameters:
getter- Default value supplier- Returns:
- Result value, or the specified fallback value, if the result is erroneous.
-