Class Result<S>
java.lang.Object
dev.derklaro.reflexion.Result<S>
- Type Parameters:
S- the successful result type of the execution.
- All Implemented Interfaces:
Supplier<S>
A rust inspired result class which either holds the result of an executable or the exception thrown by it. That makes
it very easy to write clean code and give exceptions back to the actual producer of them rather than requiring a
wrapped rethrow of it.
- Since:
- 1.0
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceRepresents a supplier which can throw any exception when executing. -
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionWraps the successful value of this result in an optional.consume(@NotNull BiConsumer<S, Throwable> consumer) Consumes both, the successful and exceptional result value wrapped in this result and passes them to the given consumer.static <T> @NotNull Result<T> exceptional(@NotNull Throwable exception) Constructs a new result instance which failed and holds the given exception as the reason of the failure.<M> @NotNull Result<M> Maps the successful result of this result into a new one or returns the same instance as before when this result already contained an exception.<M> @NotNull Result<M> flatMapExceptional(@NotNull Function<Throwable, Result<M>> function) Maps the wrapped exceptional result of this result into a new one or returns the same instance as before when this result was executed successfully.get()Get the successful result wrapped in this result.@NotNull ThrowableGet the exception which was thrown during the execution.@UnknownNullability SGet the successful result wrapped in this result or the given value if this result was executed exceptionally.@UnknownNullability SGet the successful result wrapped in this result or evaluates the given supplier value and returns that.@UnknownNullability SGet the successful result wrapped in this result or maps the wrapped exception using the supplied function.@UnknownNullability SGet the wrapped success value of this result or rethrows the wrapped exception unchecked.ifExceptional(@NotNull Consumer<Throwable> consumer) Executes the given consumer using the wrapped exceptional result value if present.Executes the given consumer using the wrapped successful result value if present.<M> @NotNull Result<M> Maps the successful result of this result into a new one or returns the same instance as before when this result already contained an exception.mapExceptional(@NotNull Function<Throwable, Throwable> function) Maps the wrapped exceptional result of this result into a new one or returns the same instance as before when this result was executed successfully.static <T> @NotNull Result<T> success(T result) Constructs a new result instance holding the given object as its successful result.static <T> Result<T> tryExecute(@NotNull Result.ExceptionalSupplier<T> supplier) Tries to execute the supplier and use the result of it as the value of this result.booleanGet if this result execution failed for some reason.booleanGet if this result was executed successfully or if it failed for some reason.
-
Field Details
-
result
-
exception
-
-
Constructor Details
-
Result
Constructs a new result type. Either the result or the exception must be present. The state of the result is determined by whether the given exception is present or absent.- Parameters:
result- the result of the execution, can be null.exception- the exceptional result the execution, only present when the result failed.
-
-
Method Details
-
tryExecute
Tries to execute the supplier and use the result of it as the value of this result. If the execution of the supplier fails a result holding the thrown exception is returned instead.This method instantly rethrows unrecoverable errors which prevent the current thread from resuming.
- Type Parameters:
T- the type of the action result.- Parameters:
supplier- the which should get executed.- Returns:
- the result of the action execution, either holding the result value or the thrown exception.
- Throws:
NullPointerException- if the given supplier is null.
-
success
Constructs a new result instance holding the given object as its successful result.- Type Parameters:
T- the type of the result.- Parameters:
result- the successful result of the action.- Returns:
- a new result instance which succeeded and holds the given object as it's result value.
-
exceptional
Constructs a new result instance which failed and holds the given exception as the reason of the failure.- Type Parameters:
T- the expected successful return type, even tho the result is never successful.- Parameters:
exception- the reason of the failure.- Returns:
- a new result instance holding the given exception as the failure reason.
- Throws:
NullPointerException- if the given exception is null.
-
wasSuccessful
public boolean wasSuccessful()Get if this result was executed successfully or if it failed for some reason.- Returns:
- true if this result was executed successfully, false otherwise.
-
wasExceptional
public boolean wasExceptional()Get if this result execution failed for some reason.- Returns:
- true if the result holds an exception, false otherwise.
-
getException
Get the exception which was thrown during the execution.- Returns:
- the thrown exception.
- Throws:
NoSuchElementException- if this result was executed successfully.
-
get
Get the successful result wrapped in this result.- Specified by:
getin interfaceSupplier<S>- Returns:
- the successful result.
- Throws:
NoSuchElementException- if the result was executed exceptionally.
-
getOrElse
Get the successful result wrapped in this result or the given value if this result was executed exceptionally.- Parameters:
or- the value to get if the result was executed exceptionally.- Returns:
- the wrapped successful return value or the given value.
-
getOrEval
Get the successful result wrapped in this result or evaluates the given supplier value and returns that.- Parameters:
supplier- the supplier to evaluate if this result was executed exceptionally.- Returns:
- the wrapped success return value or the evaluated value from the given supplier.
- Throws:
NullPointerException- if the given supplier is null.
-
getOrMap
Get the successful result wrapped in this result or maps the wrapped exception using the supplied function.- Parameters:
function- the function to call with the exception thrown by the execution.- Returns:
- the wrapped success return value or the evaluated value from the given function.
- Throws:
NullPointerException- if the given function is null.
-
getOrThrow
Get the wrapped success value of this result or rethrows the wrapped exception unchecked.- Returns:
- the successful return value of the result.
-
map
Maps the successful result of this result into a new one or returns the same instance as before when this result already contained an exception. This method will return an exceptional result if the mapping function throws an exception.- Type Parameters:
M- the new result type mapped from the given function.- Parameters:
function- the mapping function to map the success value of this result.- Returns:
- a new result based on the mapping function if this result was executed successfully.
- Throws:
NullPointerException- if the given function is null.
-
flatMap
Maps the successful result of this result into a new one or returns the same instance as before when this result already contained an exception. This method will not catch any exceptions thrown by the mapping function.- Type Parameters:
M- the new result type mapped from the given function.- Parameters:
function- the mapping function to map the success value of this result.- Returns:
- a new result based on the mapping function if this result was executed successfully.
- Throws:
NullPointerException- if the given function is null.- Since:
- 1.5.0
-
mapExceptional
@NotNull public @NotNull Result<S> mapExceptional(@NotNull @NotNull Function<Throwable, Throwable> function) Maps the wrapped exceptional result of this result into a new one or returns the same instance as before when this result was executed successfully. This method will always return an exceptional result. This method will not catch any exceptions thrown by the mapping function.- Parameters:
function- the mapping function for the wrapped exception.- Returns:
- a new result based on the mapping function if this result was executed exceptionally.
- Throws:
NullPointerException- if the given mapping function is null.
-
flatMapExceptional
@NotNull public <M> @NotNull Result<M> flatMapExceptional(@NotNull @NotNull Function<Throwable, Result<M>> function) Maps the wrapped exceptional result of this result into a new one or returns the same instance as before when this result was executed successfully. This method will not catch any exceptions thrown by the mapping function.- Parameters:
function- the mapping function for the wrapped exception.- Returns:
- a new result based on the mapping function if this result was executed exceptionally.
- Throws:
NullPointerException- if the given mapping function is null.- Since:
- 1.5.0
-
ifSuccess
Executes the given consumer using the wrapped successful result value if present. This method does nothing if the result completed exceptionally.- Parameters:
consumer- the consumer to call with the wrapped successful value.- Returns:
- the same instance as used to call the method, for chaining.
- Throws:
NullPointerException- if the given consumer is null.
-
ifExceptional
Executes the given consumer using the wrapped exceptional result value if present. This method does nothing if the result completed successfully.- Parameters:
consumer- the consumer to call with the wrapped exceptional result.- Returns:
- the same instance as used to call the method, for chaining.
-
consume
Consumes both, the successful and exceptional result value wrapped in this result and passes them to the given consumer. The given exception will be null if the result completed successfully.Note: the successful result type might be null even when the result completed successfully, indicating that the execution returned null.
- Parameters:
consumer- the consumer to accept both, the successful and exceptional value wrapped in this result.- Returns:
- the same instance as used to call the method, for chaining.
- Throws:
NullPointerException- if the given consumer is null.
-
asOptional
Wraps the successful value of this result in an optional. Note: the optional value might be absent even when the result completed successfully, indicating that the execution returned null.- Returns:
- an optional of the wrapped return value in this result.
-