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 <T1,
T2, E> Result <T2, E> Casting function to translate an erroneous result from one value type to anotherstatic <T,
E> Result <T, E> err
(E err) Creates an erroneous result out of the specifiederr
error.error()
Gets the result's error.If this is a successful result, returns the mapper function's result.Gets the result's value or throws aResultException
.getOrThrow
(Function<E, X> factory) Gets the result's value or throws an exception specified by thefactory
function.If this is an erroneous result, the specifiedconsumer
function is applied to the result's error value.If this a successful result, the specifiedconsumer
function is applied to the result's value.static <T> Result
<T, DelphiException> ioError
(IOException exc) Wraps an IO error in a result.boolean
isError()
Tests if this result is erroneous.boolean
Tests if this result is successful.If a value is present, returns a new result with the return value of the specifiedmapper
function, 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-null
value.If the result is an erroneous result, thedefaultValue
is returned, otherwise the result's value is returned.If the result is an erroneous result, thegetter
function is called to get the return value.value()
Gets the value of the result.
-
Method Details
-
err
Creates an erroneous result out of the specifiederr
error.- Parameters:
err
- Error value- Returns:
- Created result
- Throws:
NullPointerException
- Iferr
isnull
-
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
-
ok
Creates a successful result with the specified non-null
value.- Parameters:
value
- Value- Returns:
- Created result
- Throws:
NullPointerException
- Ifvalue
isnull
-
ioError
Wraps an IO error in a result.
Returned results Exception Types Returned error code NoSuchFileException
DelphiException.ERR_NO_FILE
AccessDeniedException
DelphiException.ERR_ACCESS_DENIED
Any other IO exception type DelphiException.ERR_IO_ERROR
- Parameters:
exc
- Exception to wrap- Returns:
- Error result
-
nothing
-
value
-
error
-
map
If a value is present, returns a new result with the return value of the specifiedmapper
function, 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
, aNullPointerException
is thrown.- Parameters:
mapper
- Mapping function- Returns:
- Mapped result.
- Throws:
NullPointerException
- Ifmapper
isnull
, 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
, aNullPointerException
is thrown.- Parameters:
mapper
- Mapping function- Returns:
- Mapped result
- Throws:
NullPointerException
- Ifmapper
isnull
, 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
, aNullPointerException
is thrown.- Parameters:
mapper
- Error mapping function- Returns:
- Error-mapped result
- Throws:
NullPointerException
- Ifmapper
isnull
, 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()
,false
otherwise.
-
isSuccess
boolean isSuccess()Tests if this result is successful.- Returns:
true
, if this result has a presentvalue()
.false
otherwise
-
ifError
-
ifSuccess
-
getOrThrow
Gets the result's value or throws aResultException
.If the result's error is a
Throwable
then the thrownResultException
will 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
-
orElse
-
orElseGet
-