Class Either<A,B>
- java.lang.Object
-
- com.github.nullterminated.trylambda.Either<A,B>
-
- Direct Known Subclasses:
Either.Left,Either.Right
public abstract class Either<A,B> extends java.lang.ObjectA 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
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classEither.Left<A,B>static classEither.Right<A,B>
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description abstract Either<B,A>flip()Flip an Either<A,B> to a Either<B,A>.abstract AgetLeft()abstract BgetRight()booleanisLeft()booleanisRight()static <A,B>
Either<A,B>left(A left)Factory method for constructing lefts.abstract <X,Y>
Either<X,Y>map(java.util.function.Function<A,X> leftFunction, java.util.function.Function<B,Y> rightFunction)Map this Either<A,B> to a new Either<X,Y>.abstract <T> Treduce(java.util.function.Function<A,T> leftFunction, java.util.function.Function<B,T> rightFunction)Reduce 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 voiduse(java.util.function.Consumer<A> leftConsumer, java.util.function.Consumer<B> rightConsumer)Pass the value of this either to a consumer.
-
-
-
Method Detail
-
left
public static <A,B> Either<A,B> left(A 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
public static <A,B> Either<A,B> right(B 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
public abstract A getLeft()
- Returns:
- the left value
- Throws:
java.lang.UnsupportedOperationException- if the receiver is right
-
getRight
public abstract B getRight()
- Returns:
- the right value
- Throws:
java.lang.UnsupportedOperationException- if the receiver is left
-
use
public abstract void use(java.util.function.Consumer<A> leftConsumer, java.util.function.Consumer<B> rightConsumer)
Pass the value of this either to a consumer.- Parameters:
leftConsumer- the consumer for leftsrightConsumer- the consumer for rights
-
map
public abstract <X,Y> Either<X,Y> map(java.util.function.Function<A,X> leftFunction, java.util.function.Function<B,Y> rightFunction)
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
public abstract <T> T reduce(java.util.function.Function<A,T> leftFunction, java.util.function.Function<B,T> rightFunction)
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
-
-