Class Either<A,​B>

  • Direct Known Subclasses:
    Either.Left, Either.Right

    public abstract class Either<A,​B>
    extends java.lang.Object
    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
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  Either.Left<A,​B>  
      static class  Either.Right<A,​B>  
    • Method Summary

      All Methods Static Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      abstract Either<B,​A> flip()
      Flip an Either<A,B> to a Either<B,A>.
      abstract A getLeft()  
      abstract B getRight()  
      boolean isLeft()  
      boolean isRight()  
      static <A,​B>
      Either<A,​B>
      left​(A left)
      Factory method for constructing lefts.
      abstract <X,​Y>
      Either<X,​Y>
      map​(java.util.function.Function<A,​X> leftFunction, java.util.function.Function<B,​Y> rightFunction)
      Map this Either<A,B> to a new Either<X,Y>.
      abstract <T> T reduce​(java.util.function.Function<A,​T> leftFunction, java.util.function.Function<B,​T> rightFunction)
      Reduce an Either<A,B> to a single value type T.
      static <A,​B>
      Either<A,​B>
      right​(B right)
      Factory method for constructing rights.
      abstract void use​(java.util.function.Consumer<A> leftConsumer, java.util.function.Consumer<B> rightConsumer)
      Pass the value of this either to a consumer.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • 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:
        java.lang.UnsupportedOperationException - if the receiver is right
      • getRight

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

        public abstract void use​(java.util.function.Consumer<A> leftConsumer,
                                 java.util.function.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​(java.util.function.Function<A,​X> leftFunction,
                                                          java.util.function.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​(java.util.function.Function<A,​T> leftFunction,
                                     java.util.function.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