Interface Either<L,R>

Type Parameters:
L - the left type
R - the right type
All Known Implementing Classes:
Either.Left, Either.Right

@NullMarked @AsOf("3.1.0") public sealed interface Either<L,R> permits Either.Left<L,R>, Either.Right<L,R>
Either one type or the other.
Since:
3.1.0
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Interface
    Description
    static final record 
    The left value.
    static final record 
    The right value.
  • Method Summary

    Modifier and Type
    Method
    Description
    default Either<L,R>
    consumeLeft(Consumer<? super L> consumer)
    Consumes the left value if present, returning this either.
    default Either<L,R>
    consumeRight(Consumer<? super R> consumer)
    Consumes the right value if present, returning this either.
    default boolean
    Checks if this either is empty.
    default <U> U
    fold(Function<? super L, ? extends U> ifLeft, Function<? super R, ? extends U> ifRight)
    Collapses both arms into a single result.
    default Optional<L>
    Gets the left value if this is a left.
    static <L,R> Either<L,R>
    left(L value)
    Creates a left value.
    default Either<L,R>
    leftOrElse(L newValue)
    Left or else.
    default Either<L,R>
    leftOverride(L value)
    Returns a left value holding value, discarding any right value this either currently holds.
    default Optional<R>
    Gets the right value if this is a right.
    static <L,R> Either<L,R>
    right(R value)
    Creates a right value.
    default Either<L,R>
    rightOrElse(R newValue)
    Right or else.
    default Either<L,R>
    Returns a right value holding value, discarding any left value this either currently holds.
  • Method Details

    • left

      @AsOf("3.1.0") static <L,R> Either<L,R> left(L value)
      Creates a left value.
      Type Parameters:
      L - the type of the value
      R - the type of the value
      Parameters:
      value - the value to wrap
      Returns:
      a left value
      Since:
      3.1.0
    • right

      @AsOf("3.1.0") static <L,R> Either<L,R> right(R value)
      Creates a right value.
      Type Parameters:
      L - the type of the value
      R - the type of the value
      Parameters:
      value - the value to wrap
      Returns:
      a right value
      Since:
      3.1.0
    • fold

      @AsOf("3.1.0") default <U> U fold(Function<? super L, ? extends U> ifLeft, Function<? super R, ? extends U> ifRight)
      Collapses both arms into a single result.
      Type Parameters:
      U - the result type
      Parameters:
      ifLeft - mapping applied when this is a left value
      ifRight - mapping applied when this is a right value
      Returns:
      the mapped result
      Since:
      3.1.0
    • left

      @AsOf("3.1.0") default Optional<L> left()
      Gets the left value if this is a left.
      Returns:
      the left value, if present
      Since:
      3.1.0
    • right

      @AsOf("3.1.0") default Optional<R> right()
      Gets the right value if this is a right.
      Returns:
      the right value, if present
      Since:
      3.1.0
    • leftOverride

      @AsOf("3.1.0") default Either<L,R> leftOverride(L value)
      Returns a left value holding value, discarding any right value this either currently holds.

      Useful when an Either backs a mutable field (e.g. a builder): a "left" setter can force the value onto its left arm regardless of which arm it currently holds. The caller supplies the already-merged left value, since Either cannot know how to combine two lefts.

      Parameters:
      value - the new left value
      Returns:
      a left value holding value
      Since:
      3.1.0
    • rightOverride

      @AsOf("3.1.0") default Either<L,R> rightOverride(R value)
      Returns a right value holding value, discarding any left value this either currently holds.
      Parameters:
      value - the new right value
      Returns:
      a right value holding value
      Since:
      3.1.0
      See Also:
    • leftOrElse

      @AsOf("3.1.0") default Either<L,R> leftOrElse(L newValue)
      Left or else.
      Parameters:
      newValue - the value to use if this is a right
      Returns:
      this either if it is a left, or a new left with the given value if it is a right
      Since:
      3.1.0
    • rightOrElse

      @AsOf("3.1.0") default Either<L,R> rightOrElse(R newValue)
      Right or else.
      Parameters:
      newValue - the value to use if this is a left
      Returns:
      this either if it is a right, or a new right with the given value if it is a left
      Since:
      3.1.0
    • consumeLeft

      @AsOf("3.1.0") default Either<L,R> consumeLeft(Consumer<? super L> consumer)
      Consumes the left value if present, returning this either.
      Parameters:
      consumer - the consumer to apply to the left value
      Returns:
      this either
      Since:
      3.1.0
    • consumeRight

      @AsOf("3.1.0") default Either<L,R> consumeRight(Consumer<? super R> consumer)
      Consumes the right value if present, returning this either.
      Parameters:
      consumer - the consumer to apply to the right value
      Returns:
      this either
      Since:
      3.1.0
    • empty

      @AsOf("3.1.0") default boolean empty()
      Checks if this either is empty.
      Returns:
      true if this either is empty
      Since:
      3.1.0