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(@NotNull BiConsumer<? super L, ? super R> consumer) Consumes both left and right valuesConsumes both left and right valuesbooleanflatMapLeft(@NotNull Function<? super L, Either<T, R>> fn) flatMapRight(@NotNull Function<? super R, Either<L, T>> fn) getLeft()Returns a left valuegetLeftElse(L defaultValue) If thisEitheris left, returns left value.getLeftElseGet(@NotNull Supplier<? extends L> defaultValueSupplier) If thisEitheris left, returns left value.getRight()Returns a right valuegetRightElse(R defaultValue) If thisEitheris right, returns right value.getRightElseGet(@NotNull 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(@NotNull BiFunction<? super L, ? super R, ? extends U> fn) Applies a function to thisEither's left and right values<U> Ureduce(@NotNull Function<? super L, ? extends U> leftFn, @NotNull Function<? super R, ? extends U> rightFn) Applies 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
@Contract(pure=true) public L getLeftElseGet(@NotNull @NotNull Supplier<? extends L> defaultValueSupplier) If thisEitheris left, returns left value. Otherwise, returns a supplied default value -
getRightElseGet
@Contract(pure=true) public R getRightElseGet(@NotNull @NotNull Supplier<? extends R> defaultValueSupplier) If thisEitheris right, returns right value. Otherwise, returns a supplied default value -
ifLeft
@Contract(pure=true) @NotNull public @NotNull Either<L,R> ifLeft(@NotNull @NotNull Consumer<? super L> leftConsumer) - Parameters:
leftConsumer- a consumer of left value- Returns:
- this
Either
-
ifRight
@Contract(pure=true) @NotNull public @NotNull Either<L,R> ifRight(@NotNull @NotNull Consumer<? super R> rightConsumer) - Parameters:
rightConsumer- a consumer of right value- Returns:
- this
Either
-
consume
@Contract(pure=true) @NotNull public @NotNull Either<L,R> consume(@NotNull @NotNull BiConsumer<? super L, ? super R> consumer) 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) @NotNull public @NotNull Either<L,R> consume(@NotNull @NotNull Consumer<? super L> leftConsumer, @NotNull @NotNull 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(@NotNull @NotNull Function<? super L, ? extends U> leftFn, @NotNull @NotNull 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
@Contract(pure=true) public <U> U reduce(@NotNull @NotNull BiFunction<? super L, ? super R, ? extends U> fn) 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
@Contract(pure=true) @NotNull public <T> @NotNull Either<T,R> mapLeft(@NotNull @NotNull Function<? super L, ? extends T> fn) - 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
@Contract(pure=true) @NotNull public <T> @NotNull Either<L,T> mapRight(@NotNull @NotNull Function<? super R, ? extends T> fn) - 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
@Contract(pure=true) @NotNull public <T> @NotNull Either<T,R> flatMapLeft(@NotNull @NotNull Function<? super L, Either<T, R>> fn) -
flatMapRight
@Contract(pure=true) @NotNull public <T> @NotNull Either<L,T> flatMapRight(@NotNull @NotNull Function<? super R, Either<L, T>> fn) -
equals
-
hashCode
public int hashCode() -
toString
-