Interface Result<Value,Error>

Type Parameters:
Value - the type of the value contained in a successful result
Error - the type of the error contained in an erroneous result
All Known Implementing Classes:
Result.Err, Result.Ok

@NullMarked public sealed interface Result<Value,Error> permits Result.Ok<Value,Error>, Result.Err<Value,Error>
A generic sealed interface representing a result type that can either be a successful value (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.

  • Method Details

    • ok

      static <Value,Error> Result<Value,Error> ok(Value value)
      Creates a Result instance 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 result
      Error - 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 Result instance of type Ok<Value, Error> containing the provided value
      Throws:
      NullPointerException - if value is null
    • 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. Use ok(Object) with an explicit type witness or a typed variable instead: Result.<Value, Error>ok(value) or Result<Value, Error> r = Result.ok(value)
      Creates an instance of Ok representing a successful result.
      Type Parameters:
      Value - the type of the value in the success case
      Error - the type of the error in the error case
      Parameters:
      value - the value to be wrapped in the Ok instance
      errorClassIgnored - the class object of the error type, ignored in this method
      Returns:
      an Ok instance containing the provided value
    • err

      static <Value,Error> Result<Value,Error> err(Error error)
      Creates a Result instance 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 here
      Error - 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 Result instance of type Err<Value, Error> containing the provided error
      Throws:
      NullPointerException - if error is null
    • 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. Use err(Object) with an explicit type witness or a typed variable instead: Result.<Value, Error>err(error) or Result<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()
      Returns true if this instance is the success (Result.Ok) variant.
    • isError

      default boolean isError()
      Returns true if this instance is the error (Result.Err) variant.
      Returns:
      true for Err, false for Ok
    • isOk

      default boolean isOk()
      Checks if the current instance represents a successful result.
      Returns:
      true if the current instance is of type Ok, indicating success; false otherwise.
    • get

      default Value get()
      Retrieves the value contained within this Result instance if it represents a successful result. If the instance represents an erroneous result, this method throws a NoSuchElementException.
      Returns:
      the non-null value contained in the successful result
      Throws:
      NoSuchElementException - if the instance represents an erroneous result
    • getError

      default Error 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

      default <NewValue> Result<NewValue,Error> map(Function<Value,NewValue> mapper)
      Transforms the value of a successful Result instance using the provided mapping function. If the Result instance 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 Result instance, containing the mapped value if the original instance was successful, or the same error if the original instance was an error
    • mapError

      default <NewError> Result<Value,NewError> mapError(Function<Error,NewError> mapper)
      Transforms the error value of an erroneous Result instance using the provided mapping function. If the Result instance 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 Result instance, containing the transformed error if the original instance was an error, or the same value if the original instance was successful
    • flatMap

      default <NewValue> Result<NewValue,Error> flatMap(Function<Value, Result<NewValue,Error>> mapper)
      Applies the given mapping function to the value of a successful Result instance and returns the resulting Result. 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 resulting Result after applying the mapping function
      Parameters:
      mapper - the function to apply to the value of the successful Result to produce a new Result
      Returns:
      a new Result instance 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

      default Result<Value,Error> peek(Consumer<Value> action)
      Executes the given action if the current Result instance represents a successful result. If the instance is of type Ok, the provided action is applied to the contained value. In both cases, the original Result instance is returned unchanged.
      Parameters:
      action - a Consumer that accepts the value contained in a successful result
      Returns:
      the original Result instance
    • peekError

      default Result<Value,Error> peekError(Consumer<Error> action)
      Executes the given action if the current Result instance represents an erroneous result. If the instance is of type Err, the provided action is applied to the contained error value. In both cases, the original Result instance is returned unchanged.
      Parameters:
      action - a Consumer that accepts the error contained in an erroneous result
      Returns:
      the original Result instance
    • filter

      default Result<Value,Error> filter(Predicate<Value> predicate, Error errorIfFalse)
      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 result
      errorIfFalse - 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

      default Result<Value,Error> filter(Predicate<Value> predicate, Function<Value,Error> errorIfFalse)
      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

      default Result<Value,Error> recover(Function<Error,Value> rescue)
      Converts an erroneous Result into a successful one by applying the given rescue function to the contained error. If this instance is already Ok, it is returned unchanged.
      Parameters:
      rescue - a function that maps the error value to a recovery value; must not return null
      Returns:
      an Ok result with the recovered value, or the original Ok if already successful
      Throws:
      NullPointerException - if rescue is null or if rescue returns null
    • recoverWith

      default <E2> Result<Value,E2> recoverWith(Function<Error, Result<Value,E2>> rescue)
      Converts an erroneous Result into a new Result by applying the given rescue function to the contained error. Unlike recover(Function), the rescue function may itself return an Err. If this instance is already Ok, it is returned as an Ok of the new error type.
      Type Parameters:
      E2 - the error type of the resulting Result
      Parameters:
      rescue - a function that maps the error to a new Result; must not return null
      Returns:
      the result of applying rescue to the error, or an Ok wrapping the original value
      Throws:
      NullPointerException - if rescue is null or if rescue returns null
    • orElse

      default Result<Value,Error> orElse(Result<? extends Value, ? extends Error> alternative)
      Returns this Result if it is Ok, otherwise returns alternative.
      Parameters:
      alternative - the fallback Result to return when this is Err; must not be null
      Returns:
      this instance if Ok, otherwise alternative
      Throws:
      NullPointerException - if alternative is null
    • orElse

      default Result<Value,Error> orElse(Supplier<? extends Result<? extends Value, ? extends Error>> alternative)
      Returns this Result if it is Ok, otherwise evaluates and returns the supplier's result. The supplier is not called when this instance is Ok.
      Parameters:
      alternative - a lazy supplier of a fallback Result; must not return null
      Returns:
      this instance if Ok, or the result of alternative.get() if Err
      Throws:
      NullPointerException - if alternative is null or returns null
    • or

      default Result<Value,Error> or(Supplier<Result<Value,Error>> fallback)
      Returns this Result if it is Ok; otherwise evaluates the given supplier and returns its result. The supplier is not called when this instance is Ok.
      Parameters:
      fallback - a lazy supplier of an alternative Result evaluated only on Err; must not return null
      Returns:
      this instance if Ok, or the result of fallback.get() if Err
      Throws:
      NullPointerException - if fallback is null or if fallback returns null
    • flatMapError

      default <E2> Result<Value,E2> flatMapError(Function<Error, Result<Value,E2>> mapper)
      Applies a function to the error of an erroneous result and returns the produced Result. If this instance is Ok, it is propagated unchanged (with a potentially different error type). This is the dual of flatMap(Function): it operates on the error channel instead of the value channel.
      Type Parameters:
      E2 - the error type of the resulting Result
      Parameters:
      mapper - a function that maps the current error to a new Result; must not return null
      Returns:
      the mapped result for Err, or the original value wrapped as Ok for Ok
      Throws:
      NullPointerException - if mapper is null or if mapper returns null
    • swap

      default Result<Error,Value> swap()
      Swaps the Ok and Err channels of this Result. An Ok(value) becomes Err(value) and an Err(error) becomes Ok(error).
      Returns:
      a Result with the value and error channels exchanged
    • getOrElseGetWithError

      default Value getOrElseGetWithError(Function<Error,Value> errorMapper)
      Returns the value of this Ok, or applies errorMapper to the contained error and returns the result. Unlike getOrElseGet(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 of errorMapper if Err
    • toTry

      default Try<Value> toTry(Function<? super Error, ? extends Throwable> errorToThrowable)
      Converts the current result into a Try instance. If the current result represents a success, the returned Try will contain the success value. If the current result represents an error, the specified function is used to transform the error into a Throwable, and the resulting Try will represent a failure.
      Parameters:
      errorToThrowable - a Function that converts the error type to a Throwable. Must not return null.
      Returns:
      a Try instance representing a success or failure based on the state of the current result.
      Throws:
      NullPointerException - if errorToThrowable is null or if errorToThrowable.apply() returns null.
    • toFuture

      default CompletableFuture<Value> toFuture()
      Converts this Result into an already-completed CompletableFuture.

      If this is Ok, returns a future completed normally with the value. If this is Err, returns a future completed exceptionally with a NoSuchElementException whose message includes the string representation of the error — consistent with the behaviour of get().

      Use toFuture(Function) when you need full control over the exception type.

      Returns:
      a completed CompletableFuture<Value>
    • toFuture

      default CompletableFuture<Value> toFuture(Function<? super Error, ? extends Throwable> errorMapper)
      Converts this Result into an already-completed CompletableFuture, using errorMapper to convert the error value into a Throwable when this is Err.

      If this is Ok, returns a future completed normally with the value. If this is Err, applies errorMapper to the error and returns a future completed exceptionally with the result.

      Parameters:
      errorMapper - a function that converts the error value to a Throwable; must not be null and must not return null
      Returns:
      a completed CompletableFuture<Value>
      Throws:
      NullPointerException - if errorMapper is null or returns null
    • toEither

      default Either<Error,Value> toEither()
      Converts this Result to an Either.

      Ok(v) maps to Either.right(v); Err(e) maps to Either.left(e). The resulting Either<Error, Value> preserves the right-bias convention: the success value is on the right track.

      Returns:
      an Either<Error, Value> representing this result
    • fromOption

      static <V,E> Result<V,E> fromOption(Option<? extends V> opt, E errorIfNone)
      Converts an Option to a Result.
      Type Parameters:
      V - The type of the value contained in the Option.
      E - The type of the error value used in the Result.
      Parameters:
      opt - The Option to convert. Must not be null.
      errorIfNone - The error value to return if the Option is empty.
      Returns:
      A Result that wraps the value from the Option if it is defined, or an error containing errorIfNone if the Option is empty.
    • fromTry

      static <V> Result<V, Throwable> fromTry(Try<V> t)
      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

      static <V> Result<V, NoSuchElementException> fromOptional(Optional<? extends V> optional)
      Converts a Optional into a Result. If the Optional contains a value, returns Ok with that value. If the Optional is empty, returns Err with a NoSuchElementException.
      Type Parameters:
      V - the value type
      Parameters:
      optional - the Optional to convert; must not be null
      Returns:
      Ok(value) if the Optional is present, or Err(NoSuchElementException) if empty
      Throws:
      NullPointerException - if optional is null
    • fromFuture

      static <V> Result<V, Throwable> fromFuture(CompletableFuture<? extends V> future)
      Converts a CompletableFuture into a Result by blocking until the future completes.

      If the future completes normally, returns Ok(value). If the future completes exceptionally, the CompletionException wrapper is unwrapped and the original cause is stored as Err(cause). If the future was cancelled, returns Err(CancellationException).

      Type Parameters:
      V - the type of the future's value
      Parameters:
      future - the CompletableFuture to convert; must not be null
      Returns:
      Ok(value) on normal completion, or Err(cause) otherwise
      Throws:
      NullPointerException - if future is null
    • sequence

      static <V,E> Result<List<V>,E> sequence(Iterable<Result<V,E>> results)
      Transforms an iterable of Result<V, E> into a single Result<List<V>, E>. If any element is an Err, that error is returned immediately (fail-fast). If all elements are Ok, returns Ok containing an unmodifiable list of values in encounter order.
      Type Parameters:
      V - the value type
      E - the error type
      Parameters:
      results - the iterable of results; must not be null and must not contain null elements
      Returns:
      Ok(List<V>) if all elements are Ok, or the first Err encountered
      Throws:
      NullPointerException - if results is null or contains a null element
    • sequence

      static <V,E> Result<List<V>,E> sequence(Stream<Result<V,E>> results)
      Transforms a stream of Result<V, E> into a single Result<List<V>, E>. If any element is an Err, that error is returned immediately (fail-fast) and the stream is closed. If all elements are Ok, returns Ok containing an unmodifiable list of values in encounter order.
      Type Parameters:
      V - the value type
      E - the error type
      Parameters:
      results - the stream of results; must not be null and must not contain null elements
      Returns:
      Ok(List<V>) if all elements are Ok, or the first Err encountered
      Throws:
      NullPointerException - if results is null or contains a null element
    • traverse

      static <A,B,E> Result<List<B>,E> traverse(Iterable<A> values, Function<? super A, Result<B,E>> mapper)
      Maps each element of an iterable through mapper and collects the results into a Result<List<B>, E>. Fails fast on the first Err returned by the mapper.
      Type Parameters:
      A - the input element type
      B - the mapped value type
      E - the error type
      Parameters:
      values - the iterable of input values; must not be null
      mapper - a function that maps each value to a Result; must not be null and must not return null
      Returns:
      Ok(List<B>) if all mappings succeed, or the first Err produced by the mapper
      Throws:
      NullPointerException - if values or mapper is null, or if the mapper returns null
    • traverse

      static <A,B,E> Result<List<B>,E> traverse(Stream<A> values, Function<? super A, Result<B,E>> mapper)
      Maps each element of a stream through mapper and collects the results into a Result<List<B>, E>. Fails fast on the first Err returned by the mapper; the stream is closed in all cases.
      Type Parameters:
      A - the input element type
      B - the mapped value type
      E - the error type
      Parameters:
      values - the stream of input values; must not be null
      mapper - a function that maps each value to a Result; must not be null and must not return null
      Returns:
      Ok(List<B>) if all mappings succeed, or the first Err produced by the mapper
      Throws:
      NullPointerException - if values or mapper is null, or if the mapper returns null
    • toList

      static <V,E> Collector<Result<V,E>, ?, Result<List<V>,E>> toList()
      Returns a Collector that accumulates a Stream<Result<V, E>> into a single Result<List<V>, E>.

      Note: this Collector is not fail-fast. Because the Java Collector API always feeds every stream element to the accumulator before the finisher runs, all elements are always consumed from the stream regardless of whether any Err is present. The finisher then scans the accumulated list and returns the first Err found, or Ok with an unmodifiable list of values in encounter order if none exists. For true fail-fast / short-circuit behaviour use sequence(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 type
      E - the error type
      Returns:
      a collector that consumes all stream elements and produces Ok(List<V>) if every element is Ok, or the first Err found in encounter order
      Throws:
      NullPointerException - if the stream contains a null element
    • partitioningBy

      static <V,E> Collector<Result<V,E>, ?, Result.Partition<V,E>> partitioningBy()
      Returns a Collector that partitions a Stream<Result<V, E>> into two typed lists: Result.Partition.oks() for values from Ok elements, and Result.Partition.errors() for errors from Err elements. 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 the Ok elements
      E - the error type of the Err elements
      Returns:
      a collector producing a Result.Partition of ok-values and errors
      Throws:
      NullPointerException - if the stream contains a null element
    • groupingBy

      static <V,K> Collector<V, ?, Map<K, NonEmptyList<V>>> groupingBy(Function<? super V, ? extends K> classifier)
      Returns a Collector that groups stream elements by a key derived from each element. Each group is collected into a NonEmptyList, 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 type
      K - the key type produced by the classifier
      Parameters:
      classifier - function mapping each element to its group key; must not be null and must not return null
      Returns:
      an unmodifiable Map<K, NonEmptyList<V>> preserving encounter order
      Throws:
      NullPointerException - if classifier is null, if a stream element is null, or if the classifier returns null
    • groupingBy

      static <V,K,R> Collector<V,?,Map<K,R>> groupingBy(Function<? super V, ? extends K> classifier, Function<? super NonEmptyList<V>, ? extends R> downstream)
      Returns a Collector that groups stream elements by a key derived from each element and applies a downstream function to each group's NonEmptyList.

      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 type
      K - the key type produced by the classifier
      R - the result type produced by the downstream function
      Parameters:
      classifier - function mapping each element to its group key; must not be null and must not return null
      downstream - function applied to each group's NonEmptyList; must not be null and must not return null
      Returns:
      an unmodifiable Map<K, R> preserving encounter order
      Throws:
      NullPointerException - if classifier or downstream is null, if a stream element is null, or if the classifier or downstream returns null
    • zip

      static <V1,V2,E> Result<Tuple2<V1,V2>, E> zip(Result<? extends V1, ? extends E> r1, Result<? extends V2, ? extends E> r2)
      Combines two Result values into a single Result containing a Tuple2. Fail-fast: returns the first Err encountered.
      Type Parameters:
      V1 - value type of the first result
      V2 - value type of the second result
      E - shared error type
      Parameters:
      r1 - first result; must not be null
      r2 - second result; must not be null
      Returns:
      Ok(Tuple2(v1, v2)) if both are Ok, otherwise the first Err
      Throws:
      NullPointerException - if r1 or r2 is null
    • zip3

      static <V1,V2,V3,E> Result<Tuple3<V1,V2,V3>, E> zip3(Result<? extends V1, ? extends E> r1, Result<? extends V2, ? extends E> r2, Result<? extends V3, ? extends E> r3)
      Combines three Result values into a single Result containing a Tuple3. Fail-fast: returns the first Err encountered.
      Type Parameters:
      V1 - value type of the first result
      V2 - value type of the second result
      V3 - value type of the third result
      E - shared error type
      Parameters:
      r1 - first result; must not be null
      r2 - second result; must not be null
      r3 - third result; must not be null
      Returns:
      Ok(Tuple3(v1, v2, v3)) if all three are Ok, otherwise the first Err
      Throws:
      NullPointerException - if any argument is null
    • zipWith3

      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 three Result values using a TriFunction. Fail-fast: returns the first Err encountered.
      Type Parameters:
      V1 - value type of the first result
      V2 - value type of the second result
      V3 - value type of the third result
      R - result value type
      E - shared error type
      Parameters:
      r1 - first result; must not be null
      r2 - second result; must not be null
      r3 - third result; must not be null
      combiner - function applied to the three values; must not be null
      Returns:
      Ok(combiner(v1, v2, v3)) if all three are Ok, otherwise the first Err
      Throws:
      NullPointerException - if any argument is null
    • zip4

      static <V1,V2,V3,V4,E> Result<Tuple4<V1,V2,V3,V4>, E> 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)
      Combines four Result values into a single Result containing a Tuple4. Fail-fast: returns the first Err encountered.
      Type Parameters:
      V1 - value type of the first result
      V2 - value type of the second result
      V3 - value type of the third result
      V4 - value type of the fourth result
      E - shared error type
      Parameters:
      r1 - first result; must not be null
      r2 - second result; must not be null
      r3 - third result; must not be null
      r4 - fourth result; must not be null
      Returns:
      Ok(Tuple4(v1,v2,v3,v4)) if all four are Ok, otherwise the first Err
      Throws:
      NullPointerException - if any argument is null
    • zipWith4

      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 four Result values using a QuadFunction. Fail-fast: returns the first Err encountered.
      Type Parameters:
      V1 - value type of the first result
      V2 - value type of the second result
      V3 - value type of the third result
      V4 - value type of the fourth result
      R - result value type
      E - shared error type
      Parameters:
      r1 - first result; must not be null
      r2 - second result; must not be null
      r3 - third result; must not be null
      r4 - fourth result; must not be null
      combiner - function applied to the four values; must not be null
      Returns:
      Ok(combiner(v1,v2,v3,v4)) if all four are Ok, otherwise the first Err
      Throws:
      NullPointerException - if any argument is null
    • getOrElse

      default Value getOrElse(Value fallback)
      Returns the success value if present, or fallback otherwise.
      Parameters:
      fallback - the non-null fallback value
      Returns:
      the success value, or fallback if this is the error variant
      Throws:
      NullPointerException - if fallback is null
    • getOrElseGet

      default Value getOrElseGet(Supplier<Value> supplier)
      Returns the success value if present, or the value produced by supplier otherwise.
      Parameters:
      supplier - supplier of the fallback value; must not return null
      Returns:
      the success value, or the value produced by supplier
      Throws:
      NullPointerException - if supplier is null or returns null
    • getOrNull

      default Value getOrNull()
      Returns the success value if present, or null otherwise.
      Returns:
      the success value or null
    • getOrThrow

      default Value getOrThrow(Function<Error, ? extends RuntimeException> exMapper)
      Returns the success value if present, or throws an exception mapped from the error.
      Parameters:
      exMapper - maps the error to a RuntimeException; must not return null
      Returns:
      the success value
      Throws:
      NullPointerException - if exMapper is null or returns null
      RuntimeException - the exception produced by exMapper on the error variant
    • stream

      default Stream<Value> stream()
      Returns a single-element stream of the success value, or an empty stream on error.
      Returns:
      a stream of the value or empty
    • fold

      default <R> R fold(Function<Value,R> onSuccess, Function<Error,R> onError)
      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 return null
      onError - applied to the error value; must not return null
      Returns:
      the result of the applied function
      Throws:
      NullPointerException - if either function is null or returns null
    • match

      default void match(Consumer<Value> onSuccess, Consumer<Error> onError)
      Executes one of the two consumers based on the active variant.
      Parameters:
      onSuccess - called with the success value
      onError - called with the error value
      Throws:
      NullPointerException - if either consumer is null
    • toOption

      default Option<Value> toOption()
      Converts this container to an Option. The success variant maps to Option.Some; the error variant maps to Option.none().
      Returns:
      an Option containing the success value, or empty