Class Either<A,B>
java.lang.Object
com.github.nullterminated.trylambda.Either<A,B>
- Type Parameters:
A- the left value typeB- the right value type
- All Implemented Interfaces:
Serializable
- Direct Known Subclasses:
Either.Left,Either.Right
A Java implementation of the Either monad.
The Either type represents values with two possibilities: a value of type
Either a b is either Left a or Right b.
The Either type is sometimes used to represent a value which is either
correct or an error; by convention, the Left constructor is used to hold an
error value and the Right constructor is used to hold a correct value
(mnemonic: "right" also means "correct").
Pattern matching is accomplished using polymorphism.
- Author:
- Ramsey Gurley
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classstatic final class -
Method Summary
Modifier and TypeMethodDescriptionflip()Flip an Either<A,B> to a Either<B,A>.abstract AgetLeft()abstract BgetRight()final booleanisLeft()final booleanisRight()static <A,B> Either <A, B> left(A left) Factory method for constructing lefts.abstract <X,Y> Either <X, Y> Map this Either<A,B> to a new Either<X,Y>.abstract <T> TReduce an Either<A,B> to a single value type T.static <A,B> Either <A, B> right(B right) Factory method for constructing rights.abstract voidPass the value of this either to a consumer.
-
Method Details
-
left
Factory method for constructing lefts.- Type Parameters:
A- the left typeB- the right type- Parameters:
left- the left value- Returns:
- a new left
-
right
Factory method for constructing rights.- Type Parameters:
A- the left typeB- the right type- Parameters:
right- the right value- Returns:
- a new right
-
isLeft
public final boolean isLeft()- Returns:
- true if left
-
isRight
public final boolean isRight()- Returns:
- true if right
-
getLeft
- Returns:
- the left value
- Throws:
UnsupportedOperationException- if the receiver is right
-
getRight
- Returns:
- the right value
- Throws:
UnsupportedOperationException- if the receiver is left
-
use
Pass the value of this either to a consumer.- Parameters:
leftConsumer- the consumer for leftsrightConsumer- the consumer for rights
-
map
Map this Either<A,B> to a new Either<X,Y>.- Type Parameters:
X- the new left typeY- the new right type- Parameters:
leftFunction- function to convert A to XrightFunction- function to convert B to Y- Returns:
- a new Either<X,Y>
-
reduce
Reduce an Either<A,B> to a single value type T.- Type Parameters:
T- the result type- Parameters:
leftFunction- function to convert A to TrightFunction- function to convert B to T- Returns:
- a value typed T
-
flip
Flip an Either<A,B> to a Either<B,A>.- Returns:
- a new either with types flipped
-