Class EitherCompanion


  • public final class EitherCompanion
    extends java.lang.Object
    Acts as a Scala type companion object for Left/Right.
    The primary purpose is to get the Scala feel of instantiating classes.
    In Scala you can define a companion object for a class, acting as a static reference/singleton for that class allowing you do define factory methods.
    One use case is to define methods with the same name as the class and let these methods invoke the constructor thus creating a nice way to create instances without using the word "new".
    This can be achieved in java by statically importing a method and then using it.
    The limitation is that classes may not have method with the same name as the class itself hence new companion classes have to be created.
    To be able to use it in a neat concise way one needs to statically import the method.
     import static javascalautils.EitherCompanion.Left;
     import static javascalautils.EitherCompanion.Right;
    
     Either<InputStream,String> left = Left(new FileInputStream("foo.bar"));
     Either<InputStream,String> right = Right("Right is not Left");
     
    Since:
    1.3
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <L,​R>
      Left<L,​R>
      Left​(L value)
      Creates an instance of Left.
      static <L,​R>
      Right<L,​R>
      Right​(R value)
      Creates an instance of Right.
      • Methods inherited from class java.lang.Object

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

      • Left

        public static <L,​R> Left<L,​R> Left​(L value)
        Creates an instance of Left.
        Best used in conjunction with statically importing this method.
         import static javascalautils.EitherCompanion.Left;
        
         Either<InputStream,String> left = Left(new FileInputStream("foo.bar"));
         
        Type Parameters:
        L - The type for the Left side (not used for this method)
        R - The type for the Right side
        Parameters:
        value - The value represented by this Left
        Returns:
        The Left representing the value
        Since:
        1.3
        See Also:
        Left(Object)
      • Right

        public static <L,​R> Right<L,​R> Right​(R value)
        Creates an instance of Right.
        Best used in conjunction with statically importing this method.
         import static javascalautils.EitherCompanion.Right;
        
         Either<InputStream,String> right = Right("Right is not Left");
         
        Type Parameters:
        L - The type for the Left side (not used for this method)
        R - The type for the Right side
        Parameters:
        value - The value represented by this Right
        Returns:
        The Right representing the value
        Since:
        1.3
        See Also:
        Right(Object)