Class Err<T,E>

java.lang.Object
org.storynode.pigeon.result.Result<T,E>
org.storynode.pigeon.result.Err<T,E>
All Implemented Interfaces:
SafelyWrapped<T>, Wrapped<T>

public class Err<T,E> extends Result<T,E>
Result variant for errors.
Author:
Andrea Coronese
See Also:
  • Constructor Details

    • Err

      public Err(@NotNull E error)
      A variant of Result that indicates an error value
      Parameters:
      error - The value for the 'error' result
  • Method Details

    • isOk

      public boolean isOk()
      Whether this Result is ok, meaning it contains a value and not an error
      Specified by:
      isOk in class Result<T,E>
      Returns:
      true if this contains an ok value, false if it contains an error
    • unwrap

      public T unwrap() throws UnwrapException
      Unwraps and return the inner value, if present. Throws an error if this result contains an error. If you need a non throwing version of this method use Result.tryUnwrap()
      Specified by:
      unwrap in interface Wrapped<T>
      Specified by:
      unwrap in class Result<T,E>
      Returns:
      The inner value
      Throws:
      UnwrapException - if this contains an error
    • unwrapError

      public E unwrapError()
      Unwraps and return the inner error, if present. Throws an error if this result contains a value. If you need a non throwing version of this method use [#tryUnwrapError()]
      Specified by:
      unwrapError in class Result<T,E>
      Returns:
      The inner error
    • orElseGet

      public T orElseGet(@NotNull @NotNull Supplier<T> defaultValueSupplier)
      Unwraps the contained value, or returns a default one if this contains an error
      Specified by:
      orElseGet in class Result<T,E>
      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
    • tryUnwrap

      @NotNull public @NotNull Option<T> tryUnwrap()
      The non-throwing variant of Wrapped.unwrap(). This is guaranteed to never throw and to always return a non-null value.

      The non-throwing variant of Wrapped.unwrap(). This is guaranteed to never throw and to always return a non-null value.

      Specified by:
      tryUnwrap in interface SafelyWrapped<T>
      Specified by:
      tryUnwrap in class Result<T,E>
      Returns:
      An Option containing the value, or empty if there is none or if an error would be raised while unwrapping said value.
    • tryUnwrapError

      @NotNull public @NotNull Option<E> tryUnwrapError()
      The non-throwing variant of Result.unwrapError(). This is guaranteed to never throw and to always return a non-null value.
      Specified by:
      tryUnwrapError in class Result<T,E>
      Returns:
      An Option containing the error, or empty if there is none
    • map

      public <U> Result<U,E> map(@NotNull @NotNull Function<? super T,? extends U> fn)
      Maps a Result<T, E> to Result<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
       
      Specified by:
      map in class Result<T,E>
      Type Parameters:
      U - The type of the new value
      Parameters:
      fn - The function to apply to the value
      Returns:
      The mapped Result
    • flatMap

      public <U> Result<U,E> flatMap(@NotNull @NotNull Function<? super T,? extends Result<U,E>> fn)
      Like Option.map(Function) but does not re-wrap the result of the provided mapping function to a Result
      Specified by:
      flatMap in class Result<T,E>
      Type Parameters:
      U - The type of the mapped Ok value
      Parameters:
      fn - The mapping function to apply
      Returns:
      The mapped value
    • mapError

      public <U> Result<T,U> mapError(@NotNull @NotNull Function<? super E,? extends U> fn)
      Maps a Result<T, E> to Result<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
      Specified by:
      mapError in class Result<T,E>
      Type Parameters:
      U - The type of the new error
      Parameters:
      fn - The function to apply to the error
      Returns:
      The mapped Result
    • ifOkOrElse

      public Result<T,E> ifOkOrElse(Consumer<T> whenOk, @NotNull @NotNull Consumer<E> whenError)
      Executes whenOk if this contains a value, whenError otherwise.
      Specified by:
      ifOkOrElse in class Result<T,E>
      Parameters:
      whenOk - The function to execute when this contains a value
      whenError - The function to execute when this contains an error
      Returns:
      this instance (for chaining)