java.lang.Object
com.github.nullterminated.trylambda.Either<A,B>
Type Parameters:
A - the left value type
B - the right value type
All Implemented Interfaces:
Serializable
Direct Known Subclasses:
Either.Left, Either.Right

public abstract class Either<A,B> extends Object implements Serializable
A Java implementation of the Either monad. The Either type represents values with two possibilities: a value of type Either a b is either Left a or Right b. The Either type is sometimes used to represent a value which is either correct or an error; by convention, the Left constructor is used to hold an error value and the Right constructor is used to hold a correct value (mnemonic: "right" also means "correct"). Pattern matching is accomplished using polymorphism.
Author:
Ramsey Gurley
See Also:
  • Method Details

    • left

      public static <A, B> Either<A,B> left(A left)
      Factory method for constructing lefts.
      Type Parameters:
      A - the left type
      B - the right type
      Parameters:
      left - the left value
      Returns:
      a new left
    • right

      public static <A, B> Either<A,B> right(B right)
      Factory method for constructing rights.
      Type Parameters:
      A - the left type
      B - the right type
      Parameters:
      right - the right value
      Returns:
      a new right
    • isLeft

      public final boolean isLeft()
      Returns:
      true if left
    • isRight

      public final boolean isRight()
      Returns:
      true if right
    • getLeft

      public abstract A getLeft()
      Returns:
      the left value
      Throws:
      UnsupportedOperationException - if the receiver is right
    • getRight

      public abstract B getRight()
      Returns:
      the right value
      Throws:
      UnsupportedOperationException - if the receiver is left
    • use

      public abstract void use(Consumer<A> leftConsumer, Consumer<B> rightConsumer)
      Pass the value of this either to a consumer.
      Parameters:
      leftConsumer - the consumer for lefts
      rightConsumer - the consumer for rights
    • map

      public abstract <X, Y> Either<X,Y> map(Function<A,X> leftFunction, Function<B,Y> rightFunction)
      Map this Either<A,B> to a new Either<X,Y>.
      Type Parameters:
      X - the new left type
      Y - the new right type
      Parameters:
      leftFunction - function to convert A to X
      rightFunction - function to convert B to Y
      Returns:
      a new Either<X,Y>
    • reduce

      public abstract <T> T reduce(Function<A,T> leftFunction, Function<B,T> rightFunction)
      Reduce an Either<A,B> to a single value type T.
      Type Parameters:
      T - the result type
      Parameters:
      leftFunction - function to convert A to T
      rightFunction - function to convert B to T
      Returns:
      a value typed T
    • flip

      public abstract Either<B,A> flip()
      Flip an Either<A,B> to a Either<B,A>.
      Returns:
      a new either with types flipped