Top Description Inners Fields Methods
io.vavr.control

public Interface Either<L, R>

extends Value<R>, Serializable
Known Direct Implementers
io.vavr.control.Either.Left, io.vavr.control.Either.Right
Type Parameters
<L>
The type of the Left value of an Either.
<R>
The type of the Right value of an Either.
Imports
io.vavr.Value, io.vavr.collection.Iterator, .Seq, .Vector, java.io.Serializable, java.util.NoSuchElementException, .Objects, java.util.function.Consumer, .Function, .Predicate, .Supplier

Either represents a value of two possible types. An Either is either a Left or a 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).
If the result of compute() is Left("error"), the value is Left("error").
Author
Daniel Dietrich

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
Either.Left<
left component type
L
,
right component type
R
>

The Left version of an Either.

public static class
Either.LeftProjection<
The type of the Left value of an Either.
L
,
The type of the Right value of an Either.
R
>

Deprecated Either is right-biased. Use swap() instead of projections.
A left projection of an Either.
public static class
Either.Right<
left component type
L
,
right component type
R
>

The Right version of an Either.

public static class
Either.RightProjection<
The type of the Left value of an Either.
L
,
The type of the Right value of an Either.
R
>

Deprecated Either is right-biased. Use swap() instead of projections.
A right projection of an Either.

Field Summary

Modifier and TypeField and Description
public static final long

Method Summary

Modifier and TypeMethod and Description
public default <
The new left type of the resulting Either
X
,
The new right type of the resulting Either
Y
>
Either<X, Y>

Returns:

A new Either instance
bimap
(Function<? super L, ? extends X>
maps the left value if this is a Left
leftMapper
,
Function<? super R, ? extends Y>
maps the right value if this is a Right
rightMapper
)

Maps either the left or the right side of this disjunction.

public boolean
equals(Object
An object
o
)

Redeclares io.vavr.Value.equals.

Clarifies that values have a proper equals() method implemented.

public default Option<Either<L, R>>

Returns:

a new Option instance
filter
(Predicate<? super R>
A predicate
predicate
)

Filters this right-biased Either by testing a predicate.

public default Either<L, R>

Returns:

an Either instance
filterOrElse
(Predicate<? super R>
A predicate
predicate
,
Function<? super R, ? extends L>
A function that turns a right value into a left value if the right value does not make it through the filter.
zero
)

Filters this right-biased Either by testing a predicate.

public default <
Component type of the mapped right value
U
>
Either<L, U>

Returns:

this as Either<L, U> if this is a Left, otherwise the right mapping result
flatMap
(Function<? super R, ? extends Either<L, ? extends U>>
A mapper
mapper
)

FlatMaps this right-biased Either.

public default <
type of the folded value
U
>
U

Returns:

A value of type U
fold
(Function<? super L, ? extends U>
maps the left value if this is a Left
leftMapper
,
Function<? super R, ? extends U>
maps the right value if this is a Right
rightMapper
)

Folds either the left or the right side of this disjunction.

public R

Returns:

the right value
get
()

Redeclares io.vavr.Value.get.

Gets the right value if this is a Right or throws if this is a Left.

public L

Returns:

The left value.
getLeft
()

Returns the left value.

public default R

Returns:

the right value, if the underlying Either is a Right or else the alternative Right value provided by other by applying the Left value.
getOrElseGet
(Function<? super L, ? extends R>
a function which converts a Left value to an alternative Right value
other
)

Gets the Right value or an alternate value, if the projected Either is a Left.

public default <
a throwable type
X extends Throwable
>
R

Returns:

the right value, if the underlying Either is a Right or else throws the exception provided by exceptionFunction by applying the Left value.
getOrElseThrow
(Function<? super L, X>
a function which creates an exception based on a Left value
exceptionFunction
)

Gets the Right value or throws, if the projected Either is a Left.

public int
hashCode()

Redeclares io.vavr.Value.hashCode.

Clarifies that values have a proper hashCode() method implemented.

public default boolean

Returns:

false
isAsync
()

Implements io.vavr.Value.isAsync.

A right-biased Either's value is computed synchronously.

public default boolean
isEmpty()

Implements io.vavr.Value.isEmpty.

Checks, this Value is empty, i.e. if the underlying value is absent.

public default boolean

Returns:

false
isLazy
()

Implements io.vavr.Value.isLazy.

A right-biased Either's value is computed eagerly.

public boolean

Returns:

true, if this is a Left, false otherwise
isLeft
()

Returns whether this Either is a Left.

public boolean

Returns:

true, if this is a Right, false otherwise
isRight
()

Returns whether this Either is a Right.

public default boolean

Returns:

true
isSingleValued
()

Implements io.vavr.Value.isSingleValued.

A right-biased Either is single-valued.

public default Iterator<R>
iterator()

Implements io.vavr.Value.iterator.

Returns a rich io.vavr.collection.Iterator.

public static <
Type of left value.
L
,
Type of right value.
R
>
Either<L, R>

Returns:

A new Left instance.
left
(L
The value.
left
)

Constructs a Left

public default Either.LeftProjection<L, R>

Returns:

