Class Err<T,E>

java.lang.Object
org.storynode.pigeon.result.Result<T,E>
org.storynode.pigeon.result.Err<T,E>
All Implemented Interfaces:
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.
      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.
      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
    • 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)
    • orElse

      public T orElse(T defaultValue)
      Description copied from class: Result
      Unwraps the contained value, or returns a default one if this contains an error
      Specified by:
      orElse in class Result<T,E>
      Parameters:
      defaultValue - The value to return in place of the error
      Returns:
      The contained value or the default one, depending on which is appropriate
    • and

      public <U> Result<U,E> and(Result<U,E> res)
      Returns res if the result is Ok, otherwise returns the Err value of this.
      Arguments passed to and are eagerly evaluated; if you are passing the result of a function call, it is recommended to use Result.andThen(Function), which is lazily evaluated.
      Specified by:
      and in class Result<T,E>
      Type Parameters:
      U - The type of the other result (if Ok)
      Parameters:
      res - The other result
      Returns:
      res if this is Ok, this error otherwise
      See Also:
    • andThen

      public <U> Result<U,E> andThen(Function<T,Result<U,E>> res)
      Like Result.and(Result) but lazily evaluated. The function is supplied the current value of the result if it's Ok
      Specified by:
      andThen in class Result<T,E>
      Type Parameters:
      U - The type of the other result (if Ok)
      Parameters:
      res - The other result
      Returns:
      res if this is Ok, this error otherwise
      See Also: