Class Either<T,​U>


  • public class Either<T,​U>
    extends Object
    It represents objects with two exclusive possibilities: either it is an object of type T or an object of type U.
    Author:
    mmoquillon
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      <R> R apply​(Function<? super T,​? extends R> leftFunction, Function<? super U,​? extends R> rightFunction)
      Applies one of the specified functions according to the nature of this Either: apply leftFunction if it is a left-object or apply the rightFunction if it is a right-object.
      T getLeft()
      If this Either is a left-object, then returns it, otherwise throws NoSuchElementException.
      U getRight()
      If this Either is a right-object, then returns it, otherwise throws NoSuchElementException.
      void ifLeftOrRight​(Consumer<? super T> leftConsumer, Consumer<? super U> rightConsumer)
      Applies one of the specified consumers according to the nature of this Either: apply leftConsumer if it is a left-object or apply rightConsumer if it is a right-object.
      boolean isLeft()
      Is an object of type T?
      boolean isRight()
      Is an object of type U?
      static <T,​U>
      Either<T,​U>
      left​(T left)
      Constructs an Either as a left-object
      static <T,​U>
      Either<T,​U>
      right​(U right)
      Constructs an Either as a right-object
    • Method Detail

      • left

        public static <T,​U> Either<T,​U> left​(T left)
        Constructs an Either as a left-object
        Type Parameters:
        T - the type of the left-object
        U - the type of the right-object.
        Parameters:
        left - the object from which to construct an Either.
        Returns:
        either an object of type T or an object of type U but whose the actual value is of type T.
      • right

        public static <T,​U> Either<T,​U> right​(U right)
        Constructs an Either as a right-object
        Type Parameters:
        T - the type of the left-object
        U - the type of the right-object.
        Parameters:
        right - the object from which to construct an Either.
        Returns:
        either an object of type T or an object of type U but whose the actual value is of type U.
      • isLeft

        public boolean isLeft()
        Is an object of type T?
        Returns:
        true if the actual value is a left-object, false otherwise.
      • isRight

        public boolean isRight()
        Is an object of type U?
        Returns:
        true if the actual value is a right-object, false otherwise.
      • getLeft

        public T getLeft()
        If this Either is a left-object, then returns it, otherwise throws NoSuchElementException.
        Returns:
        the left-object of this Either.
      • getRight

        public U getRight()
        If this Either is a right-object, then returns it, otherwise throws NoSuchElementException.
        Returns:
        the right-object of this Either.
      • ifLeftOrRight

        public void ifLeftOrRight​(Consumer<? super T> leftConsumer,
                                  Consumer<? super U> rightConsumer)
        Applies one of the specified consumers according to the nature of this Either: apply leftConsumer if it is a left-object or apply rightConsumer if it is a right-object.
        Parameters:
        leftConsumer - a consumer of a left-object.
        rightConsumer - a consumer of a right-object.
      • apply

        public <R> R apply​(Function<? super T,​? extends R> leftFunction,
                           Function<? super U,​? extends R> rightFunction)
        Applies one of the specified functions according to the nature of this Either: apply leftFunction if it is a left-object or apply the rightFunction if it is a right-object.
        Type Parameters:
        R - the type of the returning object computed by one of the specified functions.
        Parameters:
        leftFunction - a function on a left-object and returning a given value of type R.
        rightFunction - a function on a right-object and returning a given value of type R.
        Returns:
        the object computed by one of the specified functions according to the actual value of this Either.