Class Either<TLeft,​TRight>

  • Type Parameters:
    TLeft - Left type.
    TRight - Right type.

    public final class Either<TLeft,​TRight>
    extends java.lang.Object
    Represents a 2-type discriminate union with Left and Right components.

    The following example assigns values to and extracts them from an Either<Integer, String>:

    
     class Sample {
         static void PrintEither(Either<Integer, String> either) {
             // If either contains the left type (Integer)
             if (either.isLeft()) {
                 // Output the left (Integer) value to the console
                 System.out.println(either.getLeft());
             } else { // either.isRight()
                 // Output the right (String) value to the console
                 System.out.println(either.getRight());    		
             }
         }
     
         public static void Example() {
             // Assign an Integer
             Either<Integer, String> either = new Either<Integer, String>(42);
    
             // Will print "42"
             PrintEither(either);
     
             // Re-assign a String
             either.setRight("Hello world!");
     
             // Will print "Hello world!"
             PrintEither(either);
         }
     }
     
    • Constructor Summary

      Constructors 
      Constructor Description
      Either​(TLeft left, TypeGuard1... guard)
      Creates a new instance of Either from an item of type TLeft.
      Either​(TRight right, TypeGuard2... guard)
      Creates a new instance of Either from an item of type TRight.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean equals​(java.lang.Object obj)  
      TLeft getLeft()
      Gets the Left component.
      TRight getRight()
      Gets the Right component.
      int hashCode()  
      boolean isLeft()
      Returns true if holding a value of type TLeft.
      boolean isRight()
      Returns true if holding a value of type TRight.
      static <TLeft,​TRight>
      Either<TLeft,​TRight>
      makeLeft​(TLeft left)
      Creates a new Either explicitly placing the item in the Left component.
      static <TLeft,​TRight>
      Either<TLeft,​TRight>
      makeRight​(TRight right)
      Creates a new Either explicitly placing the item in the Right component.
      void setLeft​(TLeft left)
      Sets the Left component.
      void setRight​(TRight right)
      Sets the Right component.
      • Methods inherited from class java.lang.Object

        getClass, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • Either

        public Either​(TLeft left,
                      TypeGuard1... guard)
        Creates a new instance of Either from an item of type TLeft.
        Parameters:
        left - Item of type TLeft.
        guard - Do not provide, used to disambiguate functions with similar signatures.
      • Either

        public Either​(TRight right,
                      TypeGuard2... guard)
        Creates a new instance of Either from an item of type TRight.
        Parameters:
        right - Item of type TRight.
        guard - Do not provide, used to disambiguate functions with similar signatures.
    • Method Detail

      • isLeft

        public boolean isLeft()
        Returns true if holding a value of type TLeft.
        Returns:
        true if holding a value of type TLeft.
      • getLeft

        public TLeft getLeft()
                      throws java.lang.ClassCastException
        Gets the Left component.
        Returns:
        Contained item of type TLeft.
        Throws:
        java.lang.ClassCastException - Thrown on get if the inhabiting object is Right.
      • setLeft

        public void setLeft​(TLeft left)
        Sets the Left component.
        Parameters:
        left - Value of type TLeft.
      • isRight

        public boolean isRight()
        Returns true if holding a value of type TRight.
        Returns:
        true if holding a value of type TRight.
      • getRight

        public TRight getRight()
                        throws java.lang.ClassCastException
        Gets the Right component.
        Returns:
        Contained item of type TRight.
        Throws:
        java.lang.ClassCastException - Thrown on get if the inhabiting object is Left.
      • setRight

        public void setRight​(TRight right)
        Sets the Right component.
        Parameters:
        right - Value of type TRight.
      • equals

        public boolean equals​(java.lang.Object obj)
        Overrides:
        equals in class java.lang.Object
      • hashCode

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

        public static <TLeft,​TRight> Either<TLeft,​TRight> makeLeft​(TLeft left)
        Creates a new Either explicitly placing the item in the Left component.
        Type Parameters:
        TLeft - Left type.
        TRight - Right type.
        Parameters:
        left - Item to place in the Either.
        Returns:
        Either containing the item.
      • makeRight

        public static <TLeft,​TRight> Either<TLeft,​TRight> makeRight​(TRight right)
        Creates a new Either explicitly placing the item in the Right component.
        Type Parameters:
        TLeft - Left type.
        TRight - Right type.
        Parameters:
        right - Item to place in the Either.
        Returns:
        Either containing the item.