Class Either<L,R>

java.lang.Object
com.dudko.tools.safejavastreams.core.Either<L,R>
Type Parameters:
L - type of the Left (typically an error or failure reason)
R - type of the Right (typically a success value)
Direct Known Subclasses:
Either.Left, Either.Right

public abstract class Either<L,R> extends Object
Functional-style container type that represents a value of one of two possible types.

Either is often used to indicate a computation that may result in a value (Right) or an error (Left). This is a functional alternative to throwing exceptions and is similar to Try, Result, etc.

  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    private static final class 
     
    private static final class 
     
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    abstract <T> Either<L,T>
    flatMap(Function<? super R,Either<L,T>> mapper)
    Applies a function that returns another Either to Right value.
    abstract <T> T
    fold(Function<? super L,? extends T> leftMapper, Function<? super R,? extends T> rightMapper)
    Reduces this Either into a single value by applying one of the provided functions.
    abstract R
    get()
    Converts this Either to a Stream.
    abstract L
    Gets the Left value if present.
    getOrElse(R defaultValue)
    Converts this Either to a Stream.
    getOrElseGet(Function<? super L,? extends R> mapper)
    Converts this Either to a Stream.
    abstract R
    Gets the Right value if present.
    abstract Either<L,R>
    ifLeft(Consumer<? super L> action)
    Executes action if this is Left.
    abstract Either<L,R>
    ifRight(Consumer<? super R> action)
    Executes action if this is Right.
    abstract boolean
    Checks whether the value is a Left.
    abstract boolean
    Checks whether the value is a Right.
    static <L, R> Either<L,R>
    left(L value)
    Creates a Left instance.
    abstract <T> Either<L,T>
    map(Function<? super R,? extends T> mapper)
    Maps Right value to another type if present.
    abstract Either<L,R>
    peek(Consumer<? super Either<L,R>> inspector)
    Allows observing the value (for logging, debugging, etc.).
    abstract Either<L,R>
    recover(Function<? super L,? extends R> recovery)
    Converts Left into Right using a recovery function.
    abstract Either<L,R>
    recoverWith(Function<? super L,Either<L,R>> recovery)
    Converts Left into another Either using a recovery function.
    static <L, R> Either<L,R>
    right(R value)
    Creates a Right instance.
    abstract Optional<R>
    Converts Right value to Optional, or empty if Left.

    Methods inherited from class java.lang.Object

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

    • Either

      public Either()
  • Method Details

    • isRight

      public abstract boolean isRight()
      Checks whether the value is a Right.
      Returns:
      true if this is a Right instance
    • isLeft

      public abstract boolean isLeft()
      Checks whether the value is a Left.
      Returns:
      true if this is a Left instance
    • getLeft

      public abstract L getLeft()
      Gets the Left value if present.
      Returns:
      Left value
      Throws:
      NoSuchElementException - if this is not a Left
    • getRight

      public abstract R getRight()
      Gets the Right value if present.
      Returns:
      Right value
      Throws:
      NoSuchElementException - if this is not a Right
    • map

      public abstract <T> Either<L,T> map(Function<? super R,? extends T> mapper)
      Maps Right value to another type if present. If Left, the result remains Left.
      Type Parameters:
      T - target type
      Parameters:
      mapper - function to map Right to T
      Returns:
      new Either
    • flatMap

      public abstract <T> Either<L,T> flatMap(Function<? super R,Either<L,T>> mapper)
      Applies a function that returns another Either to Right value. If Left, the result remains unchanged.
      Type Parameters:
      T - target type
      Parameters:
      mapper - function that returns Either
      Returns:
      new Either
    • ifRight

      public abstract Either<L,R> ifRight(Consumer<? super R> action)
      Executes action if this is Right.
      Parameters:
      action - consumer to run
      Returns:
      same Either
    • ifLeft

      public abstract Either<L,R> ifLeft(Consumer<? super L> action)
      Executes action if this is Left.
      Parameters:
      action - consumer to run
      Returns:
      same Either
    • fold

      public abstract <T> T fold(Function<? super L,? extends T> leftMapper, Function<? super R,? extends T> rightMapper)
      Reduces this Either into a single value by applying one of the provided functions.
      Type Parameters:
      T - result type
      Parameters:
      leftMapper - function to apply to Left
      rightMapper - function to apply to Right
      Returns:
      reduced value
    • recover

      public abstract Either<L,R> recover(Function<? super L,? extends R> recovery)
      Converts Left into Right using a recovery function.
      Parameters:
      recovery - mapping function
      Returns:
      recovered Either
    • recoverWith

      public abstract Either<L,R> recoverWith(Function<? super L,Either<L,R>> recovery)
      Converts Left into another Either using a recovery function.
      Parameters:
      recovery - function to convert Left into Either
      Returns:
      new Either
    • peek

      public abstract Either<L,R> peek(Consumer<? super Either<L,R>> inspector)
      Allows observing the value (for logging, debugging, etc.).
      Parameters:
      inspector - consumer that sees this Either
      Returns:
      same Either
    • toOptional

      public abstract Optional<R> toOptional()
      Converts Right value to Optional, or empty if Left.
      Returns:
      Optional with Right or empty
    • get

      public abstract R get()
      Converts this Either to a Stream.
      Returns:
      Stream with this Either
    • getOrElse

      public R getOrElse(R defaultValue)
      Converts this Either to a Stream.
      Returns:
      Stream with this Either
    • getOrElseGet

      public R getOrElseGet(Function<? super L,? extends R> mapper)
      Converts this Either to a Stream.
      Returns:
      Stream with this Either
    • right

      public static <L, R> Either<L,R> right(R value)
      Creates a Right instance.
      Type Parameters:
      L - Left type
      R - Right type
      Parameters:
      value - right value
      Returns:
      Right Either
    • left

      public static <L, R> Either<L,R> left(L value)
      Creates a Left instance.
      Type Parameters:
      L - Left type
      R - Right type
      Parameters:
      value - left value
      Returns:
      Left Either