L
- The type of the Left value of an Either.R
- The type of the Right value of an Either.public interface Either<L,R> extends Value<R>
Either.Left
or a
Either.Right
.
If the given Either is a Right and projected to a Left, the Left operations have no effect on the Right value.
If the given Either is a Left and projected to a Right, the Right operations have no effect on the Left value.
If a Left is projected to a Left or a Right is projected to a Right, the operations have an effect.
Example: A compute() function, which results either in an Integer value (in the case of success) or in an error message of type String (in the case of failure). By convention the success case is Right and the failure is Left.
Either<String,Integer> value = compute().right().map(i -> i * 2).toEither();
If the result of compute() is Right(1), the value is Right(2).Modifier and Type | Interface and Description |
---|---|
static class |
Either.Left<L,R>
The
Left version of an Either . |
static class |
Either.LeftProjection<L,R>
A left projection of an Either.
|
static class |
Either.Right<L,R>
The
Right version of an Either . |
static class |
Either.RightProjection<L,R>
A right projection of an Either.
|
Modifier and Type | Method and Description |
---|---|
default <X,Y> Either<X,Y> |
bimap(Function<? super L,? extends X> leftMapper,
Function<? super R,? extends Y> rightMapper)
Maps either the left or the right side of this disjunction.
|
boolean |
equals(Object o)
Clarifies that values have a proper equals() method implemented.
|
default Option<Either<L,R>> |
filter(Predicate<? super R> predicate)
Filters this right-biased
Either by testing a predicate. |
default <U> Either<L,U> |
flatMap(Function<? super R,? extends Either<L,? extends U>> mapper)
FlatMaps this right-biased Either.
|
default <U> U |
fold(Function<? super L,? extends U> leftMapper,
Function<? super R,? extends U> rightMapper)
Folds either the left or the right side of this disjunction.
|
R |
get()
Gets the right value if this is a
Right or throws if this is a Left . |
L |
getLeft()
Returns the left value.
|
default R |
getOrElseGet(Function<? super L,? extends R> other)
Gets the Right value or an alternate value, if the projected Either is a Left.
|
default <X extends Throwable> |
getOrElseThrow(Function<? super L,X> exceptionFunction)
Gets the Right value or throws, if the projected Either is a Left.
|
int |
hashCode()
Clarifies that values have a proper hashCode() method implemented.
|
default boolean |
isEmpty()
Checks, this
Value is empty, i.e. |
boolean |
isLeft()
Returns whether this Either is a Left.
|
boolean |
isRight()
Returns whether this Either is a Right.
|
default boolean |
isSingleValued()
A right-biased
Either is single-valued. |
default Iterator<R> |
iterator()
Returns a rich
javaslang.collection.Iterator . |
default Either.LeftProjection<L,R> |
left()
Returns a LeftProjection of this Either.
|
static <L,R> Either<L,R> |
left(L left)
Constructs a
Either.Left |
default <U> Either<L,U> |
map(Function<? super R,? extends U> mapper)
Maps this right-biased Either.
|
default <U> Either<U,R> |
mapLeft(Function<? super L,? extends U> leftMapper)
Maps this right-biased Either.
|
static <L,R> Either<L,R> |
narrow(Either<? extends L,? extends R> either)
Narrows a widened
Either<? extends L, ? extends R> to Either<L, R>
by performing a type safe-cast. |
default Either<L,R> |
orElse(Either<? extends L,? extends R> other) |
default Either<L,R> |
orElse(Supplier<? extends Either<? extends L,? extends R>> supplier) |
default void |
orElseRun(Consumer<? super L> action)
Runs an action in the case this is a projection on a Left value.
|
default Either<L,R> |
peek(Consumer<? super R> action)
Performs the given
action on the first element if this is an eager implementation. |
default Either.RightProjection<L,R> |
right()
Returns a RightProjection of this Either.
|
static <L,R> Either<L,R> |
right(R right)
Constructs a
Either.Right |
default Either<R,L> |
swap()
Converts a
Left to a Right vice versa by wrapping the value in a new type. |
String |
toString()
Clarifies that values have a proper toString() method implemented.
|
contains, corresponds, eq, exists, forAll, forEach, getOption, getOrElse, getOrElse, getOrElseThrow, getOrElseTry, narrow, out, out, stderr, stdout, stringPrefix, toArray, toCharSeq, toJavaArray, toJavaArray, toJavaCollection, toJavaList, toJavaList, toJavaMap, toJavaMap, toJavaOptional, toJavaSet, toJavaSet, toJavaStream, toLeft, toLeft, toList, toMap, toOption, toQueue, toRight, toRight, toSet, toStack, toStream, toTree, toTry, toTry, toVector
spliterator
static <L,R> Either<L,R> right(R right)
Either.Right
L
- Type of left value.R
- Type of right value.right
- The value.Right
instance.static <L,R> Either<L,R> left(L left)
Either.Left
L
- Type of left value.R
- Type of right value.left
- The value.Left
instance.static <L,R> Either<L,R> narrow(Either<? extends L,? extends R> either)
Either<? extends L, ? extends R>
to Either<L, R>
by performing a type safe-cast. This is eligible because immutable/read-only
collections are covariant.L
- Type of left value.R
- Type of right value.either
- A Either
.either
instance as narrowed type Either<L, R>
.L getLeft()
NoSuchElementException
- if this is a Right
.boolean isLeft()
boolean isRight()
default Either.LeftProjection<L,R> left()
default Either.RightProjection<L,R> right()
default <X,Y> Either<X,Y> bimap(Function<? super L,? extends X> leftMapper, Function<? super R,? extends Y> rightMapper)
X
- The new left type of the resulting EitherY
- The new right type of the resulting EitherleftMapper
- maps the left value if this is a LeftrightMapper
- maps the right value if this is a Rightdefault <U> U fold(Function<? super L,? extends U> leftMapper, Function<? super R,? extends U> rightMapper)
U
- type of the folded valueleftMapper
- maps the left value if this is a LeftrightMapper
- maps the right value if this is a Rightdefault R getOrElseGet(Function<? super L,? extends R> other)
other
- a function which converts a Left value to an alternative Right valueother
by applying the Left value.default void orElseRun(Consumer<? super L> action)
action
- an action which consumes a Left valuedefault <X extends Throwable> R getOrElseThrow(Function<? super L,X> exceptionFunction) throws X extends Throwable
X
- a throwable typeexceptionFunction
- a function which creates an exception based on a Left valueexceptionFunction
by applying the Left value.X
- if the projected Either is a LeftX extends Throwable
default Either<R,L> swap()
Left
to a Right
vice versa by wrapping the value in a new type.Either
default <U> Either<L,U> flatMap(Function<? super R,? extends Either<L,? extends U>> mapper)
U
- Component type of the mapped right valuemapper
- A mapperEither<L, U>
if this is a Left, otherwise the right mapping resultNullPointerException
- if mapper
is nulldefault <U> Either<L,U> map(Function<? super R,? extends U> mapper)
map
in interface Value<R>
U
- Component type of the mapped right valuemapper
- A mapperMonad
NullPointerException
- if mapper
is nulldefault <U> Either<U,R> mapLeft(Function<? super L,? extends U> leftMapper)
U
- Component type of the mapped right valueleftMapper
- A mapperMonad
NullPointerException
- if mapper
is nulldefault Option<Either<L,R>> filter(Predicate<? super R> predicate)
Either
by testing a predicate.
predicate
- A predicateOption
instanceNullPointerException
- if predicate
is nullR get()
Right
or throws if this is a Left
.get
in interface Value<R>
NoSuchElementException
- if this is a Left.default boolean isEmpty()
Value
Value
is empty, i.e. if the underlying value is absent.default boolean isSingleValued()
Either
is single-valued.isSingleValued
in interface Value<R>
true
default Iterator<R> iterator()
Value
javaslang.collection.Iterator
.default Either<L,R> peek(Consumer<? super R> action)
Value
action
on the first element if this is an eager implementation.
Performs the given action
on all elements (the first immediately, successive deferred),
if this is a lazy implementation.boolean equals(Object o)
Value
int hashCode()
Value
See Object.hashCode().
Copyright © 2016. All Rights Reserved.