Interface Either<L,R>

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

public sealed interface Either<L,R> permits Either.Left<L,R>, Either.Right<L,R>
Minimal functional Either implementation.

It does not have left or right projections, map/flatMap/filter operations work only on right side of Either and do not have effect for left side. Anyway you can use swap() for this purpose.

Either intentionally does not have any getX() methods, use fold(Function, Function), optional() or stream() instead.

Either has a set of catching(CheckedConsumer) higher order functions which will be useful for exception handling.

       var socket = catching(() -> new Socket("foo", 1234));
       try (var c = socket.asCloseable()) {
           socket.flatMap(catching(Socket::getOutputStream))
           .flatMap(catching(o -> {
            o.write(new byte[] {1,2,3});
        }));
    }
 }));
 
  • Method Details

    • isLeft

      default boolean isLeft()
      Checks if this is left side.
      Returns:
      true is this is left side of Either.
    • isRight

      default boolean isRight()
      Checks if this is right side.
      Returns:
      true is this is right side of Either.
    • stream

      default Stream<R> stream()
      Converts Either to Stream
      Returns:
      one element stream for right, empty stream for left.
    • optional

      default Optional<R> optional()
      Converts Either to Optional
      Returns:
      Optional for right, Optional empty for left.
    • map

      default <T> Either<L,T> map(Function<? super R,? extends T> mapper)
      Maps right side of the Either.
      Type Parameters:
      T - new right type
      Parameters:
      mapper - mapper function that maps R type to T type
      Returns:
      new Either
    • flatMap

      default <T> Either<L,T> flatMap(Function<? super R,? extends Either<? extends L,? extends T>> mapper)
      Flat maps right side of the Either.
      Type Parameters:
      T - new right type
      Parameters:
      mapper - R to Either<L,T>
      Returns:
      new Either<L,T>
    • filter

      default Either<L,R> filter(Predicate<? super R> predicate, Function<? super R,? extends L> leftMapper)
      Filters right side of Either, does not have effect if it is left side.
      Parameters:
      predicate - condition to test
      leftMapper - will map R to L if predicate return false
      Returns:
      Either
    • filter

      default Either<L,Optional<R>> filter(Predicate<? super R> predicate)
    • swap

      default Either<R,L> swap()
      Swaps Either
      Returns:
      swapped Either
    • fold

      <T> T fold(Function<? super L,? extends T> leftMapper, Function<? super R,? extends T> rightMapper)
      Folds Either to type T
      Type Parameters:
      T - folded type
      Parameters:
      leftMapper - maps left to T
      rightMapper - maps right to T
      Returns:
      value of T type
    • accept

      default Either<L,R> accept(Consumer<? super L> leftConsumer, Consumer<? super R> rightConsumer)
      Produces side effects for Either
      Parameters:
      leftConsumer - left side effect
      rightConsumer - right side effect
      Returns:
      this Either
    • right

      static <L, R> Either<L,R> right(R right)
      Factory method to produce right side of Either from R value
      Type Parameters:
      R - type of right side
      L - type of left side
      Parameters:
      right - right value
      Returns:
      Either.Right
    • left

      static <L, R> Either<L,R> left(L left)
      Factory method to produce left side of Either from L value
      Type Parameters:
      R - type of right side
      L - type of left side
      Parameters:
      left - left value
      Returns:
      Either.Left
    • catching

      static <R> Either<Exception,R> catching(Try.CheckedSupplier<? extends R> supplier)
      Factory method to produce Either<Exception,R> from throwing supplier
      Type Parameters:
      R - right side type
      Parameters:
      supplier - throwing supplier of R
      Returns:
      Either
    • catching

      static <R, T> Function<R,Either<Exception,T>> catching(Try.CheckedFunction<? super R,? extends T> mapper)
      Lifts throwing R->T function to total R->Either<Exception,R> function. May be useful with flatMap.
      Type Parameters:
      R - right side type
      T - left side type
      Parameters:
      mapper - throwing function
      Returns:
      lifted function
    • catching

      static <R> Function<R,Either<Exception,R>> catching(Try.CheckedConsumer<? super R> consumer)
      Lifts throwing consumer to total R->Either<Exception,R> function.
      Type Parameters:
      R - right side type
      Parameters:
      consumer -
      Returns:
      lifted function
    • asCloseable

      default Closeable asCloseable()
      Converts this Either to Closeable which is handy to use in try-with-resources block.
      Returns:
      Closeable