Interface Either<L,R>

All Known Implementing Classes:
Either.Left, Either.Right

public interface Either<L,R>
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Interface
    Description
    static final class 
    The Left version of an Either.
    static final class 
    The Right version of an Either.
  • Method Summary

    Modifier and Type
    Method
    Description
    default <U> U
    fold(Function<? super L,? extends U> leftMapper, Function<? super R,? extends U> rightMapper)
    Folds either the left or the right side of this disjunction.
    get()
    Gets the right value if this is a Right or throws if this is a Left.
    Returns the left value.
    default R
     
    default boolean
     
    boolean
    Returns whether this Either is a Left.
    boolean
    Returns whether this Either is a Right.
    static <L, R> Either<L,R>
    left(L left)
    Constructs a Either.Left
    default <U> Either<L,U>
    map(Function<? super R,? extends U> mapper)
    Maps the left value if the projected Either is a Left.
    default <U> Either<U,R>
    mapLeft(Function<? super L,? extends U> leftMapper)
    Maps the value of this Either if it is a Left, performs no operation if this is a Right.
    static <L, R> Either<L,R>
    right(R right)
    Constructs a Either.Right
    default Either<R,L>
    Converts a Left to a Right vice versa by wrapping the value in a new type.
  • Method Details

    • right

      static <L, R> Either<L,R> right(R right)
      Constructs a Either.Right
      Type Parameters:
      L - Type of left value.
      R - Type of right value.
      Parameters:
      right - The value.
      Returns:
      A new Right instance.
    • left

      static <L, R> Either<L,R> left(L left)
      Constructs a Either.Left
      Type Parameters:
      L - Type of left value.
      R - Type of right value.
      Parameters:
      left - The value.
      Returns:
      A new Left instance.
    • getLeft

      L getLeft()
      Returns the left value.
      Returns:
      The left value.
      Throws:
      NoSuchElementException - if this is a Right.
    • isLeft

      boolean isLeft()
      Returns whether this Either is a Left.
      Returns:
      true, if this is a Left, false otherwise
    • isRight

      boolean isRight()
      Returns whether this Either is a Right.
      Returns:
      true, if this is a Right, false otherwise
    • get

      R get()
      Gets the right value if this is a Right or throws if this is a Left.
      Returns:
      the right value
      Throws:
      NoSuchElementException - if this is a Left.
    • swap

      default Either<R,L> swap()
      Converts a Left to a Right vice versa by wrapping the value in a new type.
      Returns:
      a new Either
    • isEmpty

      default boolean isEmpty()
    • getOrNull

      default R getOrNull()
    • mapLeft

      default <U> Either<U,R> mapLeft(Function<? super L,? extends U> leftMapper)
      Maps the value of this Either if it is a Left, performs no operation if this is a Right.
      Type Parameters:
      U - Component type of the mapped right value
      Parameters:
      leftMapper - A mapper
      Returns:
      a mapped Monad
      Throws:
      NullPointerException - if mapper is null
    • map

      default <U> Either<L,U> map(Function<? super R,? extends U> mapper)
      Maps the left value if the projected Either is a Left.
      Type Parameters:
      U - The new type of a Left value
      Parameters:
      mapper - A mapper which takes a left value and returns a value of type U
      Returns:
      A new LeftProjection
    • fold

      default <U> U fold(Function<? super L,? extends U> leftMapper, Function<? super R,? extends U> rightMapper)
      Folds either the left or the right side of this disjunction.
      Type Parameters:
      U - type of the folded value
      Parameters:
      leftMapper - maps the left value if this is a Left
      rightMapper - maps the right value if this is a Right
      Returns:
      A value of type U