Class Either<L,R>

java.lang.Object
io.activej.common.collection.Either<L,R>
Type Parameters:
L - type of left value
R - type of right value

public final class Either<L,R> extends Object
A compound type that represents a disjoint union of two values (only one value is present, either left or right)

Supports null values

  • Method Details

    • left

      public static <L, R> Either<L,R> left(@Nullable L left)
      Creates a new Either with a left value
      Type Parameters:
      L - type of left value
      R - type of right value
      Parameters:
      left - an object to be used as a left value
      Returns:
      a new instance of Either with a left value
    • right

      public static <L, R> Either<L,R> right(@Nullable R right)
      Creates a new Either with a right value
      Type Parameters:
      L - type of left value
      R - type of right value
      Parameters:
      right - an object to be used as a right value
      Returns:
      a new instance of Either with a right value
    • isLeft

      @Contract(pure=true) public boolean isLeft()
      Returns whether this Either is left (has left value)
    • isRight

      @Contract(pure=true) public boolean isRight()
      Returns whether this Either is right (has right value)
    • getLeft

      @Contract(pure=true) @Nullable public L getLeft()
      Returns a left value
    • getRight

      @Contract(pure=true) @Nullable public R getRight()
      Returns a right value
    • getLeftElse

      @Contract(pure=true) public L getLeftElse(@Nullable L defaultValue)
      If this Either is left, returns left value. Otherwise, returns a given default value
      Parameters:
      defaultValue - a default value to be returned if this Either is right
      Returns:
      a left value if this Either is left or a default value, otherwise
    • getRightElse

      @Contract(pure=true) public R getRightElse(@Nullable R defaultValue)
      If this Either is right, returns right value. Otherwise, returns a given default value
      Parameters:
      defaultValue - a default value to be returned if this Either is left
      Returns:
      a right value if this Either is right or a default value, otherwise
    • getLeftElseGet

      @Contract(pure=true) public L getLeftElseGet(@NotNull @NotNull Supplier<? extends L> defaultValueSupplier)
      If this Either is left, returns left value. Otherwise, returns a supplied default value
      Parameters:
      defaultValueSupplier - a supplier of default value to be obtained if this Either is right
      Returns:
      a left value if this Either is left or a supplied default value, otherwise
    • getRightElseGet

      @Contract(pure=true) public R getRightElseGet(@NotNull @NotNull Supplier<? extends R> defaultValueSupplier)
      If this Either is right, returns right value. Otherwise, returns a supplied default value
      Parameters:
      defaultValueSupplier - a supplier of default value to be obtained if this Either is left
      Returns:
      a right value if this Either is right or a supplied default value, otherwise
    • ifLeft

      @Contract(pure=true) @NotNull public @NotNull Either<L,R> ifLeft(@NotNull @NotNull Consumer<? super L> leftConsumer)
      Consumes a left value if this Either is left. Otherwise, does nothing

      Always returns this Either

      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)
      Consumes a right value if this Either is right. Otherwise, does nothing

      Always returns this Either

      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 values

      Always 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 values

      Always returns this Either

      Parameters:
      leftConsumer - a consumer of left value
      rightConsumer - 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 this Either's left or right value
      Type Parameters:
      U - a type of mapping result
      Parameters:
      leftFn - a function to map left value
      rightFn - 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 this Either'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

      @Contract(pure=true) @NotNull public @NotNull Either<R,L> swap()
      Returns a new Either which 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)
      Returns a mapped Either obtained by mapping a left value of this Either to a new value
      Type Parameters:
      T - a type of new left value
      Parameters:
      fn - a function to map left value to a new value
      Returns:
      an Either with a mapped left value
    • mapRight

      @Contract(pure=true) @NotNull public <T> @NotNull Either<L,T> mapRight(@NotNull @NotNull Function<? super R,? extends T> fn)
      Returns a mapped Either obtained by mapping a right value of this Either to a new value
      Type Parameters:
      T - a type of new right value
      Parameters:
      fn - a function to map right value to a new value
      Returns:
      an Either with 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)
      Returns a mapped Either obtained by mapping a left value of this Either to a new Either which has the same right type
      Type Parameters:
      T - a type of new left value
      Parameters:
      fn - a function to map left value to a new Either
      Returns:
      a mapped Either
    • flatMapRight

      @Contract(pure=true) @NotNull public <T> @NotNull Either<L,T> flatMapRight(@NotNull @NotNull Function<? super R,Either<L,T>> fn)
      Returns a mapped Either obtained by mapping a right value of this Either to a new Either which has the same left type
      Type Parameters:
      T - a type of new right value
      Parameters:
      fn - a function to map right value to a new Either
      Returns:
      a mapped Either
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object