Package e.java

Interface EOr<A>

Type Parameters:
A - Type of the value this EOr can contain
All Known Implementing Classes:
EOr.Failure, EOr.Success

public interface EOr<A>
A container that can either be a Failure containing an E or Success containing a value
See Also:
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Interface
    Description
    static final class 
    A failed EOr
    static final class 
    A successful EOr
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final E
    A default E to be used when condition does not hold while filtering an EOr
    static final EOr<Void>
    A successful EOr of type Unit
  • Method Summary

    Modifier and Type
    Method
    Description
    default <B> EOr<B>
    andThen(Supplier<EOr<B>> next)
    Provides a next EOr if this one has a value, ignoring the value
    static <A> EOr<A>
    Constructs an EOr from a computation that can throw
    default Optional<E>
     
    default EOr<A>
    filter(Function<A,Boolean> condition)
    Filters this EOr by value in it, if it exists
    default EOr<A>
    filter(Function<A,Boolean> condition, Function<A,E> filteredError)
    Filters this EOr by value in it, if it exists, using given function
    default <B> EOr<B>
    Computes a new EOr using value in this, if it exists, with given flat mapping function
    default EOr<A>
    Computes a new EOr using E in this, if it exists, with given flat mapping function
    default <B> B
    fold(Function<E,B> ifFailure, Function<A,B> ifSuccess)
    Folds this into a single value, handling both E and value conversions with given functions
    default EOr<A>
    Alias of `onValue`
    static <A> EOr<A>
    from(A a)
    Constructs a successful EOr containing given value
    static <A> EOr<A>
    from(E e)
    Constructs a failed EOr containing given E
    static <A> EOr<A>
    fromNullable(A a, Supplier<E> ifNull)
    Constructs an EOr from a nullable value
    static <A> EOr<A>
    fromOptional(Optional<A> a, Supplier<E> ifEmpty)
    Constructs an EOr from an Optional value
    default A
    getOrElse(Supplier<A> alternative)
    Gets the value in this or falls back to given default value
    default boolean
     
    default boolean
     
    default <B> EOr<B>
    map(Function<A,B> f)
    Converts value in this, if it exists, using given mapping function to make a new EOr
    default EOr<A>
    Converts E in this, if it exists, using given mapping function to make a new EOr
    default EOr<A>
    Performs a side-effect using error in this, if it exists
    default EOr<A>
    Performs a side-effect using value in this, if it exists
    default EOr<A>
    orElse(Supplier<EOr<A>> alternative)
    Provides an alternative EOr if this one has E, ignoring the E
    default Optional<A>
     
  • Field Details

  • Method Details

    • hasError

      default boolean hasError()
      Returns:
      Whether or not this contains an E
    • hasValue

      default boolean hasValue()
      Returns:
      Whether or not this contains a value
    • error

      default Optional<E> error()
      Returns:
      E in this as an Optional
    • value

      default Optional<A> value()
      Returns:
      Value in this as an Optional
    • map

      default <B> EOr<B> map(Function<A,B> f)
      Converts value in this, if it exists, using given mapping function to make a new EOr
      Type Parameters:
      B - Type of the new value
      Parameters:
      f - Mapping function
      Returns:
      A new EOr containing either the new value or E in this one
    • flatMap

      default <B> EOr<B> flatMap(Function<A,EOr<B>> f)
      Computes a new EOr using value in this, if it exists, with given flat mapping function
      Type Parameters:
      B - Type of the new value
      Parameters:
      f - Flat mapping function
      Returns:
      Computed EOr or a new EOr containing E in this one
    • mapError

      default EOr<A> mapError(Function<E,E> f)
      Converts E in this, if it exists, using given mapping function to make a new EOr
      Parameters:
      f - E mapping function
      Returns:
      This EOr or a new EOr containing computed E if this one has E
    • flatMapError

      default EOr<A> flatMapError(Function<E,EOr<A>> f)
      Computes a new EOr using E in this, if it exists, with given flat mapping function
      Parameters:
      f - E flat mapping function
      Returns:
      This EOr or a computed EOr if this one has E
    • fold

      default <B> B fold(Function<E,B> ifFailure, Function<A,B> ifSuccess)
      Folds this into a single value, handling both E and value conversions with given functions
      Type Parameters:
      B - Type of the desired result
      Parameters:
      ifFailure - Conversion function for E
      ifSuccess - Conversion function for value
      Returns:
      Converted result
    • getOrElse

      default A getOrElse(Supplier<A> alternative)
      Gets the value in this or falls back to given default value
      Parameters:
      alternative - Default value to use in case this has E
      Returns:
      Value in this or given default value
    • orElse

      default EOr<A> orElse(Supplier<EOr<A>> alternative)
      Provides an alternative EOr if this one has E, ignoring the E
      Parameters:
      alternative - Alternative EOr in case this one has E
      Returns:
      This EOr or alternative if this one has E
    • andThen

      default <B> EOr<B> andThen(Supplier<EOr<B>> next)
      Provides a next EOr if this one has a value, ignoring the value
      Type Parameters:
      B - Type of value in next EOr
      Parameters:
      next - Next EOr in case this one has a value
      Returns:
      Next EOr or a new EOr containing E in this one
    • onError

      default EOr<A> onError(Consumer<E> f)
      Performs a side-effect using error in this, if it exists
      Parameters:
      f - Side-effecting function
      Returns:
      This EOr for chaining
    • onValue

      default EOr<A> onValue(Consumer<A> f)
      Performs a side-effect using value in this, if it exists
      Parameters:
      f - Side-effecting function
      Returns:
      This EOr for chaining
    • forEach

      default EOr<A> forEach(Consumer<A> f)
      Alias of `onValue`
      Returns:
      This EOr for chaining
      See Also:
    • filter

      default EOr<A> filter(Function<A,Boolean> condition, Function<A,E> filteredError)
      Filters this EOr by value in it, if it exists, using given function
      Parameters:
      condition - Filtering function
      filteredError - E conversion function
      Returns:
      This EOr of a new EOr containing an E computed by given conversion function
    • filter

      default EOr<A> filter(Function<A,Boolean> condition)
      Filters this EOr by value in it, if it exists
      Parameters:
      condition - Filtering function
      Returns:
      This EOr of a new EOr containing an E
      See Also:
    • from

      static <A> EOr<A> from(E e)
      Constructs a failed EOr containing given E
      Type Parameters:
      A - Type of value of resulting EOr
      Parameters:
      e - An E
      Returns:
      A new failed EOr containing given E
    • from

      static <A> EOr<A> from(A a)
      Constructs a successful EOr containing given value
      Type Parameters:
      A - Type of value of resulting EOr
      Parameters:
      a - A value
      Returns:
      A new failed EOr containing given value
    • fromNullable

      static <A> EOr<A> fromNullable(A a, Supplier<E> ifNull)
      Constructs an EOr from a nullable value
      Type Parameters:
      A - Type of value
      Parameters:
      a - A nullable value
      ifNull - An error to use in case value is null
      Returns:
      An EOr containing either value or given E
    • fromOptional

      static <A> EOr<A> fromOptional(Optional<A> a, Supplier<E> ifEmpty)
      Constructs an EOr from an Optional value
      Type Parameters:
      A - Type of value
      Parameters:
      a - An Optional value
      ifEmpty - An error to use in case value is empty
      Returns:
      An EOr containing either value or given E
    • catching

      static <A> EOr<A> catching(UnsafeSupplier<A> f, Function<Throwable,E> ifFailure)
      Constructs an EOr from a computation that can throw
      Type Parameters:
      A - Type of value
      Parameters:
      f - A computation that can throw
      ifFailure - An E conversion function
      Returns:
      An EOr containing either computed value or an E computed by given function