Record Class Either.Left<L,R>

java.lang.Object
java.lang.Record
cloud.opencode.base.core.result.Either.Left<L,R>
Type Parameters:
L - left type - 左类型
R - right type - 右类型
Record Components:
value - left value - 左值
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>
Left - Represents the left case (typically error) Left - 表示左情况(通常为错误)
Since:
JDK 25, opencode-base-core V1.0.3
Author:
Leon Soo www.LeonSoo.com
  • Nested Class Summary

    Nested classes/interfaces inherited from interface Either

    Either.Left<L,R>, Either.Right<L,R>
  • Constructor Summary

    Constructors
    Constructor
    Description
    Left(L value)
    Creates an instance of a Left record class.
  • Method Summary

    Modifier and Type
    Method
    Description
    <L2,R2> Either<L2,R2>
    bimap(Function<? super L, ? extends L2> leftMapper, Function<? super R, ? extends R2> rightMapper)
    Transform both sides 转换两侧的值
    final boolean
    Indicates whether some other object is "equal to" this one.
    <U> Either<L,U>
    flatMap(Function<? super R, Either<L,U>> mapper)
    Transform the Right value to another Either 将 Right 值转换为另一个 Either
    <T> T
    fold(Function<? super L, ? extends T> leftMapper, Function<? super R, ? extends T> rightMapper)
    Fold both cases to a single result 将两种情况折叠为单一结果
    Get the Left value if present 获取 Left 值(如果存在)
    getOrElse(R defaultValue)
    Get Right value or default if Left 获取 Right 值或默认值(如果是 Left)
    Get the Right value if present 获取 Right 值(如果存在)
    final int
    Returns a hash code value for this object.
    boolean
    Check if this is a Left 检查是否为 Left
    boolean
    Check if this is a Right 检查是否为 Right
    <U> Either<L,U>
    map(Function<? super R, ? extends U> mapper)
    Transform the Right value 转换 Right 值
    <U> Either<U,R>
    mapLeft(Function<? super L, ? extends U> mapper)
    Transform the Left value 转换 Left 值
    orElse(Either<L,R> other)
    Return this or other Either if Left 返回本 Either 或其他 Either(如果是 Left)
    peek(Consumer<? super R> action)
    Execute action on Right value 对 Right 值执行操作
    peekLeft(Consumer<? super L> action)
    Execute action on Left value 对 Left 值执行操作
    Convert to Stream.
    Swap Left and Right 交换 Left 和 Right
    Convert to Optional.
    Convert to Result.
    <X extends Throwable>
    Result<R>
    toResult(Function<L,X> leftToThrowable)
    Convert to Result with a type-safe left-to-throwable conversion function.
    Returns a string representation of this record class.
    Returns the value of the value record component.

    Methods inherited from class Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • 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

    • isLeft

      public boolean isLeft()
      Description copied from interface: Either
      Check if this is a Left 检查是否为 Left
      Specified by:
      isLeft in interface Either<L,R>
      Returns:
      true if Left - 如果是 Left 返回 true
    • isRight

      public boolean isRight()
      Description copied from interface: Either
      Check if this is a Right 检查是否为 Right
      Specified by:
      isRight in interface Either<L,R>
      Returns:
      true if Right - 如果是 Right 返回 true
    • getLeft

      public Optional<L> getLeft()
      Description copied from interface: Either
      Get the Left value if present 获取 Left 值(如果存在)
      Specified by:
      getLeft in interface Either<L,R>
      Returns:
      Optional containing Left value
    • getRight

      public Optional<R> getRight()
      Description copied from interface: Either
      Get the Right value if present 获取 Right 值(如果存在)
      Specified by:
      getRight in interface Either<L,R>
      Returns:
      Optional containing Right value
    • map

      public <U> Either<L,U> map(Function<? super R, ? extends U> mapper)
      Description copied from interface: Either
      Transform the Right value 转换 Right 值
      Specified by:
      map in interface Either<L,R>
      Type Parameters:
      U - result type - 结果类型
      Parameters:
      mapper - transformation function - 转换函数
      Returns:
      transformed Either
    • flatMap

      public <U> Either<L,U> flatMap(Function<? super R, Either<L,U>> mapper)
      Description copied from interface: Either
      Transform the Right value to another Either 将 Right 值转换为另一个 Either
      Specified by:
      flatMap in interface Either<L,R>
      Type Parameters:
      U - result type - 结果类型
      Parameters:
      mapper - transformation function returning Either - 返回 Either 的转换函数
      Returns:
      resulting Either
    • mapLeft

      public <U> Either<U,R> mapLeft(Function<? super L, ? extends U> mapper)
      Description copied from interface: Either
      Transform the Left value 转换 Left 值
      Specified by:
      mapLeft in interface Either<L,R>
      Type Parameters:
      U - result type - 结果类型
      Parameters:
      mapper - transformation function - 转换函数
      Returns:
      transformed Either
    • bimap

      public <L2,R2> Either<L2,R2> bimap(Function<? super L, ? extends L2> leftMapper, Function<? super R, ? extends R2> rightMapper)
      Description copied from interface: Either
      Transform both sides 转换两侧的值
      Specified by:
      bimap in interface Either<L,R>
      Type Parameters:
      L2 - new left type - 新的左类型
      R2 - new right type - 新的右类型
      Parameters:
      leftMapper - transformation for Left - Left 的转换函数
      rightMapper - transformation for Right - Right 的转换函数
      Returns:
      transformed Either
    • getOrElse

      public R getOrElse(R defaultValue)
      Description copied from interface: Either
      Get Right value or default if Left 获取 Right 值或默认值(如果是 Left)
      Specified by:
      getOrElse in interface Either<L,R>
      Parameters:
      defaultValue - default value - 默认值
      Returns:
      Right value or default
    • orElse

      public Either<L,R> orElse(Either<L,R> other)
      Description copied from interface: Either
      Return this or other Either if Left 返回本 Either 或其他 Either(如果是 Left)
      Specified by:
      orElse in interface Either<L,R>
      Parameters:
      other - alternative Either - 备选 Either
      Returns:
      this if Right, other if Left
    • fold

      public <T> T fold(Function<? super L, ? extends T> leftMapper, Function<? super R, ? extends T> rightMapper)
      Description copied from interface: Either
      Fold both cases to a single result 将两种情况折叠为单一结果
      Specified by:
      fold in interface Either<L,R>
      Type Parameters:
      T - result type - 结果类型
      Parameters:
      leftMapper - function for Left - Left 的函数
      rightMapper - function for Right - Right 的函数
      Returns:
      folded result
    • swap

      public Either<R,L> swap()
      Description copied from interface: Either
      Swap Left and Right 交换 Left 和 Right
      Specified by:
      swap in interface Either<L,R>
      Returns:
      swapped Either
    • peek

      public Either<L,R> peek(Consumer<? super R> action)
      Description copied from interface: Either
      Execute action on Right value 对 Right 值执行操作
      Specified by:
      peek in interface Either<L,R>
      Parameters:
      action - action to execute - 要执行的操作
      Returns:
      this Either for chaining
    • peekLeft

      public Either<L,R> peekLeft(Consumer<? super L> action)
      Description copied from interface: Either
      Execute action on Left value 对 Left 值执行操作
      Specified by:
      peekLeft in interface Either<L,R>
      Parameters:
      action - action to execute - 要执行的操作
      Returns:
      this Either for chaining
    • toOptional

      public Optional<R> toOptional()
      Description copied from interface: Either
      Convert to Optional. Right value becomes Optional.of(value), Left becomes Optional.empty(). 转换为 Optional。Right 值变为 Optional.of(value),Left 变为 Optional.empty()。
      Specified by:
      toOptional in interface Either<L,R>
      Returns:
      Optional containing the Right value, or empty if Left
    • stream

      public Stream<R> stream()
      Description copied from interface: Either
      Convert to Stream. Right value becomes a single-element Stream, Left becomes an empty Stream. 转换为 Stream。Right 值变为单元素 Stream,Left 变为空 Stream。
      Specified by:
      stream in interface Either<L,R>
      Returns:
      Stream containing the Right value, or empty if Left
    • toResult

      public Result<R> toResult()
      Description copied from interface: Either
      Convert to Result. Right becomes Result.success, Left(Throwable) becomes Result.failure, Left(other) becomes Result.failure(new OpenException(L.toString())). 转换为 Result。Right 变为 Result.success,Left(Throwable) 变为 Result.failure, Left(其他) 变为 Result.failure(new OpenException(L.toString()))。
      Specified by:
      toResult in interface Either<L,R>
      Returns:
      Result representation of this Either
    • toResult

      public <X extends Throwable> Result<R> toResult(Function<L,X> leftToThrowable)
      Description copied from interface: Either
      Convert to Result with a type-safe left-to-throwable conversion function. 使用类型安全的 left-to-throwable 转换函数转换为 Result。
      Specified by:
      toResult in interface Either<L,R>
      Type Parameters:
      X - the throwable type - 异常类型
      Parameters:
      leftToThrowable - function to convert Left value to a Throwable - 将 Left 值转换为 Throwable 的函数
      Returns:
      Result representation of this Either
    • toString

      public 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