Interface Result<Value,Error>
- Type Parameters:
Value- the type of the value contained in a successful resultError- the type of the error contained in an erroneous result
- All Known Implementing Classes:
Result.Err, Result.Ok
Result.Ok) or an error (Result.Err).
This is a common functional programming construct that allows for
handling both success and error cases in a unified type.
This interface is NullMarked: all types are non-null by default.
Ok.value is guaranteed non-null; use Option inside the value type
to model an optional successful result.
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic final recordRepresents an erroneous result containing an error of typeError.static final recordRepresents a successful result containing a value of typeValue.static final recordA typed container holding the two partitions produced bypartitioningBy(). -
Method Summary
Modifier and TypeMethodDescriptionstatic <Value,Error>
Result<Value, Error> err(Error error) Creates aResultinstance representing an erroneous result with the given error value.static <Value,Error>
Result<Value, Error> Deprecated, for removal: This API element is subject to removal in a future version.This overload exists only as a type-inference aid and is no longer needed.Filters the current result based on the specified predicate.Filters the current result by applying a given predicate to its value.Applies the given mapping function to the value of a successfulResultinstance and returns the resultingResult.flatMapError(Function<Error, Result<Value, E2>> mapper) Applies a function to the error of an erroneous result and returns the producedResult.default <R> RFolds this container to a single value by applying the appropriate function.fromFuture(CompletableFuture<? extends V> future) Converts aCompletableFutureinto aResultby blocking until the future completes.static <V,E> Result <V, E> fromOption(Option<? extends V> opt, E errorIfNone) static <V> Result<V, NoSuchElementException> fromOptional(Optional<? extends V> optional) Converts a Try instance into a Result instance.default Valueget()Retrieves the value contained within thisResultinstance if it represents a successful result.default ErrorgetError()Retrieves the error value of the current instance if it is an Err.default ValueReturns the success value if present, orfallbackotherwise.default ValuegetOrElseGet(Supplier<Value> supplier) Returns the success value if present, or the value produced bysupplierotherwise.default ValuegetOrElseGetWithError(Function<Error, Value> errorMapper) Returns the value of thisOk, or applieserrorMapperto the contained error and returns the result.default ValueReturns the success value if present, ornullotherwise.default ValuegetOrThrow(Function<Error, ? extends RuntimeException> exMapper) Returns the success value if present, or throws an exception mapped from the error.static <V,K> Collector <V, ?, Map<K, NonEmptyList<V>>> groupingBy(Function<? super V, ? extends K> classifier) Returns aCollectorthat groups stream elements by a key derived from each element.groupingBy(Function<? super V, ? extends K> classifier, Function<? super NonEmptyList<V>, ? extends R> downstream) Returns aCollectorthat groups stream elements by a key derived from each element and applies a downstream function to each group'sNonEmptyList.default booleanisError()Returnstrueif this instance is the error (Result.Err) variant.default booleanisOk()Checks if the current instance represents a successful result.default booleanReturnstrueif this instance is the success (Result.Ok) variant.Transforms the value of a successfulResultinstance using the provided mapping function.Transforms the error value of an erroneousResultinstance using the provided mapping function.default voidExecutes one of the two consumers based on the active variant.static <Value,Error>
Result<Value, Error> ok(Value value) Creates aResultinstance representing a successful result with the given value.static <Value,Error>
Result<Value, Error> Deprecated, for removal: This API element is subject to removal in a future version.This overload exists only as a type-inference aid and is no longer needed.Returns thisResultif it isOk; otherwise evaluates the given supplier and returns its result.Returns thisResultif it isOk, otherwise returnsalternative.Returns thisResultif it isOk, otherwise evaluates and returns the supplier's result.static <V,E> Collector <Result<V, E>, ?, Result.Partition<V, E>> Returns aCollectorthat partitions aStream<Result<V, E>>into two typed lists:Result.Partition.oks()for values fromOkelements, andResult.Partition.errors()for errors fromErrelements.Executes the given action if the currentResultinstance represents a successful result.Executes the given action if the currentResultinstance represents an erroneous result.Converts an erroneousResultinto a successful one by applying the given rescue function to the contained error.recoverWith(Function<Error, Result<Value, E2>> rescue) Converts an erroneousResultinto a newResultby applying the given rescue function to the contained error.Transforms an iterable ofResult<V, E>into a singleResult<List<V>, E>.Transforms a stream ofResult<V, E>into a singleResult<List<V>, E>.stream()Returns a single-element stream of the success value, or an empty stream on error.swap()Swaps theOkandErrchannels of thisResult.toEither()Converts thisResultto anEither.default CompletableFuture<Value> toFuture()Converts thisResultinto an already-completedCompletableFuture.default CompletableFuture<Value> Converts thisResultinto an already-completedCompletableFuture, usingerrorMapperto convert the error value into aThrowablewhen this isErr.toList()toOption()Converts this container to anOption.Converts the current result into aTryinstance.Maps each element of an iterable throughmapperand collects the results into aResult<List<B>, E>.Maps each element of a stream throughmapperand collects the results into aResult<List<B>, E>.zip3(Result<? extends V1, ? extends E> r1, Result<? extends V2, ? extends E> r2, Result<? extends V3, ? extends E> r3) zip4(Result<? extends V1, ? extends E> r1, Result<? extends V2, ? extends E> r2, Result<? extends V3, ? extends E> r3, Result<? extends V4, ? extends E> r4) static <V1,V2, V3, R, E>
Result<R, E> zipWith3(Result<? extends V1, ? extends E> r1, Result<? extends V2, ? extends E> r2, Result<? extends V3, ? extends E> r3, TriFunction<? super V1, ? super V2, ? super V3, ? extends R> combiner) Combines threeResultvalues using aTriFunction.static <V1,V2, V3, V4, R, E>
Result<R, E> zipWith4(Result<? extends V1, ? extends E> r1, Result<? extends V2, ? extends E> r2, Result<? extends V3, ? extends E> r3, Result<? extends V4, ? extends E> r4, QuadFunction<? super V1, ? super V2, ? super V3, ? super V4, ? extends R> combiner) Combines fourResultvalues using aQuadFunction.
-
Method Details
-
ok
Creates aResultinstance representing a successful result with the given value. This method is used to indicate that a computation or action has succeeded.- Type Parameters:
Value- the type of the value contained in the successful resultError- the type of the error that would be contained in an erroneous result- Parameters:
value- the non-null value to encapsulate in the successful result- Returns:
- a
Resultinstance of typeOk<Value, Error>containing the provided value - Throws:
NullPointerException- ifvalueisnull
-
ok
@Deprecated(forRemoval=true) static <Value,Error> Result<Value,Error> ok(Value value, Class<Error> errorClassIgnored) Deprecated, for removal: This API element is subject to removal in a future version.This overload exists only as a type-inference aid and is no longer needed. Useok(Object)with an explicit type witness or a typed variable instead:Result.<Value, Error>ok(value)orResult<Value, Error> r = Result.ok(value)Creates an instance ofOkrepresenting a successful result.- Type Parameters:
Value- the type of the value in the success caseError- the type of the error in the error case- Parameters:
value- the value to be wrapped in theOkinstanceerrorClassIgnored- the class object of the error type, ignored in this method- Returns:
- an
Okinstance containing the provided value
-
err
Creates aResultinstance representing an erroneous result with the given error value. This method is used to indicate that a computation or action has failed.- Type Parameters:
Value- the type of the value that would be contained in a successful result, unused hereError- the type of the error contained in the erroneous result- Parameters:
error- the non-null error value to encapsulate in the erroneous result- Returns:
- a
Resultinstance of typeErr<Value, Error>containing the provided error - Throws:
NullPointerException- iferrorisnull
-
err
@Deprecated(forRemoval=true) static <Value,Error> Result<Value,Error> err(Error error, Class<Value> classValueIgnored) Deprecated, for removal: This API element is subject to removal in a future version.This overload exists only as a type-inference aid and is no longer needed. Useerr(Object)with an explicit type witness or a typed variable instead:Result.<Value, Error>err(error)orResult<Value, Error> r = Result.err(error)Creates and returns a new result instance representing an error state.- Type Parameters:
Value- The type of the value that would have been contained in a success state.Error- The type of the error being represented.- Parameters:
error- The error to be encapsulated in the result.classValueIgnored- The ignored class type of the value, typically used for type inference.- Returns:
- A result instance representing an error state containing the provided error.
-
isSuccess
default boolean isSuccess()Returnstrueif this instance is the success (Result.Ok) variant. -
isError
default boolean isError()Returnstrueif this instance is the error (Result.Err) variant.- Returns:
trueforErr,falseforOk
-
isOk
default boolean isOk()Checks if the current instance represents a successful result.- Returns:
trueif the current instance is of typeOk, indicating success;falseotherwise.
-
get
Retrieves the value contained within thisResultinstance if it represents a successful result. If the instance represents an erroneous result, this method throws aNoSuchElementException.- Returns:
- the non-null value contained in the successful result
- Throws:
NoSuchElementException- if the instance represents an erroneous result
-
getError
Retrieves the error value of the current instance if it is an Err. If the instance is an Ok, calling this method will throw a NoSuchElementException.- Returns:
- the error value of the instance if it is an Err.
- Throws:
NoSuchElementException- if the instance is an Ok.
-
map
Transforms the value of a successfulResultinstance using the provided mapping function. If theResultinstance represents an error, the error remains unchanged.- Type Parameters:
NewValue- the type of the value after applying the mapping function- Parameters:
mapper- the function to apply to the value of the successful result- Returns:
- a new
Resultinstance, containing the mapped value if the original instance was successful, or the same error if the original instance was an error
-
mapError
Transforms the error value of an erroneousResultinstance using the provided mapping function. If theResultinstance represents success, the value remains unchanged.- Type Parameters:
NewError- the type of the error after applying the mapping function- Parameters:
mapper- the function to apply to the error of the erroneous result- Returns:
- a new
Resultinstance, containing the transformed error if the original instance was an error, or the same value if the original instance was successful
-
flatMap
Applies the given mapping function to the value of a successfulResultinstance and returns the resultingResult. If the instance represents an error, the error is propagated without applying the mapping function.- Type Parameters:
NewValue- the type of the value in the resultingResultafter applying the mapping function- Parameters:
mapper- the function to apply to the value of the successfulResultto produce a newResult- Returns:
- a new
Resultinstance returned by applying the mapping function to the value if the original instance was successful, or the same error if the original instance was an error
-
peek
Executes the given action if the currentResultinstance represents a successful result. If the instance is of typeOk, the provided action is applied to the contained value. In both cases, the originalResultinstance is returned unchanged.- Parameters:
action- aConsumerthat accepts the value contained in a successful result- Returns:
- the original
Resultinstance
-
peekError
Executes the given action if the currentResultinstance represents an erroneous result. If the instance is of typeErr, the provided action is applied to the contained error value. In both cases, the originalResultinstance is returned unchanged.- Parameters:
action- aConsumerthat accepts the error contained in an erroneous result- Returns:
- the original
Resultinstance
-
filter
Filters the current result based on the specified predicate. If the result is an instance of Ok and the predicate returns false for the value, a new error result is returned with the specified error value. If the current result does not match Ok or the predicate evaluates to true, the current result is returned unchanged.- Parameters:
predicate- the condition to test the value of the resulterrorIfFalse- the error to return if the predicate evaluates to false- Returns:
- the current result if it is not an Ok instance or the predicate evaluates to true, otherwise a new error result with the specified error value
-
filter
Filters the current result by applying a given predicate to its value. If the result is of type Ok and the predicate evaluates to false, the method transforms it into an Err with the error provided by the errorIfFalse function. If the predicate evaluates to true or the result is already of type Err, the original result is returned.- Parameters:
predicate- the condition to be evaluated on the value wrapped by the Ok result.errorIfFalse- a function to generate an error if the predicate evaluates to false.- Returns:
- a filtered result, returning the same instance if the predicate evaluates to true or the result is Err; otherwise, returns an Err with the provided error value.
-
recover
Converts an erroneousResultinto a successful one by applying the given rescue function to the contained error. If this instance is alreadyOk, it is returned unchanged.- Parameters:
rescue- a function that maps the error value to a recovery value; must not returnnull- Returns:
- an
Okresult with the recovered value, or the originalOkif already successful - Throws:
NullPointerException- ifrescueis null or ifrescuereturnsnull
-
recoverWith
Converts an erroneousResultinto a newResultby applying the given rescue function to the contained error. Unlikerecover(Function), the rescue function may itself return anErr. If this instance is alreadyOk, it is returned as anOkof the new error type.- Type Parameters:
E2- the error type of the resultingResult- Parameters:
rescue- a function that maps the error to a newResult; must not returnnull- Returns:
- the result of applying
rescueto the error, or anOkwrapping the original value - Throws:
NullPointerException- ifrescueis null or ifrescuereturnsnull
-
orElse
Returns thisResultif it isOk, otherwise returnsalternative.- Parameters:
alternative- the fallbackResultto return when this isErr; must not benull- Returns:
- this instance if
Ok, otherwisealternative - Throws:
NullPointerException- ifalternativeis null
-
orElse
default Result<Value,Error> orElse(Supplier<? extends Result<? extends Value, ? extends Error>> alternative) Returns thisResultif it isOk, otherwise evaluates and returns the supplier's result. The supplier is not called when this instance isOk.- Parameters:
alternative- a lazy supplier of a fallbackResult; must not returnnull- Returns:
- this instance if
Ok, or the result ofalternative.get()ifErr - Throws:
NullPointerException- ifalternativeis null or returnsnull
-
or
Returns thisResultif it isOk; otherwise evaluates the given supplier and returns its result. The supplier is not called when this instance isOk.- Parameters:
fallback- a lazy supplier of an alternativeResultevaluated only onErr; must not returnnull- Returns:
- this instance if
Ok, or the result offallback.get()ifErr - Throws:
NullPointerException- iffallbackis null or iffallbackreturnsnull
-
flatMapError
Applies a function to the error of an erroneous result and returns the producedResult. If this instance isOk, it is propagated unchanged (with a potentially different error type). This is the dual offlatMap(Function): it operates on the error channel instead of the value channel.- Type Parameters:
E2- the error type of the resultingResult- Parameters:
mapper- a function that maps the current error to a newResult; must not returnnull- Returns:
- the mapped result for
Err, or the original value wrapped asOkforOk - Throws:
NullPointerException- ifmapperis null or ifmapperreturnsnull
-
swap
-
getOrElseGetWithError
Returns the value of thisOk, or applieserrorMapperto the contained error and returns the result. UnlikegetOrElseGet(Supplier), the fallback function receives the error value, which is useful for deriving a default from the error itself.This method is intentionally named differently from
getOrElseGet(Supplier)to avoid overload ambiguity when passing a null literal or a Groovy/Kotlin closure.- Parameters:
errorMapper- a function that maps the error to a fallback value- Returns:
- the contained value if
Ok, or the result oferrorMapperifErr
-
toTry
Converts the current result into aTryinstance. If the current result represents a success, the returnedTrywill contain the success value. If the current result represents an error, the specified function is used to transform the error into aThrowable, and the resultingTrywill represent a failure.- Parameters:
errorToThrowable- aFunctionthat converts the error type to aThrowable. Must not return null.- Returns:
- a
Tryinstance representing a success or failure based on the state of the current result. - Throws:
NullPointerException- iferrorToThrowableis null or iferrorToThrowable.apply()returns null.
-
toFuture
Converts thisResultinto an already-completedCompletableFuture.If this is
Ok, returns a future completed normally with the value. If this isErr, returns a future completed exceptionally with aNoSuchElementExceptionwhose message includes the string representation of the error — consistent with the behaviour ofget().Use
toFuture(Function)when you need full control over the exception type.- Returns:
- a completed
CompletableFuture<Value>
-
toFuture
Converts thisResultinto an already-completedCompletableFuture, usingerrorMapperto convert the error value into aThrowablewhen this isErr.If this is
Ok, returns a future completed normally with the value. If this isErr, applieserrorMapperto the error and returns a future completed exceptionally with the result.- Parameters:
errorMapper- a function that converts the error value to aThrowable; must not benulland must not returnnull- Returns:
- a completed
CompletableFuture<Value> - Throws:
NullPointerException- iferrorMapperisnullor returnsnull
-
toEither
Converts thisResultto anEither.Ok(v)maps toEither.right(v);Err(e)maps toEither.left(e). The resultingEither<Error, Value>preserves the right-bias convention: the success value is on the right track.- Returns:
- an
Either<Error, Value>representing this result
-
fromOption
- Type Parameters:
V- The type of the value contained in theOption.E- The type of the error value used in theResult.- Parameters:
opt- TheOptionto convert. Must not be null.errorIfNone- The error value to return if theOptionis empty.- Returns:
- A
Resultthat wraps the value from theOptionif it is defined, or an error containingerrorIfNoneif theOptionis empty.
-
fromTry
Converts a Try instance into a Result instance.- Type Parameters:
V- the type of the value contained in the Try instance- Parameters:
t- the Try instance to be converted; must not be null- Returns:
- a Result instance that represents either the success or failure of the given Try instance
-
fromOptional
Converts aOptionalinto aResult. If theOptionalcontains a value, returnsOkwith that value. If theOptionalis empty, returnsErrwith aNoSuchElementException.- Type Parameters:
V- the value type- Parameters:
optional- theOptionalto convert; must not benull- Returns:
Ok(value)if theOptionalis present, orErr(NoSuchElementException)if empty- Throws:
NullPointerException- ifoptionalisnull
-
fromFuture
Converts aCompletableFutureinto aResultby blocking until the future completes.If the future completes normally, returns
Ok(value). If the future completes exceptionally, theCompletionExceptionwrapper is unwrapped and the original cause is stored asErr(cause). If the future was cancelled, returnsErr(CancellationException).- Type Parameters:
V- the type of the future's value- Parameters:
future- theCompletableFutureto convert; must not benull- Returns:
Ok(value)on normal completion, orErr(cause)otherwise- Throws:
NullPointerException- iffutureisnull
-
sequence
Transforms an iterable ofResult<V, E>into a singleResult<List<V>, E>. If any element is anErr, that error is returned immediately (fail-fast). If all elements areOk, returnsOkcontaining an unmodifiable list of values in encounter order.- Type Parameters:
V- the value typeE- the error type- Parameters:
results- the iterable of results; must not benulland must not containnullelements- Returns:
Ok(List<V>)if all elements areOk, or the firstErrencountered- Throws:
NullPointerException- ifresultsisnullor contains anullelement
-
sequence
Transforms a stream ofResult<V, E>into a singleResult<List<V>, E>. If any element is anErr, that error is returned immediately (fail-fast) and the stream is closed. If all elements areOk, returnsOkcontaining an unmodifiable list of values in encounter order.- Type Parameters:
V- the value typeE- the error type- Parameters:
results- the stream of results; must not benulland must not containnullelements- Returns:
Ok(List<V>)if all elements areOk, or the firstErrencountered- Throws:
NullPointerException- ifresultsisnullor contains anullelement
-
traverse
static <A,B, Result<List<B>,E> E> traverse(Iterable<A> values, Function<? super A, Result<B, E>> mapper) Maps each element of an iterable throughmapperand collects the results into aResult<List<B>, E>. Fails fast on the firstErrreturned by the mapper.- Type Parameters:
A- the input element typeB- the mapped value typeE- the error type- Parameters:
values- the iterable of input values; must not benullmapper- a function that maps each value to aResult; must not benulland must not returnnull- Returns:
Ok(List<B>)if all mappings succeed, or the firstErrproduced by the mapper- Throws:
NullPointerException- ifvaluesormapperisnull, or if the mapper returnsnull
-
traverse
static <A,B, Result<List<B>,E> E> traverse(Stream<A> values, Function<? super A, Result<B, E>> mapper) Maps each element of a stream throughmapperand collects the results into aResult<List<B>, E>. Fails fast on the firstErrreturned by the mapper; the stream is closed in all cases.- Type Parameters:
A- the input element typeB- the mapped value typeE- the error type- Parameters:
values- the stream of input values; must not benullmapper- a function that maps each value to aResult; must not benulland must not returnnull- Returns:
Ok(List<B>)if all mappings succeed, or the firstErrproduced by the mapper- Throws:
NullPointerException- ifvaluesormapperisnull, or if the mapper returnsnull
-
toList
Returns aCollectorthat accumulates aStream<Result<V, E>>into a singleResult<List<V>, E>.Note: this Collector is not fail-fast. Because the Java
CollectorAPI always feeds every stream element to the accumulator before the finisher runs, all elements are always consumed from the stream regardless of whether anyErris present. The finisher then scans the accumulated list and returns the firstErrfound, orOkwith an unmodifiable list of values in encounter order if none exists. For true fail-fast / short-circuit behaviour usesequence(Stream)instead.Example:
Result<List<Integer>, String> r = Stream.of(Result.ok(1), Result.ok(2), Result.ok(3)) .collect(Result.toList());- Type Parameters:
V- the value typeE- the error type- Returns:
- a collector that consumes all stream elements and produces
Ok(List<V>)if every element isOk, or the firstErrfound in encounter order - Throws:
NullPointerException- if the stream contains anullelement
-
partitioningBy
Returns aCollectorthat partitions aStream<Result<V, E>>into two typed lists:Result.Partition.oks()for values fromOkelements, andResult.Partition.errors()for errors fromErrelements. Both lists are unmodifiable and maintain encounter order.Example:
Result.Partition<Integer, String> p = Stream.of(Result.ok(1), Result.err("bad"), Result.ok(3)) .collect(Result.partitioningBy()); // p.oks() == [1, 3] // p.errors() == ["bad"]- Type Parameters:
V- the value type of theOkelementsE- the error type of theErrelements- Returns:
- a collector producing a
Result.Partitionof ok-values and errors - Throws:
NullPointerException- if the stream contains anullelement
-
groupingBy
static <V,K> Collector<V, ?, Map<K, NonEmptyList<V>>> groupingBy(Function<? super V, ? extends K> classifier) Returns aCollectorthat groups stream elements by a key derived from each element. Each group is collected into aNonEmptyList, making the non-emptiness of every group explicit in the type.Example:
Map<String, NonEmptyList<String>> byFirstChar = Stream.of("apple", "avocado", "banana") .collect(Result.groupingBy(s -> s.substring(0, 1))); // {"a" -> ["apple", "avocado"], "b" -> ["banana"]}- Type Parameters:
V- the element typeK- the key type produced by the classifier- Parameters:
classifier- function mapping each element to its group key; must not benulland must not returnnull- Returns:
- an unmodifiable
Map<K, NonEmptyList<V>>preserving encounter order - Throws:
NullPointerException- ifclassifierisnull, if a stream element isnull, or if the classifier returnsnull
-
groupingBy
static <V,K, Collector<V,R> ?, groupingByMap<K, R>> (Function<? super V, ? extends K> classifier, Function<? super NonEmptyList<V>, ? extends R> downstream) Returns aCollectorthat groups stream elements by a key derived from each element and applies a downstream function to each group'sNonEmptyList.Example:
Map<String, Long> countByFirstChar = Stream.of("apple", "avocado", "banana") .collect(Result.groupingBy(s -> s.substring(0, 1), NonEmptyList::size)); // {"a" -> 2, "b" -> 1}- Type Parameters:
V- the element typeK- the key type produced by the classifierR- the result type produced by the downstream function- Parameters:
classifier- function mapping each element to its group key; must not benulland must not returnnulldownstream- function applied to each group'sNonEmptyList; must not benulland must not returnnull- Returns:
- an unmodifiable
Map<K, R>preserving encounter order - Throws:
NullPointerException- ifclassifierordownstreamisnull, if a stream element isnull, or if the classifier or downstream returnsnull
-
zip
static <V1,V2, Result<Tuple2<V1,E> V2>, E> zip(Result<? extends V1, ? extends E> r1, Result<? extends V2, ? extends E> r2) Combines twoResultvalues into a singleResultcontaining aTuple2. Fail-fast: returns the firstErrencountered.- Type Parameters:
V1- value type of the first resultV2- value type of the second resultE- shared error type- Parameters:
r1- first result; must not benullr2- second result; must not benull- Returns:
Ok(Tuple2(v1, v2))if both areOk, otherwise the firstErr- Throws:
NullPointerException- ifr1orr2isnull
-
zip3
static <V1,V2, Result<Tuple3<V1,V3, E> V2, zip3V3>, E> (Result<? extends V1, ? extends E> r1, Result<? extends V2, ? extends E> r2, Result<? extends V3, ? extends E> r3) Combines threeResultvalues into a singleResultcontaining aTuple3. Fail-fast: returns the firstErrencountered.- Type Parameters:
V1- value type of the first resultV2- value type of the second resultV3- value type of the third resultE- shared error type- Parameters:
r1- first result; must not benullr2- second result; must not benullr3- third result; must not benull- Returns:
Ok(Tuple3(v1, v2, v3))if all three areOk, otherwise the firstErr- Throws:
NullPointerException- if any argument isnull
-
zipWith3
static <V1,V2, Result<R,V3, R, E> E> zipWith3(Result<? extends V1, ? extends E> r1, Result<? extends V2, ? extends E> r2, Result<? extends V3, ? extends E> r3, TriFunction<? super V1, ? super V2, ? super V3, ? extends R> combiner) - Type Parameters:
V1- value type of the first resultV2- value type of the second resultV3- value type of the third resultR- result value typeE- shared error type- Parameters:
r1- first result; must not benullr2- second result; must not benullr3- third result; must not benullcombiner- function applied to the three values; must not benull- Returns:
Ok(combiner(v1, v2, v3))if all three areOk, otherwise the firstErr- Throws:
NullPointerException- if any argument isnull
-
zip4
static <V1,V2, Result<Tuple4<V1,V3, V4, E> V2, zip4V3, V4>, E> (Result<? extends V1, ? extends E> r1, Result<? extends V2, ? extends E> r2, Result<? extends V3, ? extends E> r3, Result<? extends V4, ? extends E> r4) Combines fourResultvalues into a singleResultcontaining aTuple4. Fail-fast: returns the firstErrencountered.- Type Parameters:
V1- value type of the first resultV2- value type of the second resultV3- value type of the third resultV4- value type of the fourth resultE- shared error type- Parameters:
r1- first result; must not benullr2- second result; must not benullr3- third result; must not benullr4- fourth result; must not benull- Returns:
Ok(Tuple4(v1,v2,v3,v4))if all four areOk, otherwise the firstErr- Throws:
NullPointerException- if any argument isnull
-
zipWith4
static <V1,V2, Result<R,V3, V4, R, E> E> zipWith4(Result<? extends V1, ? extends E> r1, Result<? extends V2, ? extends E> r2, Result<? extends V3, ? extends E> r3, Result<? extends V4, ? extends E> r4, QuadFunction<? super V1, ? super V2, ? super V3, ? super V4, ? extends R> combiner) - Type Parameters:
V1- value type of the first resultV2- value type of the second resultV3- value type of the third resultV4- value type of the fourth resultR- result value typeE- shared error type- Parameters:
r1- first result; must not benullr2- second result; must not benullr3- third result; must not benullr4- fourth result; must not benullcombiner- function applied to the four values; must not benull- Returns:
Ok(combiner(v1,v2,v3,v4))if all four areOk, otherwise the firstErr- Throws:
NullPointerException- if any argument isnull
-
getOrElse
Returns the success value if present, orfallbackotherwise.- Parameters:
fallback- the non-null fallback value- Returns:
- the success value, or
fallbackif this is the error variant - Throws:
NullPointerException- iffallbackisnull
-
getOrElseGet
Returns the success value if present, or the value produced bysupplierotherwise.- Parameters:
supplier- supplier of the fallback value; must not returnnull- Returns:
- the success value, or the value produced by
supplier - Throws:
NullPointerException- ifsupplierisnullor returnsnull
-
getOrNull
Returns the success value if present, ornullotherwise.- Returns:
- the success value or
null
-
getOrThrow
Returns the success value if present, or throws an exception mapped from the error.- Parameters:
exMapper- maps the error to aRuntimeException; must not returnnull- Returns:
- the success value
- Throws:
NullPointerException- ifexMapperisnullor returnsnullRuntimeException- the exception produced byexMapperon the error variant
-
stream
-
fold
Folds this container to a single value by applying the appropriate function.- Type Parameters:
R- the result type- Parameters:
onSuccess- applied to the success value; must not returnnullonError- applied to the error value; must not returnnull- Returns:
- the result of the applied function
- Throws:
NullPointerException- if either function isnullor returnsnull
-
match
Executes one of the two consumers based on the active variant.- Parameters:
onSuccess- called with the success valueonError- called with the error value- Throws:
NullPointerException- if either consumer isnull
-
toOption
Converts this container to anOption. The success variant maps toOption.Some; the error variant maps toOption.none().- Returns:
- an
Optioncontaining the success value, or empty
-