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.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 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 boolean
equals(java.lang.Object obj)
TLeft
getLeft()
Gets the Left component.TRight
getRight()
Gets the Right component.int
hashCode()
boolean
isLeft()
Returnstrue
if holding a value of typeTLeft
.boolean
isRight()
Returnstrue
if 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.void
setLeft(TLeft left)
Sets the Left component.void
setRight(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()
Returnstrue
if holding a value of typeTLeft
.- Returns:
true
if 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()
Returnstrue
if holding a value of typeTRight
.- Returns:
true
if 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:
equals
in classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCode
in 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.
-
-