Package javascalautils
Interface Either<L,R>
-
public interface Either<L,R>
Represents a value of one of two possible types.
Instances ofEither
are either an instance ofLeft
orRight
.
This is what is called a disjoint union, meaning that the Either will never contain both types only one of them.
This is the biggest difference from a tuple that would contain both values.
Examples of creating anEither
of the typeRight
.
Either<InputStream, String> either = new Right<>("Right is not Left");
In contrast toTry
andOption
theEither
cannot directly be used as a collection (i.e iterate over it).
This is due to thatEither
is unbiased as to which ofLeft
,Right
it represents.
To get access to the data represented by theEither
you as a developer have to decide to work with either theLeft
orRight
side.
Consider theEither
(Right
) instance exemplified above.
To get hold of the data it represents you need to decide which side (Left, Right) to work with.
Easiest way is to first decide which side the instance represents.
The methodsisLeft()
andisRight()
will help you decide which side is represented.
Invoking eitherleft()
orright()
will yield a biased projection for that side.
RightProjection<InputStream, String> projection = either.right();
- Since:
- 1.1
-
-
Method Summary
All Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description <T> T
fold(java.util.function.Function<L,T> func_left, java.util.function.Function<R,T> func_right)
default boolean
isLeft()
boolean
isRight()
default LeftProjection<L,R>
left()
Returns aLeftProjection
for this instance.default RightProjection<L,R>
right()
Returns aRightProjection
for this instance.Either<R,L>
swap()
-
-
-
Method Detail
-
isRight
boolean isRight()
- Returns:
true
if this instance isRight
.- Since:
- 1.1
-
isLeft
default boolean isLeft()
- Returns:
true
if this instance isLeft
.- Since:
- 1.1
-
fold
<T> T fold(java.util.function.Function<L,T> func_left, java.util.function.Function<R,T> func_right)
-
right
default RightProjection<L,R> right()
Returns aRightProjection
for this instance.- Returns:
- The projection
- Since:
- 1.1
-
left
default LeftProjection<L,R> left()
Returns aLeftProjection
for this instance.- Returns:
- The projection
- Since:
- 1.1
-
-