Record Class Either.Left<L,R>

java.lang.Object
java.lang.Record
io.camunda.zeebe.util.Either.Left<L,R>
Type Parameters:
L - The left type
R - The right type
All Implemented Interfaces:
Either<L,R>
Enclosing interface:
Either<L,R>

public static record Either.Left<L,R>(L value) extends Record implements Either<L,R>
A left for either a left or right. By convention, right is used for success and left for error.
  • Constructor Details

    • Left

      public Left(L value)
      Creates an instance of a Left record class.
      Parameters:
      value - the value for the value record component
  • Method Details

    • isRight

      public boolean isRight()
      Description copied from interface: Either
      Returns true if this Either is a Either.Right.
      Specified by:
      isRight in interface Either<L,R>
      Returns:
      true if right, false if left
    • isLeft

      public boolean isLeft()
      Description copied from interface: Either
      Returns true if this Either is a Either.Left.
      Specified by:
      isLeft in interface Either<L,R>
      Returns:
      true if left, false if right
    • get

      public R get()
      Description copied from interface: Either
      Returns the right value, if this is a Either.Right.
      Specified by:
      get in interface Either<L,R>
      Returns:
      the right value
    • getOrElse

      public R getOrElse(R defaultValue)
      Description copied from interface: Either
      Returns the right value, or a default value if this is a Either.Left.
      Specified by:
      getOrElse in interface Either<L,R>
      Parameters:
      defaultValue - the default value
      Returns:
      the right value, or the default value if this is a Either.Left
    • getLeft

      public L getLeft()
      Description copied from interface: Either
      Returns the left value, if this is a Either.Left.
      Specified by:
      getLeft in interface Either<L,R>
      Returns:
      the left value
    • map

      public <T> Either<L,T> map(Function<? super R,? extends T> right)
      Description copied from interface: Either
      Maps the right value, if this is a Either.Right.
      Specified by:
      map in interface Either<L,R>
      Type Parameters:
      T - the type of the resulting right value
      Parameters:
      right - the mapping function for the right value
      Returns:
      a mapped Either.Right or the same Either.Left
    • mapLeft

      public <T> Either<T,R> mapLeft(Function<? super L,? extends T> left)
      Description copied from interface: Either
      Maps the left value, if this is a Either.Left.
      Specified by:
      mapLeft in interface Either<L,R>
      Type Parameters:
      T - the type of the resulting left value
      Parameters:
      left - the mapping function for the left value
      Returns:
      a mapped Either.Left or the same Either.Right
    • flatMap

      public <T> Either<L,T> flatMap(Function<? super R,? extends Either<L,T>> right)
      Description copied from interface: Either
      Flatmaps the right value into a new Either, if this is a Either.Right.

      A common use case is to map a right value to a new right, unless some error occurs in which case the value can be mapped to a new left. Note that this flatMap does not allow to alter the type of the left side. Example:

      
       Either.<String, Integer>right(0) // => Right(0)
         .flatMap(x -> Either.right(x + 1)) // => Right(1)
         .flatMap(x -> Either.left("an error occurred")) // => Left("an error occurred")
         .getLeft(); // => "an error occurred"
       
      Specified by:
      flatMap in interface Either<L,R>
      Type Parameters:
      T - the type of the right side of the resulting either
      Parameters:
      right - the flatmapping function for the right value
      Returns:
      either a mapped Either.Right or a new Either.Left if this is a right; otherwise the same left, but cast to consider the new type of the right.
    • thenDo

      public Either<L,R> thenDo(Consumer<R> action)
      Description copied from interface: Either
      Executes the given action with the right value if this is a Either.Right, otherwise does nothing. This method facilitates side-effect operations on the right value without altering the state or the type of the Either. After executing the action, the original Either<L, R> is returned, allowing for further chaining of operations in a fluent API style.

      When the instance is a Either.Right, the action is executed, and the original Either<L, R> is returned. This maintains the right value's type and allows the action to be performed as a side-effect without changing the outcome. When the instance is a Either.Left, no action is performed, and the Left instance is returned unchanged, preserving the error information.

      Usage example when Either is a Either.Right:

      
       Either<Exception, String> rightEither = Either.right("Success");
       Either<Exception, String> result = rightEither.thenDo(value -> System.out.println("Processed value: " + value));
       // Output: Processed value: Success
       // result remains a Right<Exception, String>, with the value "Success"
       

      Usage example when Either is a Either.Left:

      
       Either<String, Integer> leftEither = Either.left("Error occurred");
       Either<String, Integer> result = leftEither.thenDo(value -> System.out.println("This will not be printed"));
       // No output as the action is not executed
       // result remains an unchanged Left<String, Integer>, containing the original error message
       
      Specified by:
      thenDo in interface Either<L,R>
      Parameters:
      action - the consuming function to perform with the right value, if present
      Returns:
      Eitherinvalid input: '<'L, R> the original Either instance, allowing further operations.
    • ifRight

      public void ifRight(Consumer<R> right)
      Description copied from interface: Either
      Performs the given action with the value if this is a Either.Right, otherwise does nothing.
      Specified by:
      ifRight in interface Either<L,R>
      Parameters:
      right - the consuming function for the right value
    • ifLeft

      public void ifLeft(Consumer<L> action)
      Description copied from interface: Either
      Performs the given action with the value if this is a Either.Left, otherwise does nothing.
      Specified by:
      ifLeft in interface Either<L,R>
      Parameters:
      action - the consuming function for the left value
    • ifRightOrLeft

      public void ifRightOrLeft(Consumer<R> rightAction, Consumer<L> leftAction)
      Description copied from interface: Either
      Performs the given right action with the value if this is a Either.Right, otherwise performs the given left action with the value.
      Specified by:
      ifRightOrLeft in interface Either<L,R>
      Parameters:
      rightAction - the consuming function for the right value
      leftAction - the consuming function for the left value
    • fold

      public <T> T fold(Function<? super R,? extends T> rightFn, Function<? super L,? extends T> leftFn)
      Description copied from interface: Either
      Maps the right or left value into a new type using the provided functions, depending on whether this is a Either.Left or Either.Right.

      A common use case is to map to a new common value in success and error cases. Example:

      
       * Either<String, Integer> success = Either.right(42); // => Right(42)
       * Either<String, Integer> failure = Either.left("Error occurred"); // => Left("Error occurred")
       *
       * var rightFn = result -> "Success: " + result;
       * var leftFn = error -> "Failure: " + error;
       *
       * success.fold(rightFn, leftFn); // => "Success: 42"
       * failure.fold(rightFn, leftFn); // => "Failure: Error occurred"
       
      Specified by:
      fold in interface Either<L,R>
      Type Parameters:
      T - the type of the resulting value
      Parameters:
      rightFn - the mapping function for the right value
      leftFn - the mapping function for the left value
      Returns:
      either a mapped Either.Left or Either.Right, folded to the new type
    • toString

      public final String toString()
      Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. All components in this record class are compared with Objects::equals(Object,Object).
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • value

      public L value()
      Returns the value of the value record component.
      Returns:
      the value of the value record component