Package org.storynode.pigeon.result
Class Result<T,E>
java.lang.Object
org.storynode.pigeon.result.Result<T,E>
- Type Parameters:
T- The type of the ok valueE- The type of the error value
- All Implemented Interfaces:
Wrapped<T>
A type representing the outcome of some operation, which value can be some value or some error
but not neither nor both at the same time.
Examples
Known variant construction
AnOk result with the value "Hello world"
Result.ok("Hello world")
An Err result with the error value set to 45D
Result.err(45D)
Construct by execution
Construct a result with the outcome of a http request
Result.of(() -> httpGet("https://www.wikipedia.com")).map(Response:getBody)
This would result in an Err result with its error value set
to the instance of the exception caught when running the provided function
Result.of(() -> 8 / 0)
- Since:
- 1.0.0
- Author:
- Andrea Coronese
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionReturnsresif the result isOk, otherwise returns the Err value ofthis.Likeand(Result)but lazily evaluated.booleanstatic <T,E> @NotNull Result <T, E> err(E error) Constructs an error variant of aResultstatic <T,E> @NotNull Result <T, E> error(E error) Constructs an error variant of aResultLikeOption.map(Function)but does not re-wrap the result of the provided mapping function to aResultExecutes the provided function if this contains an errorExecutes the provided function if this contains a valueifOkOrElse(Consumer<T> whenOk, Consumer<E> whenError) ExecuteswhenOkif this contains a value,whenErrorotherwise.booleanisErr()Whether thisResultis an errorbooleanReturnstrueif the result contains an error and that error satisfies apredicateabstract booleanisOk()Whether thisResultis ok, meaning it contains a value and not an errorbooleanReturnstrueif the result contains a value and that value satisfies apredicateMaps aResult<T, E>toResult<U, E>by applying a function to a contained value, leaving a result that contains an error untouched.Maps aResult<T, E>toResult<T, U>by applying a function to a contained error, leaving a result that contains a value untouched.of(ThrowingSupplier<T> fn) Constructs a newResultby using the provided function return value.static <T,E> @NotNull Result <T, E> ok(T inner) Constructs an ok variant of aResult.abstract TUnwraps the contained value, or returns a default one if this contains an errorabstract TUnwraps the contained value, or returns a default one if this contains an errortoTuple()Converts this result to aPairtuple having the value and the error as fieldsabstract Tunwrap()Unwraps and return the inner value, if present.abstract EUnwraps and return the inner error, if present.
-
Constructor Details
-
Result
public Result()
-
-
Method Details
-
ok
@Contract(value="_ -> new", pure=true) @NotNull public static <T,E> @NotNull Result<T,E> ok(@NotNull T inner) Constructs an ok variant of aResult.- Type Parameters:
T- The type of the ok valueE- The type of the error value- Parameters:
inner- The value of the result for the ok state- Returns:
- The constructed result
-
err
@Contract(value="_ -> new", pure=true) @NotNull public static <T,E> @NotNull Result<T,E> err(@NotNull E error) Constructs an error variant of aResult- Type Parameters:
T- The type of the ok valueE- The type of the error value- Parameters:
error- The value of the result for the error state- Returns:
- The constructed result
-
error
@Contract(value="_ -> new", pure=true) @NotNull public static <T,E> @NotNull Result<T,E> error(@NotNull E error) Constructs an error variant of aResult- Type Parameters:
T- The type of the ok valueE- The type of the error value- Parameters:
error- The value of the result for the error state- Returns:
- The constructed result
-
of
Constructs a newResultby using the provided function return value. If the supplier completes exceptionally, theResultwill contain the caught exception as error- Type Parameters:
T- The type of the contained value- Parameters:
fn- The function to execute to obtain the value of the result- Returns:
- A
Resultwith a value or an error, depending on the function execution
-
isOk
public abstract boolean isOk()Whether thisResultis ok, meaning it contains a value and not an error- Returns:
trueif this contains an ok value,falseif it contains an error
-
unwrap
Unwraps and return the inner value, if present. Throws an error if this result contains an error.- Specified by:
unwrapin interfaceWrapped<T>- Returns:
- The inner value
- Throws:
UnwrapException- if this contains an error
-
unwrapError
Unwraps and return the inner error, if present. Throws an error if this result contains a value.- Returns:
- The inner error
-
orElseGet
Unwraps the contained value, or returns a default one if this contains an error- Parameters:
defaultValueSupplier- The supplier for the value to return in place of the error- Returns:
- The contained value or the default one, depending on which is appropriate
-
map
Maps aResult<T, E>toResult<U, E>by applying a function to a contained value, leaving a result that contains an error untouched. This can be used to compose the result of two functions.Example
Result.ok(2).map(Math:sqrt) // Gets the square root of two as a Result- Type Parameters:
U- The type of the new value- Parameters:
fn- The function to apply to the value- Returns:
- The mapped
Result
-
flatMap
public abstract <U> Result<U,E> flatMap(@NotNull @NotNull Function<? super T, ? extends Result<U, E>> fn) LikeOption.map(Function)but does not re-wrap the result of the provided mapping function to aResult- Type Parameters:
U- The type of the mappedOkvalue- Parameters:
fn- The mapping function to apply- Returns:
- The mapped value
-
mapError
Maps aResult<T, E>toResult<T, U>by applying a function to a contained error, leaving a result that contains a value untouched. This can be used to compose the result of two functions- Type Parameters:
U- The type of the new error- Parameters:
fn- The function to apply to the error- Returns:
- The mapped
Result
-
ifOkOrElse
ExecuteswhenOkif this contains a value,whenErrorotherwise.- Parameters:
whenOk- The function to execute when this contains a valuewhenError- The function to execute when this contains an error- Returns:
- this instance (for chaining)
-
orElse
Unwraps the contained value, or returns a default one if this contains an error- Parameters:
defaultValue- The value to return in place of the error- Returns:
- The contained value or the default one, depending on which is appropriate
-
isOkAnd
Returnstrueif the result contains a value and that value satisfies apredicate- Parameters:
predicate- The predicate to satisfy- Returns:
- If the value is present and satisfies the predicate
-
isErr
public boolean isErr()Whether thisResultis an error- Returns:
trueif this contains an error,falseif it contains a value
-
isErrAnd
Returnstrueif the result contains an error and that error satisfies apredicate- Parameters:
predicate- The predicate to satisfy- Returns:
- If the error is present and satisfies the predicate
-
ifOk
Executes the provided function if this contains a value- Parameters:
whenOk- The function to execute- Returns:
- this instance (for chaining)
-
ifError
Executes the provided function if this contains an error- Parameters:
whenError- The function to execute- Returns:
- this instance (for chaining)
-
and
Returnsresif the result isOk, otherwise returns the Err value ofthis.
Arguments passed toandare eagerly evaluated; if you are passing the result of a function call, it is recommended to useandThen(Function), which is lazily evaluated.- Type Parameters:
U- The type of the other result (ifOk)- Parameters:
res- The other result- Returns:
resif this isOk, this error otherwise- See Also:
-
andThen
Likeand(Result)but lazily evaluated. The function is supplied the current value of the result if it'sOk- Type Parameters:
U- The type of the other result (ifOk)- Parameters:
res- The other result- Returns:
resif this isOk, this error otherwise- See Also:
-
toTuple
Converts this result to aPairtuple having the value and the error as fields- Returns:
- The constructed tuple
-
equals
-