Package com.vladris.maki
Class Either<TLeft,TRight>
- java.lang.Object
-
- com.vladris.maki.Either<TLeft,TRight>
-
- Type Parameters:
TLeft- Left type.TRight- Right type.
public final class Either<TLeft,TRight> extends java.lang.ObjectRepresents 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 typeTLeft.Either(TRight right, TypeGuard2... guard)Creates a new instance of Either from an item of typeTRight.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanequals(java.lang.Object obj)TLeftgetLeft()Gets the Left component.TRightgetRight()Gets the Right component.inthashCode()booleanisLeft()Returnstrueif holding a value of typeTLeft.booleanisRight()Returnstrueif holding a value of typeTRight.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.voidsetLeft(TLeft left)Sets the Left component.voidsetRight(TRight right)Sets the Right component.
-
-
-
Constructor Detail
-
Either
public Either(TLeft left, TypeGuard1... guard)
Creates a new instance of Either from an item of typeTLeft.- Parameters:
left- Item of typeTLeft.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 typeTRight.- Parameters:
right- Item of typeTRight.guard- Do not provide, used to disambiguate functions with similar signatures.
-
-
Method Detail
-
isLeft
public boolean isLeft()
Returnstrueif holding a value of typeTLeft.- Returns:
trueif holding a value of typeTLeft.
-
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 typeTLeft.
-
isRight
public boolean isRight()
Returnstrueif holding a value of typeTRight.- Returns:
trueif holding a value of typeTRight.
-
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 typeTRight.
-
equals
public boolean equals(java.lang.Object obj)
- Overrides:
equalsin classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCodein classjava.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.
-
-