Class Either<L,​R>

  • Type Parameters:
    L - The left value
    R - The right value
    Direct Known Subclasses:
    Either.Left, Either.Right

    public abstract class Either<L,​R>
    extends java.lang.Object
    Represents a value that can be one of two types. Inspired by Either, it is right biased, but does not define all of the combinators that the scala version does.
    • Method Summary

      All Methods Static Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      abstract boolean equals​(java.lang.Object other)  
      R get()
      Get the right projected value of the either.
      static <T> T getOrElse​(Either<?,​? extends T> either, T t)
      Get the right projected value of the either or a provided default value.
      abstract int hashCode()  
      abstract boolean isLeft()
      Check whether this is a Left projection.
      abstract boolean isRight()
      Check whether this is a Right projection.
      static <L,​R,​T extends L>
      Either<L,​R>
      left​(T value)
      Returns a left projected either.
      static <L,​R>
      Either.Left<L,​R>
      leftProjection​(Either<L,​R> either)
      Returns the Left projection for the provided Either or throws an exception if the Either is actually an instance of Either.Right.
      static <L,​R,​T extends R>
      Either<L,​R>
      right​(T value)
      Returns a right projected either.
      static <L,​R>
      Either.Right<L,​R>
      rightProjection​(Either<L,​R> either)
      Returns the Right projection for the provided Either or throws an exception if the Either is actually an instance of Either.Left.
      • Methods inherited from class java.lang.Object

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

      • leftProjection

        public static <L,​R> Either.Left<L,​R> leftProjection​(Either<L,​R> either)
                                                                 throws Either.NotLeftException
        Returns the Left projection for the provided Either or throws an exception if the Either is actually an instance of Either.Right.
        Type Parameters:
        L - the left type of the either.
        R - the right type of the either.
        Parameters:
        either - the either, assumed to be an instance of left, that will
        Returns:
        a Left projection.
        Throws:
        Either.NotLeftException - if the value is a Either.Right.
      • rightProjection

        public static <L,​R> Either.Right<L,​R> rightProjection​(Either<L,​R> either)
                                                                   throws Either.NotRightException
        Returns the Right projection for the provided Either or throws an exception if the Either is actually an instance of Either.Left.
        Type Parameters:
        L - the left type of the either.
        R - the right type of the either.
        Parameters:
        either - the either, assumed to be an instance of left, that will
        Returns:
        a Right projection.
        Throws:
        Either.NotRightException - if the value is a Either.Left.
      • isLeft

        public abstract boolean isLeft()
        Check whether this is a Left projection.
        Returns:
        true if this is a Reft projection
      • isRight

        public abstract boolean isRight()
        Check whether this is a Right projection.
        Returns:
        true if this is a Right projection
      • get

        public R get()
        Get the right projected value of the either. This is unsafe to call without checking whether the value is a right first.
        Returns:
        the wrapped value if is a right projection
        Throws:
        Either.NotRightException - if this is a left projection
      • getOrElse

        public static <T> T getOrElse​(Either<?,​? extends T> either,
                                      T t)
        Get the right projected value of the either or a provided default value.
        Type Parameters:
        T - the default type
        Parameters:
        either - the either from which the method extracts the result if it is a Either.Right.
        t - the default value
        Returns:
        the wrapped value if this is a right projection, otherwise the default
      • hashCode

        public abstract int hashCode()
        Overrides:
        hashCode in class java.lang.Object
      • equals

        public abstract boolean equals​(java.lang.Object other)
        Overrides:
        equals in class java.lang.Object
      • left

        public static <L,​R,​T extends L> Either<L,​R> left​(T value)
        Returns a left projected either.
        Type Parameters:
        L - the type of the left parameter of the result
        R - the type of the right parameter of the result
        T - a refinement type that allows us to wrap subtypes of L
        Parameters:
        value - the value to wrap
        Returns:
        A left projected either
      • right

        public static <L,​R,​T extends R> Either<L,​R> right​(T value)
        Returns a right projected either.
        Type Parameters:
        L - the type of the left parameter of the result
        R - the type of the right parameter of the result
        T - a refinement type that allows us to wrap subtypes of R
        Parameters:
        value - the value to wrap
        Returns:
        a right projected either.