Class Either<L,R>

java.lang.Object
net.luis.utils.util.Either<L,R>
Type Parameters:
L - The type of the left value
R - The type of the right value
Direct Known Subclasses:
Either.Left, Either.Right

public abstract sealed class Either<L,R> extends Object permits Either.Left<L,R>, Either.Right<L,R>
Represents one of two possible values.
An instance of Either is either an instance of Either.Left or Either.Right.

Sealed to prevent external instantiation and extension.

See Also:
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    (package private) static final class 
    Left implementation of the either.
    (package private) static final class 
    Right implementation of the either.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    private
    Private because this class is sealed and not instantiable from outside.
  • Method Summary

    Modifier and Type
    Method
    Description
    abstract void
    ifLeft(@NotNull Consumer<? super L> action)
    Performs the given action if this either instance is a left instance.
    abstract void
    ifRight(@NotNull Consumer<? super R> action)
    Performs the given action if this either instance is a right instance.
    abstract boolean
    Checks if this either instance is a left instance.
    abstract boolean
    Checks if this either instance is a right instance.
    abstract @NotNull Optional<L>
    Returns the left value as an Optional.
    static <L, R> @NotNull Either<L,R>
    left(L value)
    Creates a new either instance with the given value as the left value.
    abstract @UnknownNullability L
    Returns the left value or throws an exception if it is a right either.
    abstract <C, D> @NotNull Either<C,D>
    mapBoth(@NotNull Function<? super L,? extends C> leftMapper, @NotNull Function<? super R,? extends D> rightMapper)
    Maps the left and right values of this either instance to new values.
    <T> Either<T,R>
    mapLeft(@NotNull Function<? super L,? extends T> mapper)
    Maps the left value of this either instance to a new value.
    If this is a right either, the mapping is not applied.
    <T> Either<L,T>
    mapRight(@NotNull Function<? super R,? extends T> mapper)
    Maps the right value of this either instance to a new value.
    If this is a left either, the mapping is not applied.
    abstract <T> T
    mapTo(@NotNull Function<? super L,? extends T> leftMapper, @NotNull Function<? super R,? extends T> rightMapper)
    Maps the left and right values of this either instance to a new value.
    abstract @NotNull Optional<R>
    Returns the right value as an Optional.
    static <L, R> @NotNull Either<L,R>
    right(R value)
    Creates a new either instance with the given value as the right value.
    abstract @UnknownNullability R
    Returns the right value or throws an exception if it is a left either.
    @NotNull Either<R,L>
    Swaps the left and right values of this either instance.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • Either

      private Either()
      Private because this class is sealed and not instantiable from outside.
  • Method Details

    • left

      @NotNull public static <L, R> @NotNull Either<L,R> left(@Nullable L value)
      Creates a new either instance with the given value as the left value.
      Type Parameters:
      L - The type of the left value
      R - The type of the right value
      Parameters:
      value - The left value
      Returns:
      The created (left) either instance
    • right

      @NotNull public static <L, R> @NotNull Either<L,R> right(@Nullable R value)
      Creates a new either instance with the given value as the right value.
      Type Parameters:
      L - The type of the left value
      R - The type of the right value
      Parameters:
      value - The right value
      Returns:
      The created (right) either instance
    • isLeft

      public abstract boolean isLeft()
      Checks if this either instance is a left instance.
      Returns:
      True if this either instance is a left instance, false otherwise
    • isRight

      public abstract boolean isRight()
      Checks if this either instance is a right instance.
      Returns:
      True if this either instance is a right instance, false otherwise
    • ifLeft

      public abstract void ifLeft(@NotNull @NotNull Consumer<? super L> action)
      Performs the given action if this either instance is a left instance.
      Parameters:
      action - The action to perform
      Throws:
      NullPointerException - If the action is null
    • ifRight

      public abstract void ifRight(@NotNull @NotNull Consumer<? super R> action)
      Performs the given action if this either instance is a right instance.
      Parameters:
      action - The action to perform
      Throws:
      NullPointerException - If the action is null
    • left

      @NotNull public abstract @NotNull Optional<L> left()
      Returns the left value as an Optional.
      Returns:
      The left value
    • right

      @NotNull public abstract @NotNull Optional<R> right()
      Returns the right value as an Optional.
      Returns:
      The right value
    • leftOrThrow

      public abstract @UnknownNullability L leftOrThrow()
      Returns the left value or throws an exception if it is a right either.
      Returns:
      The left value
      Throws:
      IllegalStateException - If the left value is not present
    • rightOrThrow

      public abstract @UnknownNullability R rightOrThrow()
      Returns the right value or throws an exception if it is a left either.
      Returns:
      The right value
      Throws:
      IllegalStateException - If the right value is not present
    • swap

      @NotNull public @NotNull Either<R,L> swap()
      Swaps the left and right values of this either instance.
      Returns:
      A new either instance with the swapped values
    • mapBoth

      @NotNull public abstract <C, D> @NotNull Either<C,D> mapBoth(@NotNull @NotNull Function<? super L,? extends C> leftMapper, @NotNull @NotNull Function<? super R,? extends D> rightMapper)
      Maps the left and right values of this either instance to new values.
      Type Parameters:
      C - The new type of the left value
      D - The new type of the right value
      Parameters:
      leftMapper - The mapper for the left value
      rightMapper - The mapper for the right value
      Returns:
      A new either instance with the mapped values
      Throws:
      NullPointerException - If the mapper used in this either implementation is null
    • mapTo

      public abstract <T> T mapTo(@NotNull @NotNull Function<? super L,? extends T> leftMapper, @NotNull @NotNull Function<? super R,? extends T> rightMapper)
      Maps the left and right values of this either instance to a new value.
      Type Parameters:
      T - The type of the mapped value
      Parameters:
      leftMapper - The mapper for the left value
      rightMapper - The mapper for the right value
      Returns:
      The mapped value
      Throws:
      NullPointerException - If the mapper used in this either implementation is null
    • mapLeft

      public <T> Either<T,R> mapLeft(@NotNull @NotNull Function<? super L,? extends T> mapper)
      Maps the left value of this either instance to a new value.
      If this is a right either, the mapping is not applied.
      Type Parameters:
      T - The new type of the left value
      Parameters:
      mapper - The mapper for the left value
      Returns:
      A new either instance with the mapped left value
      Throws:
      NullPointerException - If the mapper is null
    • mapRight

      public <T> Either<L,T> mapRight(@NotNull @NotNull Function<? super R,? extends T> mapper)
      Maps the right value of this either instance to a new value.
      If this is a left either, the mapping is not applied.
      Type Parameters:
      T - The new type of the right value
      Parameters:
      mapper - The mapper for the right value
      Returns:
      A new either instance with the mapped right value
      Throws:
      NullPointerException - If the mapper is null