Class Err<V>

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

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

    • error

      private final String error
  • Constructor Details

  • Method Details

    • isOk

      public boolean isOk()
      Returns true if this instance represents an Ok value, false otherwise.
      Specified by:
      isOk in interface Result<V>
      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>
      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>
      Returns:
      The value of this instance as an Optional. Returns Optional.empty() if this is an Err instance.
    • error

      public Optional<String> 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>
      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>
      Returns:
      The contained value
    • unwrap

      public V unwrap(CharSequence 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>
      Parameters:
      message - The message to pass to a potential ResultException. Must not be null.
      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>
      Parameters:
      orElse - The value to return if this is an Err instance.
      Returns:
      The contained value or the alternate value
    • orElseGet

      public V orElseGet(SupplierWithException<? 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>
      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 String,? 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>
      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 - The exception returned by the throwableSupplier if this is an Err instance.
    • map

      public <U> Result<U> 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>
      Type Parameters:
      U - The new value type.
      Parameters:
      mapper - The function to map the contained value into a new value. Must not be null.
      Returns:
      A result containing the mapped value if this is an Ok value. Otherwise this.
    • mapErr

      public Result<V> mapErr(FunctionWithException<? super String,? extends CharSequence> mapper)
      Map the contained error if this is an Err value. Otherwise return this.
      Specified by:
      mapErr in interface Result<V>
      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> flatMap(FunctionWithException<? super V,? extends Result<? extends U>> mapper)
      FlatMap the contained value if this is an Ok value. Otherwise return this.
      Specified by:
      flatMap in interface Result<V>
      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> recover(FunctionWithException<? super String,? extends V> recover)
      Recover the contained error if this is an Err value. Otherwise return this.

      To recover with a recovery value of null, the Result.recoverWith(FunctionWithException) method must be used. The specified function for Result.recoverWith(FunctionWithException) can return Result.ok(null) to supply the desired null value.

      Specified by:
      recover in interface Result<V>
      Parameters:
      recover - The function to recover the contained error into a new value. Must not be null.
      Returns:
      A result containing the new non-null value if this is an Err value. Otherwise this if this is an Ok value or the recover function returned null.
    • recoverWith

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

      public void accept(ConsumerWithException<? super V> ok, ConsumerWithException<? super String> err)
      Processes the result.
      Specified by:
      accept in interface Result<V>
      Parameters:
      ok - The consumer called this result is an Ok value. Must not be null.
      err - The consumer called when this result is an Err value. Must not be null.
    • asError

      public <U> Result<U> asError()
      If an Err, return this coerced to the desired generic type.
      Specified by:
      asError in interface Result<V>
      Type Parameters:
      U - The desired generic type of the Err.
      Returns:
      this
    • toString

      public String toString()
      Overrides:
      toString in class Object