Class Either<L,R>

java.lang.Object
com.github.darksoulq.abyssallib.common.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 class Either<L,R> extends Object
A container object which may hold one of two types of values: a Left or a Right.

By convention, the Left type represents a failure or error, while the Right type represents a success or the "correct" value.

  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static final class 
    Represents the Left side of the Either.
    static final class 
    Represents the Right side of the Either.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    apply(Consumer<? super L> lc, Consumer<? super R> rc)
    Executes the appropriate consumer based on which side is present.
    <LL,RR> Either<LL,RR>
    flatMap(Function<? super L, ? extends Either<? extends LL, ? extends RR>> lf, Function<? super R, ? extends Either<? extends LL, ? extends RR>> rf)
    FlatMaps both sides into a new Either type.
    abstract <T> T
    fold(Function<? super L, ? extends T> lf, Function<? super R, ? extends T> rf)
    Reduces the Either to a single value by applying the matching function.
    abstract void
    ifLeft(Consumer<? super L> c)
    Executes the consumer if this is a Left.
    abstract void
    ifRight(Consumer<? super R> c)
    Executes the consumer if this is a Right.
    abstract Optional<L>
     
    static <L,R> Either<L,R>
    left(L value)
    Creates an instance of Either containing a Left value.
    abstract L
    Returns the Left value if present, otherwise returns the provided default.
    abstract L
    Returns the Left value or throws an exception if this is a Right.
    <LL,RR> Either<LL,RR>
    map(Function<? super L, ? extends LL> lf, Function<? super R, ? extends RR> rf)
    Transforms both the Left and Right values using the provided functions.
    abstract <LL> Either<LL,R>
    mapLeft(Function<? super L, ? extends LL> f)
    Transforms the Left value using the provided function.
    abstract <RR> Either<L,RR>
    mapRight(Function<? super R, ? extends RR> f)
    Transforms the Right value using the provided function.
    abstract Optional<R>
     
    static <L,R> Either<L,R>
    right(R value)
    Creates an instance of Either containing a Right value.
    abstract R
    Returns the Right value if present, otherwise returns the provided default.
    abstract R
    Returns the Right value or throws an exception if this is a Left.
    Swaps the sides of the Either.

    Methods inherited from class Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • left

      public static <L,R> Either<L,R> left(L value)
      Creates an instance of Either containing a Left value.
      Type Parameters:
      L - The type of the Left value.
      R - The type of the Right value.
      Parameters:
      value - The value to wrap.
      Returns:
      A Left instance of Either.
    • right

      public static <L,R> Either<L,R> right(R value)
      Creates an instance of Either containing a Right value.
      Type Parameters:
      L - The type of the Left value.
      R - The type of the Right value.
      Parameters:
      value - The value to wrap.
      Returns:
      A Right instance of Either.
    • left

      public abstract Optional<L> left()
      Returns:
      An Optional containing the Left value if present, otherwise empty.
    • right

      public abstract Optional<R> right()
      Returns:
      An Optional containing the Right value if present, otherwise empty.
    • leftOrElse

      public abstract L leftOrElse(L def)
      Returns the Left value if present, otherwise returns the provided default.
      Parameters:
      def - The default value.
      Returns:
      The Left value or the default.
    • rightOrElse

      public abstract R rightOrElse(R def)
      Returns the Right value if present, otherwise returns the provided default.
      Parameters:
      def - The default value.
      Returns:
      The Right value or the default.
    • leftOrThrow

      public abstract L leftOrThrow()
      Returns the Left value or throws an exception if this is a Right.
      Returns:
      The Left value.
      Throws:
      IllegalStateException - If this is a Right instance.
    • rightOrThrow

      public abstract R rightOrThrow()
      Returns the Right value or throws an exception if this is a Left.
      Returns:
      The Right value.
      Throws:
      IllegalStateException - If this is a Left instance.
    • mapLeft

      public abstract <LL> Either<LL,R> mapLeft(Function<? super L, ? extends LL> f)
      Transforms the Left value using the provided function.
      Type Parameters:
      LL - The new Left type.
      Parameters:
      f - The transformation function.
      Returns:
      A new Either with the transformed Left or the original Right.
    • mapRight

      public abstract <RR> Either<L,RR> mapRight(Function<? super R, ? extends RR> f)
      Transforms the Right value using the provided function.
      Type Parameters:
      RR - The new Right type.
      Parameters:
      f - The transformation function.
      Returns:
      A new Either with the transformed Right or the original Left.
    • map

      public <LL,RR> Either<LL,RR> map(Function<? super L, ? extends LL> lf, Function<? super R, ? extends RR> rf)
      Transforms both the Left and Right values using the provided functions.
      Type Parameters:
      LL - The new Left type.
      RR - The new Right type.
      Parameters:
      lf - The function to apply to a Left.
      rf - The function to apply to a Right.
      Returns:
      A new transformed Either.
    • ifLeft

      public abstract void ifLeft(Consumer<? super L> c)
      Executes the consumer if this is a Left.
      Parameters:
      c - The consumer to execute.
    • ifRight

      public abstract void ifRight(Consumer<? super R> c)
      Executes the consumer if this is a Right.
      Parameters:
      c - The consumer to execute.
    • apply

      public void apply(Consumer<? super L> lc, Consumer<? super R> rc)
      Executes the appropriate consumer based on which side is present.
      Parameters:
      lc - The consumer for a Left value.
      rc - The consumer for a Right value.
    • fold

      public abstract <T> T fold(Function<? super L, ? extends T> lf, Function<? super R, ? extends T> rf)
      Reduces the Either to a single value by applying the matching function.
      Type Parameters:
      T - The result type.
      Parameters:
      lf - The function to apply to a Left.
      rf - The function to apply to a Right.
      Returns:
      The result of the applied function.
    • flatMap

      public <LL,RR> Either<LL,RR> flatMap(Function<? super L, ? extends Either<? extends LL, ? extends RR>> lf, Function<? super R, ? extends Either<? extends LL, ? extends RR>> rf)
      FlatMaps both sides into a new Either type.
      Type Parameters:
      LL - The new Left type.
      RR - The new Right type.
      Parameters:
      lf - The function providing a new Either from a Left.
      rf - The function providing a new Either from a Right.
      Returns:
      The new Either instance.
    • swap

      public Either<R,L> swap()
      Swaps the sides of the Either.
      Returns:
      A new Either where the Left is the old Right and vice versa.