Interface Either<L,​R>

  • Type Parameters:
    L - The type for the Left side
    R - The type for the Right side
    All Known Implementing Classes:
    Left, Right

    public interface Either<L,​R>
    Represents a value of one of two possible types.
    Instances of Either are either an instance of Left or Right.
    This is what is called a disjoint union, meaning that the Either will never contain both types only one of them.
    This is the biggest difference from a tuple that would contain both values.
    Examples of creating an Either of the type Right.
    Either<InputStream, String> either = new Right<>("Right is not Left");
    In contrast to Try and Option the Either cannot directly be used as a collection (i.e iterate over it).
    This is due to that Either is unbiased as to which of Left, Right it represents.
    To get access to the data represented by the Either you as a developer have to decide to work with either the Left or Right side.

    Consider the Either (Right) instance exemplified above.
    To get hold of the data it represents you need to decide which side (Left, Right) to work with.
    Easiest way is to first decide which side the instance represents.
    The methods isLeft() and isRight() will help you decide which side is represented.
    Invoking either left() or right() will yield a biased projection for that side.
    RightProjection<InputStream, String> projection = either.right();
    Since:
    1.1
    • Method Detail

      • isRight

        boolean isRight()
        Returnstrue if this instance is Right, else false.
        Returns:
        true if this instance is Right.
        Since:
        1.1
      • isLeft

        default boolean isLeft()
        Returnstrue if this instance is Left, else false
        Returns:
        true if this instance is Left.
        Since:
        1.1
      • fold

        <T> T fold​(java.util.function.Function<L,​T> func_left,
                   java.util.function.Function<R,​T> func_right)
        Applies func_left if this is a Left or func_right if this is a Right
        Type Parameters:
        T - The type to return from func_left/func_right
        Parameters:
        func_left - The function to apply in case this is a Left
        func_right - The function to apply in case this is a Right
        Returns:
        The result from applying either func_left or func_right
        Since:
        1.1
      • swap

        Either<R,​L> swap()
        If this is a Left, then return the left value in Right or vice versa.
        Returns:
        The swapped version of this instance
        Since:
        1.1