Interface Result<T,E>
- Type Parameters:
T- type of successful resultE- type of error result
- All Superinterfaces:
Supplier<T>
- All Known Implementing Classes:
Result.Error,Result.Success
A tagged union representing either a successful result or an error.
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic final classstatic final classExtensions for Result providing type-specific specializations which are impossible via generic virtual methods.static classAn exception thrown wheneverunwrapError()is called on a successful result.static classAn exception thrown wheneverunwrap()is called on an error result.static final classHolder of anull-error result.static final classHolder of anull-success result.static final classRepresentation of asuccessfulresult. -
Method Summary
Modifier and TypeMethodDescriptionReturns the given result if this is asuccessful resultotherwise keeping theerror result.Converts this result into anOptionalof its error.Converts this result into aStreamof its error value.@NotNull ValueContainer<E>Converts this result into aValueContainerof its error value.Converts this result into anOptionalof its successful value.asStream()Converts this result into aStreamof its successful value.@NotNull ValueContainer<T>Converts this result into aValueContainerof its successful value.static <T,E> @NotNull Result<T, E> error(E error) Creates a new error result.errorOrComputeDefault(@NonNull Function<? super T, ? extends E> defaultErrorFactory) Gets the value of this result if this is asuccessfulotherwise returning the value computed by applying the given function to theerror value.errorOrDefault(E defaultError) Gets the error of this result if this is anerrorotherwise returning default value.errorOrElseSneakyThrow(@NonNull Function<? super T, ? extends X> exceptionFactory) Gets the error of this result throwingXgot by using the specified supplier if this is asuccessful result.errorOrElseThrow(@NonNull Function<? super T, ? extends X> exceptionFactory) Gets the error of this result throwingXgot by using the specified supplier if this is asuccessful result.errorOrGetDefault(@NonNull Supplier<? extends E> defaultErrorSupplier) Gets the value of this result if this is anerrorotherwise returning the value got by using the specified supplier.Gets the error of this result if this is anerrorotherwise returningnull.Gets the value of this result throwing aResult.NotSuccessExceptionif this is anerror result.expectError(String message) Gets the error of this result throwing aResult.NotErrorExceptionif this is asuccessful result.Also known asandThen.static <T,E> @NotNull Result<T, @Nullable E> Converts the givenOptionalinto anull-error result.static <T,E> @NotNull Result<T, E> Converts the givenOptionalinto anerror result.default Tget()voidInvokes the corresponding function depending on this result's type.voidInvokes the given function if this result is anerror result.voidInvokes the given function if this result is asuccessful result.booleanisError()Checks is this result is an error.booleanChecks is this result is successful.Maps the result if it issuccessfulreturning a new result with the result of mapping otherwise keeping theerror result.Maps the result if it is anerror resultreturning a new result with the result of mapping otherwise keeping theerror result.nonNullError(@Any E error) Creates a newerror resultif the value is notnull.nonNullSuccess(T value) Creates a newsuccessful resultif the value is notnull.static <T,E> @NotNull Result<T, @Nullable E> Creates a newvoid-error result.static <T,E> @NotNull Result<@Nullable T, E> Creates a new successful result withnullvalue.Returns this result if this is asuccessful resultotherwise returning the given result.orComputeDefault(@NonNull Function<? super E, ? extends T> defaultValueFactory) Gets the value of this result if this is asuccessfulotherwise returning the value computed by applying the given function to theerror value.Gets the value of this result if this is asuccessfulotherwise returning default value.Maps the result if this is anerror resultreturning the result of mapping otherwise keeping thesuccessful result.orElseSneakyThrow(@NonNull Function<? super E, ? extends X> exceptionFactory) Gets the value of this result throwingXgot by using the specified supplier if this is anerror result.orElseThrow(@NonNull Function<? super E, ? extends X> exceptionFactory) Gets the value of this result throwingXgot by using the specified supplier if this is anerror result.orGetDefault(@NonNull Supplier<? extends T> defaultValueSupplier) Gets the value of this result if this is asuccessfulotherwise returning the value got by using the specified supplier.orNull()Gets the value of this result if this is asuccessfulotherwise returningnull.Consumes the result if it issuccessfulreturning the same result.Consumes the result if it is anerror resultreturning the same result.static <T,E> @NotNull Result<T, E> success(T value) Creates a new successful result.swap()Swaps this result making anerror resultfrom asuccessful resultand asuccessful resultfrom anerror result.Creates a result by calling the specified callable.Deprecated.tryGet(@NonNull ThrowingSupplier<? extends T, ? extends X> supplier, @NonNull Class<? super X> throwableType) Creates a result by getting the value of the specified supplier.tryGet(@NonNull ThrowingSupplier<? extends T, ? extends X> supplier, @Nullable X @NonNull ... throwableTypeHint) Creates a result by getting the value of the specified supplier.tryGetCatchAny(@NonNull ThrowingSupplier<? extends T, Throwable> supplier) Creates a result by getting the value of the specified supplier.tryRun(@NonNull ThrowingRunnable<? extends X> runnable, @NonNull Class<? super X> throwableType) Creates a result by running the specified runnable.tryRun(@NonNull ThrowingRunnable<? extends X> runnable, @Nullable X @NonNull ... throwableTypeHint) Creates a result by running the specified runnable.tryRunCatchAny(@NonNull ThrowingRunnable<? extends Throwable> runnable) Creates a result by running the specified runnable.unwrap()Gets the value of this result throwing aResult.NotSuccessExceptionif this is anerror result.Gets the error of this result throwing aResult.NotErrorExceptionif this is asuccessful result.
-
Method Details
-
success
Creates a new successful result.- Type Parameters:
T- type of successful resultE- type of error result- Parameters:
value- value of the successful result- Returns:
- created successful result
-
nullSuccess
Creates a new successful result withnullvalue.- Type Parameters:
T- type of successful resultE- type of error result- Returns:
- created successful result
-
error
Creates a new error result.- Type Parameters:
T- type of successful resultE- type of error result- Parameters:
error- value of the error result- Returns:
- created error result
-
nullError
Creates a newvoid-error result.- Type Parameters:
T- type of successful resultE- type of error result- Returns:
- created error result
-
nonNullSuccess
@NotNull static <T,@Any E> @NotNull Result<@NotNull T,@Nullable E> nonNullSuccess(@Nullable T value) Creates a newsuccessful resultif the value is notnull.- Type Parameters:
T- type of the successful valueE- any formal type of the error value- Parameters:
value- value which should be non-nullto be considered a successful value- Returns:
- a
successful resultif thevalueis notnullor anull errorotherwise
-
nonNullError
@NotNull static <T,@Any E> @NotNull Result<@Nullable T,@NotNull E> nonNullError(@Nullable @Any E error) Creates a newerror resultif the value is notnull.- Type Parameters:
T- any formal type of the successful valueE- type of the error value- Parameters:
error- value which should be non-nullto be considered an error value- Returns:
- a
error resultif thevalueis notnullor anull successotherwise
-
from
@NotNull static <T,E> @NotNull Result<T,@Nullable E> from(@NonNull @NonNull Optional<? extends T> optional) Converts the givenOptionalinto anull-error result.- Type Parameters:
T- type of successful resultE- type of error result- Parameters:
optional- optional to be converted into the result- Returns:
successful resultif the valueOptional.isPresent()in the optional and anull-error resultotherwise- See Also:
-
from
@NotNull static <T,E> @NotNull Result<T,E> from(@NonNull @NonNull Optional<? extends T> optional, @NonNull @NonNull Supplier<? extends E> errorSupplier) Converts the givenOptionalinto anerror result.- Type Parameters:
T- type of successful resultE- type of error result- Parameters:
optional- optional to be converted into the resulterrorSupplier- supplier to create an error if the givenOptionalisempty- Returns:
successful resultif the valueOptional.isPresent()in the optional and anerror resultwith an error supplied fromerror supplierotherwise- See Also:
-
tryRun
@NotNull static <X extends Throwable> @NotNull Result<@Nullable Void,@NotNull X> tryRun(@NonNull @NonNull ThrowingRunnable<? extends X> runnable, @NonNull @NonNull Class<? super X> throwableType) Creates a result by running the specified runnable.- Type Parameters:
X- type of the thrown throwable- Parameters:
runnable- function whose failure indicates theerror resultthrowableType- class instance representing the type of the thrown exception- Returns:
successful void-resultif the runnable runs unexceptionally or anerror resultcontaining the thrownthrowableifit is ofthe expected type- API note
- if an unexpected exception is thrown then it will be rethrown
-
tryRun
@SafeVarargs @NotNull static <X extends Throwable> @NotNull Result<@Nullable Void,@NotNull X> tryRun(@NonNull @NonNull ThrowingRunnable<? extends X> runnable, @TypeHint @Nullable @Nullable X @NonNull ... throwableTypeHint) Creates a result by running the specified runnable.- Type Parameters:
X- type of the thrown throwable- Parameters:
runnable- function whose failure indicates theerror resultthrowableTypeHint- array used for throwable type discovery- Returns:
successful void-resultif the runnable runs unexceptionally or anerror resultcontaining the thrownthrowableifit is ofthe expected type- API note
- if an unexpected exception is thrown then it will be rethrown
-
tryRunCatchAny
@NotNull static @NotNull Result<@Nullable Void,? extends @NotNull Throwable> tryRunCatchAny(@NonNull @NonNull ThrowingRunnable<? extends Throwable> runnable) Creates a result by running the specified runnable.- Parameters:
runnable- function whose failure indicates theerror result- Returns:
successful void-resultif the runnable runs unexceptionally or anerror resultcontaining the thrownthrowableotherwise
-
tryGet
static <T,X extends Throwable> Result<T,@NotNull X> tryGet(@NonNull @NonNull ThrowingSupplier<? extends T, ? extends X> supplier, @NonNull @NonNull Class<? super X> throwableType) Creates a result by getting the value of the specified supplier.- Type Parameters:
T- type of thesuccessful resultprovided by the given supplierX- type of the thrown throwable- Parameters:
supplier- provider of the result whose failure indicates theerror resultthrowableType- class instance representing the type of the thrown exception- Returns:
successful resultif the supplier provides the value unexceptionally or anerror resultcontaining the thrownthrowableifit is ofthe expected type ifit is ofthe expected type- API note
- if an unexpected exception is thrown then it will be rethrown
-
tryGet
@SafeVarargs static <T,X extends Throwable> Result<T,@NotNull X> tryGet(@NonNull @NonNull ThrowingSupplier<? extends T, ? extends X> supplier, @TypeHint @Nullable @Nullable X @NonNull ... throwableTypeHint) Creates a result by getting the value of the specified supplier.- Type Parameters:
T- type of thesuccessful resultprovided by the given supplierX- type of the thrown throwable- Parameters:
supplier- provider of the result whose failure indicates theerror resultthrowableTypeHint- array used for throwable type discovery- Returns:
successful resultif the supplier provides the value unexceptionally or anerror resultcontaining the thrownthrowableotherwise ifit is ofthe expected type- API note
- if an unexpected exception is thrown then it will be rethrown
-
tryGetCatchAny
static <T> Result<T,@NotNull Throwable> tryGetCatchAny(@NonNull @NonNull ThrowingSupplier<? extends T, Throwable> supplier) Creates a result by getting the value of the specified supplier.- Type Parameters:
T- type of the successful result value- Parameters:
supplier- provider of the result whose failure indicates theerror result- Returns:
successful resultif the supplier provides the value unexceptionally or anerror resultcontaining the thrownthrowable
-
tryCall
@NotNull static <T> @NotNull Result<T,@NotNull Exception> tryCall(@NonNull @NonNull Callable<? extends T> callable) Creates a result by calling the specified callable.- Type Parameters:
T- type of thesuccessful resultprovided by the given supplier- Parameters:
callable- provider of the result whose failure indicates theerror result- Returns:
successful resultif the callable completes unexceptionally or anerror resultcontaining the thrownexceptionotherwise
-
tryFrom
@Deprecated @NotNull static <T> @NotNull Result<T,@NotNull Exception> tryFrom(@NonNull @NonNull Callable<? extends T> callable) Deprecated.in favor oftryCall(Callable)Creates a result by calling the specified callable.- Type Parameters:
T- type of thesuccessful resultprovided by the given supplier- Parameters:
callable- provider of the result whose failure indicates theerror result- Returns:
successful resultif the callable completes unexceptionally or anerror resultcontaining the thrownexceptionotherwise
-
isSuccess
boolean isSuccess()Checks is this result is successful.- Returns:
trueif this is a successful result andfalseotherwise
-
isError
boolean isError()Checks is this result is an error.- Returns:
trueif this is an error result andfalseotherwise
-
get
-
unwrap
T unwrap()Gets the value of this result throwing aResult.NotSuccessExceptionif this is anerror result.- Returns:
- successful value of this result
- Throws:
Result.NotSuccessException- if this is anerror result- See Also:
-
expect
Gets the value of this result throwing aResult.NotSuccessExceptionif this is anerror result.- Parameters:
message- message to be specified toResult.NotSuccessException- Returns:
- successful value of this result
- Throws:
Result.NotSuccessException- if this is anerror result- See Also:
-
orElseThrow
<X extends Throwable> T orElseThrow(@NonNull @NonNull Function<? super E, ? extends X> exceptionFactory) throws XGets the value of this result throwingXgot by using the specified supplier if this is anerror result.- Type Parameters:
X- type of exception thrown if this is anerror result- Parameters:
exceptionFactory- factory of a thrown exception consuming the error value of this result- Returns:
- successful value of this result
- Throws:
X- if this is anerror result- See Also:
-
orElseSneakyThrow
default <X extends Throwable> T orElseSneakyThrow(@NonNull @NonNull Function<? super E, ? extends X> exceptionFactory) Gets the value of this result throwingXgot by using the specified supplier if this is anerror result. ThrowsXif this is anerror result. This differs fromorElseThrow(Function)as this does not declareXas a thrown exception.- Type Parameters:
X- type of exception thrown if this is anerror result- Parameters:
exceptionFactory- factory of a thrown exception consuming the error value of this result- Returns:
- successful value of this result
- See Also:
-
orDefault
Gets the value of this result if this is asuccessfulotherwise returning default value.- Parameters:
defaultValue- default value to be returned if this is anerror value- Returns:
- successful value if this a
successful resultordefaultValueotherwise
-
orNull
Gets the value of this result if this is asuccessfulotherwise returningnull.- Returns:
- successful value if this a
successful resultornullotherwise
-
orGetDefault
Gets the value of this result if this is asuccessfulotherwise returning the value got by using the specified supplier.- Parameters:
defaultValueSupplier- supplier of the default value to be returned if this is anerror value- Returns:
- successful value if this a
successful resultor got default otherwise
-
orComputeDefault
Gets the value of this result if this is asuccessfulotherwise returning the value computed by applying the given function to theerror value.- Parameters:
defaultValueFactory- factory used for creation of the default value to be returned if this is anerror value- Returns:
- successful value if this a
successful resultor the computed default otherwise
-
unwrapError
E unwrapError()Gets the error of this result throwing aResult.NotErrorExceptionif this is asuccessful result.- Returns:
- error value of this result
- Throws:
Result.NotErrorException- if this is asuccessful result- See Also:
-
expectError
Gets the error of this result throwing aResult.NotErrorExceptionif this is asuccessful result.- Parameters:
message- message to be specified toResult.NotErrorException- Returns:
- error value of this result
- Throws:
Result.NotErrorException- if this is asuccessful result- See Also:
-
errorOrElseThrow
<X extends Throwable> E errorOrElseThrow(@NonNull @NonNull Function<? super T, ? extends X> exceptionFactory) throws XGets the error of this result throwingXgot by using the specified supplier if this is asuccessful result.- Type Parameters:
X- type of exception thrown if this is asuccessful result- Parameters:
exceptionFactory- factory of a thrown exception consuming the successful value of this result- Returns:
- error value of this result
- Throws:
X- if this is anerror result- See Also:
-
errorOrElseSneakyThrow
default <X extends Throwable> E errorOrElseSneakyThrow(@NonNull @NonNull Function<? super T, ? extends X> exceptionFactory) Gets the error of this result throwingXgot by using the specified supplier if this is asuccessful result. ThrowsXif this is anerror result. This differs fromorElseThrow(Function)as this does not declareXas a thrown exception.- Type Parameters:
X- type of exception thrown if this is asuccessful result- Parameters:
exceptionFactory- factory of a thrown exception consuming the successful value of this result- Returns:
- error value of this result
- See Also:
-
errorOrDefault
Gets the error of this result if this is anerrorotherwise returning default value.- Parameters:
defaultError- default value to be returned if this is ansuccessful result- Returns:
- error value if this an
error resultordefaultErrorotherwise
-
errorOrNull
Gets the error of this result if this is anerrorotherwise returningnull.- Returns:
- error value if this an
error resultornullotherwise
-
errorOrGetDefault
Gets the value of this result if this is anerrorotherwise returning the value got by using the specified supplier.- Parameters:
defaultErrorSupplier- supplier of the default error to be returned if this is asuccessful value- Returns:
- error value if this an
error resultor got default otherwise
-
errorOrComputeDefault
Gets the value of this result if this is asuccessfulotherwise returning the value computed by applying the given function to theerror value.- Parameters:
defaultErrorFactory- factory used for creation of the default error to be returned if this is asuccessful value- Returns:
- successful value if this an
error resultor the computed default otherwise
-
ifSuccess
Invokes the given function if this result is asuccessful result.- Parameters:
successConsumer- consumer accepting thesuccessful value
-
ifError
Invokes the given function if this result is anerror result.- Parameters:
errorConsumer- consumer accepting theerror value
-
handle
void handle(@NonNull @NonNull Consumer<? super T> successConsumer, @NonNull @NonNull Consumer<? super E> errorConsumer) Invokes the corresponding function depending on this result's type.- Parameters:
successConsumer- consumer accepting thesuccessful valueerrorConsumer- consumer accepting theerror value
-
map
@NotNull <R> @NotNull Result<R,E> map(@NonNull @NonNull Function<? super T, ? extends R> mappingFunction) Maps the result if it issuccessfulreturning a new result with the result of mapping otherwise keeping theerror result.- Type Parameters:
R- type of the resulting successful value- Parameters:
mappingFunction- function to map the successful result- Returns:
- mapped successful result if it was a
successful resultor an error result if it was anerror result
-
peek
Consumes the result if it issuccessfulreturning the same result.- Parameters:
consumer- consumer to accept the successful result- Returns:
- the same result
-
mapError
@NotNull <R> @NotNull Result<T,R> mapError(@NonNull @NonNull Function<? super E, ? extends R> mappingFunction) Maps the result if it is anerror resultreturning a new result with the result of mapping otherwise keeping theerror result.- Type Parameters:
R- type of the resulting error value- Parameters:
mappingFunction- function to map the error result- Returns:
- mapped error result if it was an
an error resultor a successful result if it was asuccessful result
-
peekError
Consumes the result if it is anerror resultreturning the same result.- Parameters:
consumer- consumer to accept the successful result- Returns:
- the same result
-
and
Returns the given result if this is asuccessful resultotherwise keeping theerror result.- Type Parameters:
R- type of the resulting successful value- Parameters:
nextResult- result to be returned if this is asuccessful result- Returns:
nextResultif this is asuccessful resultor this error result otherwise- See Also:
-
flatMap
@NotNull <R> @NotNull Result<R,E> flatMap(@NonNull @NonNull Function<? super T, ? extends @NotNull Result<R, E>> mapper) Also known asandThen. Maps the result if this is asuccessful resultreturning the result of mapping otherwise keeping theerror result.- Type Parameters:
R- type of the resulting successful value- Parameters:
mapper- function to create a new result fromcurrent successful one- Returns:
- mapped
successful resultif this wasthe oneor this error result otherwise - See Also:
-
or
Returns this result if this is asuccessful resultotherwise returning the given result.- Type Parameters:
R- type of the resulting error value- Parameters:
alternateResult- result to be returned if this is anerror result- Returns:
- successful result if this is
the oneoralternateResultotherwise - See Also:
-
orElse
@NotNull <R> @NotNull Result<T,R> orElse(@NonNull @NonNull Function<? super E, ? extends @NotNull Result<T, R>> mapper) Maps the result if this is anerror resultreturning the result of mapping otherwise keeping thesuccessful result.- Type Parameters:
R- type of the resulting error value- Parameters:
mapper- function to create a new result fromcurrent error one- Returns:
- mapped
error resultif this wasthe oneor this successful result otherwise - See Also:
-
swap
Swaps this result making anerror resultfrom asuccessful resultand asuccessful resultfrom anerror result.- Returns:
error resultif this was asuccessful resultand asuccessful resultif this was anerror result
-
asOptional
Converts this result into anOptionalof its successful value.- Returns:
optionalcontaining the value if this is asuccessful resultor anempty optionalotherwise
-
asErrorOptional
Converts this result into anOptionalof its error.- Returns:
optionalcontaining the error if this is anerror resultor anempty optionalotherwise
-
asValueContainer
Converts this result into aValueContainerof its successful value.- Returns:
value containercontaining the value if this is asuccessful resultor anempty value-containerotherwise
-
asErrorValueContainer
Converts this result into aValueContainerof its error value.- Returns:
value containercontaining the error if this is anerror resultor anempty value-containerotherwise
-
asStream
Converts this result into aStreamof its successful value.- Returns:
streamof the value if this is asuccessful resultor anempty streamotherwise
-
asErrorStream
Converts this result into aStreamof its error value.- Returns:
streamcontaining the error if this is anerror resultor anempty streamotherwise
-
tryCall(Callable)