a new LeftProjection of this
left
()
Deprecated Either is right-biased. Use swap() instead of projections.

Returns a LeftProjection of this Either.

public default <
Component type of the mapped right value
U
>
Either<L, U>

Returns:

a mapped Monad
map
(Function<? super R, ? extends U>
A mapper
mapper
)

Implements io.vavr.Value.map.

Maps the value of this Either if it is a Right, performs no operation if this is a Left.

public default <
Component type of the mapped right value
U
>
Either<U, R>

Returns:

a mapped Monad
mapLeft
(Function<? super L, ? extends U>
A mapper
leftMapper
)

Maps the value of this Either if it is a Left, performs no operation if this is a Right.

public static <
Type of left value.
L
,
Type of right value.
R
>
Either<L, R>

Returns:

the given either instance as narrowed type Either<L, R>.
narrow
(Either<? extends L, ? extends R>
A Either.
either
)

Narrows a widened Either<?

public default Either<L, R>
orElse(Either<? extends L, ? extends R> other)

public default Either<L, R>
orElse(Supplier<? extends Either<? extends L, ? extends R>> supplier)

public default void
orElseRun(Consumer<? super L>
an action which consumes a Left value
action
)

Runs an action in the case this is a projection on a Left value.

public default Either<L, R>
peek(Consumer<? super R>
The action that will be performed on the element(s).
action
)

Implements io.vavr.Value.peek.

Performs the given action on the first element if this is an eager implementation.

public default Either<L, R>
peekLeft(Consumer<? super L> action)

public static <
Type of left value.
L
,
Type of right value.
R
>
Either<L, R>

Returns:

A new Right instance.
right
(R
The value.
right
)

Constructs a Right

public default Either.RightProjection<L, R>

Returns:

a new RightProjection of this
right
()
Deprecated Either is right-biased. Use swap() instead of projections.

Returns a RightProjection of this Either.

public static <
closure of all left types of the given Eithers
L
,
closure of all right types of the given Eithers
R
>
Either<Seq<L>, Seq<R>>

Returns:

An Either of a Seq of left or right values
sequence
(Iterable<? extends Either<? extends L, ? extends R>>
An Iterable of Eithers
eithers
)

Reduces many Eithers into a single Either by transforming an Iterable<Either<L, R>> into a Either<Seq<L>, Seq<R>>.

public static <
closure of all left types of the given Eithers
L
,
closure of all right types of the given Eithers
R
>
Either<L, Seq<R>>

Returns:

An Either of either a Seq of right values or the first left value, if present.
sequenceRight
(Iterable<? extends Either<? extends L, ? extends R>>
An Iterable of Eithers
eithers
)

Reduces many Eithers into a single Either by transforming an Iterable<Either<L, R>> into a Either<L, Seq<R>>.

public default Either<R, L>

Returns:

a new Either
swap
()

Converts a Left to a Right vice versa by wrapping the value in a new type.

public String
toString()

Redeclares io.vavr.Value.toString.

Clarifies that values have a proper toString() method implemented.

public default Validation<L, R>

Returns:

Validation.valid(get()) if this is right, otherwise Validation.invalid(getLeft()).
toValidation
()

Returns this as Validation.

public static <
The mapped left value type.
L
,
The mapped right value type.
R
,
The type of the given values.
T
>
Either<Seq<L>, Seq<R>>

Returns:

A Either of a Seq of results.
traverse
(Iterable<? extends T>
An Iterable of values.
values
,
Function<? super T, ? extends Either<? extends L, ? extends R>>
A mapper of values to Eithers
mapper
)

Maps the values of an iterable to a sequence of mapped values into a single Either by transforming an Iterable<?

public static <
The mapped left value type.
L
,
The mapped right value type.
R
,
The type of the given values.
T
>
Either<L, Seq<R>>

Returns:

A Either of a Seq of results.
traverseRight
(Iterable<? extends T>
An Iterable of values.
values
,
Function<? super T, ? extends Either<? extends L, ? extends R>>
A mapper of values to Eithers
mapper
)

Maps the values of an iterable to a sequence of mapped values into a single Either by transforming an Iterable<?

Inherited from io.vavr.Value:
collectcollectcontainscorrespondseqexistsforAllforEachgetOrElsegetOrElsegetOrElseThrowgetOrElseTrygetOrNullnarrowoutoutspliteratorstderrstdoutstringPrefixtoArraytoCharSeqtoCompletableFuturetoEithertoEithertoInvalidtoInvalidtoJavaArraytoJavaArraytoJavaArraytoJavaCollectiontoJavaListtoJavaListtoJavaMaptoJavaMaptoJavaMaptoJavaOptionaltoJavaParallelStreamtoJavaSettoJavaSettoJavaStreamtoLefttoLefttoLinkedMaptoLinkedMaptoLinkedSettoListtoMaptoMaptoOptiontoPriorityQueuetoPriorityQueuetoQueuetoRighttoRighttoSettoSortedMaptoSortedMaptoSortedMaptoSortedMaptoSortedSettoSortedSettoStreamtoTreetoTreetoTrytoTrytoValidtoValidtoValidationtoValidationtoVector