Package io.activej.common.collection
Class Either<L,R>
java.lang.Object
io.activej.common.collection.Either<L,R>
- Type Parameters:
L- type of left valueR- type of right value
A compound type that represents a disjoint union of two values (only one value is present, either left or right)
Supports null values
-
Method Summary
Modifier and TypeMethodDescriptionconsume(BiConsumer<? super L, ? super R> consumer) Consumes both left and right valuesConsumes both left and right valuesbooleanflatMapLeft(Function<? super L, Either<T, R>> fn) flatMapRight(Function<? super R, Either<L, T>> fn) getLeft()Returns a left valuegetLeftElse(L defaultValue) If thisEitheris left, returns left value.getLeftElseGet(Supplier<? extends L> defaultValueSupplier) If thisEitheris left, returns left value.getRight()Returns a right valuegetRightElse(R defaultValue) If thisEitheris right, returns right value.getRightElseGet(Supplier<? extends R> defaultValueSupplier) If thisEitheris right, returns right value.inthashCode()Consumes a left value if thisEitheris left.Consumes a right value if thisEitheris right.booleanisLeft()Returns whether thisEitheris left (has left value)booleanisRight()Returns whether thisEitheris right (has right value)static <L,R> Either<L, R> left(L left) Creates a newEitherwith a left value<U> Ureduce(BiFunction<? super L, ? super R, ? extends U> fn) Applies a function to thisEither's left and right values<U> UApplies a function to thisEither's left or right valuestatic <L,R> Either<L, R> right(R right) Creates a newEitherwith a right valueswap()Returns a newEitherwhich has its left and right values swappedtoString()
-
Method Details
-
left
Creates a newEitherwith a left value- Type Parameters:
L- type of left valueR- type of right value- Parameters:
left- an object to be used as a left value- Returns:
- a new instance of
Eitherwith a left value
-
right
Creates a newEitherwith a right value- Type Parameters:
L- type of left valueR- type of right value- Parameters:
right- an object to be used as a right value- Returns:
- a new instance of
Eitherwith a right value
-
isLeft
@Contract(pure=true) public boolean isLeft()Returns whether thisEitheris left (has left value) -
isRight
@Contract(pure=true) public boolean isRight()Returns whether thisEitheris right (has right value) -
getLeft
Returns a left value -
getRight
Returns a right value -
getLeftElse
If thisEitheris left, returns left value. Otherwise, returns a given default value -
getRightElse
If thisEitheris right, returns right value. Otherwise, returns a given default value -
getLeftElseGet
If thisEitheris left, returns left value. Otherwise, returns a supplied default value -
getRightElseGet
If thisEitheris right, returns right value. Otherwise, returns a supplied default value -
ifLeft
- Parameters:
leftConsumer- a consumer of left value- Returns:
- this
Either
-
ifRight
- Parameters:
rightConsumer- a consumer of right value- Returns:
- this
Either
-
consume
Consumes both left and right valuesAlways returns this
Either- Parameters:
consumer- a consumer of left and right values- Returns:
- this
Either
-
consume
@Contract(pure=true) public Either<L,R> consume(Consumer<? super L> leftConsumer, Consumer<? super R> rightConsumer) Consumes both left and right valuesAlways returns this
Either- Parameters:
leftConsumer- a consumer of left valuerightConsumer- a consumer of right value- Returns:
- this
Either
-
reduce
@Contract(pure=true) public <U> U reduce(Function<? super L, ? extends U> leftFn, Function<? super R, ? extends U> rightFn) Applies a function to thisEither's left or right value- Type Parameters:
U- a type of mapping result- Parameters:
leftFn- a function to map left valuerightFn- a function to map left value- Returns:
- a result of mapping of either left or right value
-
reduce
Applies a function to thisEither's left and right values- Type Parameters:
U- a type of mapping result- Parameters:
fn- a function to map left and right values- Returns:
- a result of mapping of left and right values
-
swap
Returns a newEitherwhich has its left and right values swapped- Returns:
- a swapped
Either
-
mapLeft
- Type Parameters:
T- a type of new left value- Parameters:
fn- a function to map left value to a new value- Returns:
- an
Eitherwith a mapped left value
-
mapRight
- Type Parameters:
T- a type of new right value- Parameters:
fn- a function to map right value to a new value- Returns:
- an
Eitherwith a mapped right value
-
flatMapLeft
-
flatMapRight
-
equals
-
hashCode
public int hashCode() -
toString
-