Class Err<V,E>

java.lang.Object
aQute.bnd.service.result.Err<V,E>
Type Parameters:
V - The value type
E - The error type
All Implemented Interfaces:
Result<V,E>

public final class Err<V,E> extends Object implements Result<V,E>
This class represents the Err side of @{link Result}.
  • Field Details

    • error

      private final E error
  • Constructor Details

    • Err

      Err(E error)
  • Method Details

    • isOk

      public boolean isOk()
      Returns true if this instance represents an Ok value, false otherwise.
      Specified by:
      isOk in interface Result<V,E>
      Returns:
      true if this instance represents an Ok value, false otherwise.
    • isErr

      public boolean isErr()
      Returns true if this instance represents an Err value, false otherwise.
      Specified by:
      isErr in interface Result<V,E>
      Returns:
      true if this instance represents an Err value, false otherwise.
    • value

      public Optional<V> value()
      Returns the value of this instance as an Optional. Returns Optional.empty() if this is an Err instance.
      Specified by:
      value in interface Result<V,E>
      Returns:
      The value of this instance as an Optional. Returns Optional.empty() if this is an Err instance.
    • error

      public Optional<E> error()
      Returns the error of this instance as an Optional. Returns Optional.empty() if this is an Ok instance.
      Specified by:
      error in interface Result<V,E>
      Returns:
      The error of this instance as an Optional. Returns Optional.empty() if this is an Ok instance.
    • unwrap

      public V unwrap()
      Returns the contained value if this is an Ok value. Otherwise throws a ResultException.
      Specified by:
      unwrap in interface Result<V,E>
      Returns:
      The contained value
    • unwrap

      public V unwrap(String message) throws ResultException
      Express the expectation that this object is an Ok value. Otherwise throws a ResultException with the specified message.
      Specified by:
      unwrap in interface Result<V,E>
      Parameters:
      message - The message to pass to a potential ResultException.
      Throws:
      ResultException - If this is an Err instance.
    • orElse

      public V orElse(V orElse)
      Returns the contained value if this is an Ok value. Otherwise returns the specified alternate value.
      Specified by:
      orElse in interface Result<V,E>
      Parameters:
      orElse - The value to return if this is an Err instance.
      Returns:
      The contained value or the alternate value
    • orElseGet

      public V orElseGet(Supplier<? extends V> orElseSupplier)
      Returns the contained value if this is an Ok value. Otherwise returns the alternate value supplied by the specified supplier.
      Specified by:
      orElseGet in interface Result<V,E>
      Parameters:
      orElseSupplier - The supplier to supply an alternate value if this is an Err instance. Must not be null.
      Returns:
      The contained value or the alternate value
    • orElseThrow

      public <R extends Throwable> V orElseThrow(FunctionWithException<? super E,? extends R> throwableSupplier) throws R
      Returns the contained value if this is an Ok value. Otherwise throws the exception supplied by the specified function.
      Specified by:
      orElseThrow in interface Result<V,E>
      Type Parameters:
      R - The exception type.
      Parameters:
      throwableSupplier - The supplier to supply an exception if this is an Err instance. Must not be null. The supplier must return a non-null result.
      Returns:
      The contained value.
      Throws:
      R - If this is an Err instance.
    • coerce

      private <U> Result<U,E> coerce()
    • map

      public <U> Result<U,E> map(FunctionWithException<? super V,? extends U> mapper)
      Map the contained value if this is an Ok value. Otherwise return this.
      Specified by:
      map in interface Result<V,E>
      Type Parameters:
      U - The new value type.
      Parameters:
      mapper - The function to map the contained value into a new value. Must not be null. The function must return a non-null value.
      Returns:
      A result containing the mapped value if this is an Ok value. Otherwise this.
    • mapErr

      public <F> Result<V,F> mapErr(FunctionWithException<? super E,? extends F> mapper)
      Map the contained error if this is an Err value. Otherwise return this.
      Specified by:
      mapErr in interface Result<V,E>
      Type Parameters:
      F - The new error type.
      Parameters:
      mapper - The function to map the contained error into a new error. Must not be null. The function must return a non-null error.
      Returns:
      A result containing the mapped error if this is an Err value. Otherwise this.
    • flatMap

      public <U> Result<U,E> flatMap(FunctionWithException<? super V,? extends Result<? extends U,? extends E>> mapper)
      FlatMap the contained value if this is an Ok value. Otherwise return this.
      Specified by:
      flatMap in interface Result<V,E>
      Type Parameters:
      U - The new value type.
      Parameters:
      mapper - The function to flatmap the contained value into a new result. Must not be null. The function must return a non-null result.
      Returns:
      The flatmapped result if this is an Ok value. Otherwise this.
    • recover

      public Result<V,E> recover(FunctionWithException<? super E,? extends V> recover)
      Recover the contained error if this is an Err value. Otherwise return this.
      Specified by:
      recover in interface Result<V,E>
      Parameters:
      recover - The function to recover the contained error into a new value. Must not be null. The function must return a non-null value.
      Returns:
      A result containing the new value if this is an Err value. Otherwise this.
    • accept

      public void accept(ConsumerWithException<? super V> ok, ConsumerWithException<? super E> err)
      Description copied from interface: Result
      Terminal function that processes the result or the error
      Specified by:
      accept in interface Result<V,E>
      Parameters:
      ok - the consumer called when ok
      err - the consumer called when not ok
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • asError

      public <X> Result<X,E> asError()
      Specified by:
      asError in interface Result<V,E>
    • unwrap

      public <X extends Throwable> V unwrap(Function<E,X> constructor) throws X
      Specified by:
      unwrap in interface Result<V,E>
      Throws:
      X extends Throwable