Record Class Result.Failure<T>

java.lang.Object
java.lang.Record
cloud.opencode.base.core.result.Result.Failure<T>
Type Parameters:
T - value type - 值类型
Record Components:
cause - the failure cause (must not be null) - 失败原因(不能为 null)
All Implemented Interfaces:
Result<T>
Enclosing interface:
Result<T>

public static record Result.Failure<T>(Throwable cause) extends Record implements Result<T>
Failure - Represents a failed computation result Failure - 表示失败的计算结果
Since:
JDK 25, opencode-base-core V1.0.3
Author:
Leon Soo www.LeonSoo.com
  • Nested Class Summary

    Nested classes/interfaces inherited from interface Result

    Result.Failure<T>, Result.Success<T>
  • Constructor Summary

    Constructors
    Constructor
    Description
    Compact constructor enforcing non-null cause 紧凑构造器强制 cause 不为 null
  • Method Summary

    Modifier and Type
    Method
    Description
    Returns the value of the cause record component.
    final boolean
    Indicates whether some other object is "equal to" this one.
    <U> Result<U>
    flatMap(Function<? super T, Result<U>> mapper)
    Transform the success value to another Result.
    getOrElse(T defaultValue)
    Get the success value, or return default if Failure 获取成功值,如果是 Failure 返回默认值
    getOrElseGet(Supplier<? extends T> supplier)
    Get the success value, or compute default from Supplier if Failure 获取成功值,如果是 Failure 从 Supplier 计算默认值
    <X extends Throwable>
    T
    getOrElseThrow(Function<? super Throwable, ? extends X> exceptionMapper)
    Get the success value, or throw an exception mapped from the cause 获取成功值,或抛出从原因映射的异常
    final int
    Returns a hash code value for this object.
    boolean
    Check if this is a Failure 检查是否为失败
    boolean
    Check if this is a Success 检查是否为成功
    <U> Result<U>
    map(Function<? super T, ? extends U> mapper)
    Transform the success value.
    peek(Consumer<? super T> action)
    Execute action on Success value 对成功值执行操作
    peekFailure(Consumer<? super Throwable> action)
    Execute action on Failure cause 对失败原因执行操作
    recover(Function<? super Throwable, ? extends T> recoverer)
    Recover from a Failure by applying a function to the cause 通过对原因应用函数来从 Failure 恢复
    recoverWith(Function<? super Throwable, Result<T>> recoverer)
    Recover from a Failure by applying a function that returns a Result 通过应用返回 Result 的函数来从 Failure 恢复
    Convert to Stream.
    Convert to Optional.
    Returns string representation showing only class name and message (no stack trace for security).

    Methods inherited from class Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • Failure

      public Failure(Throwable cause)
      Compact constructor enforcing non-null cause 紧凑构造器强制 cause 不为 null
  • Method Details

    • isSuccess

      public boolean isSuccess()
      Description copied from interface: Result
      Check if this is a Success 检查是否为成功
      Specified by:
      isSuccess in interface Result<T>
      Returns:
      true if Success - 如果成功返回 true
    • isFailure

      public boolean isFailure()
      Description copied from interface: Result
      Check if this is a Failure 检查是否为失败
      Specified by:
      isFailure in interface Result<T>
      Returns:
      true if Failure - 如果失败返回 true
    • map

      public <U> Result<U> map(Function<? super T, ? extends U> mapper)
      Description copied from interface: Result
      Transform the success value. If the mapper throws, returns Failure with the caught exception. 转换成功值。如果映射函数抛出异常,返回包含捕获异常的 Failure。
      Specified by:
      map in interface Result<T>
      Type Parameters:
      U - result type - 结果类型
      Parameters:
      mapper - transformation function - 转换函数
      Returns:
      transformed Result
    • flatMap

      public <U> Result<U> flatMap(Function<? super T, Result<U>> mapper)
      Description copied from interface: Result
      Transform the success value to another Result. If the mapper throws, returns Failure. 将成功值转换为另一个 Result。如果映射函数抛出异常,返回 Failure。
      Specified by:
      flatMap in interface Result<T>
      Type Parameters:
      U - result type - 结果类型
      Parameters:
      mapper - transformation function returning Result - 返回 Result 的转换函数
      Returns:
      resulting Result
    • recover

      public Result<T> recover(Function<? super Throwable, ? extends T> recoverer)
      Description copied from interface: Result
      Recover from a Failure by applying a function to the cause 通过对原因应用函数来从 Failure 恢复
      Specified by:
      recover in interface Result<T>
      Parameters:
      recoverer - recovery function that takes the cause and returns a value - 接受原因并返回值的恢复函数
      Returns:
      Success with recovered value, or original Success
    • recoverWith

      public Result<T> recoverWith(Function<? super Throwable, Result<T>> recoverer)
      Description copied from interface: Result
      Recover from a Failure by applying a function that returns a Result 通过应用返回 Result 的函数来从 Failure 恢复
      Specified by:
      recoverWith in interface Result<T>
      Parameters:
      recoverer - recovery function that takes the cause and returns a Result - 接受原因并返回 Result 的恢复函数
      Returns:
      recovered Result, or original Success
    • peek

      public Result<T> peek(Consumer<? super T> action)
      Description copied from interface: Result
      Execute action on Success value 对成功值执行操作
      Specified by:
      peek in interface Result<T>
      Parameters:
      action - action to execute on success value - 对成功值执行的操作
      Returns:
      this Result for chaining
    • peekFailure

      public Result<T> peekFailure(Consumer<? super Throwable> action)
      Description copied from interface: Result
      Execute action on Failure cause 对失败原因执行操作
      Specified by:
      peekFailure in interface Result<T>
      Parameters:
      action - action to execute on failure cause - 对失败原因执行的操作
      Returns:
      this Result for chaining
    • getOrElse

      public T getOrElse(T defaultValue)
      Description copied from interface: Result
      Get the success value, or return default if Failure 获取成功值,如果是 Failure 返回默认值
      Specified by:
      getOrElse in interface Result<T>
      Parameters:
      defaultValue - default value - 默认值
      Returns:
      success value or default
    • getOrElseGet

      public T getOrElseGet(Supplier<? extends T> supplier)
      Description copied from interface: Result
      Get the success value, or compute default from Supplier if Failure 获取成功值,如果是 Failure 从 Supplier 计算默认值
      Specified by:
      getOrElseGet in interface Result<T>
      Parameters:
      supplier - default value supplier - 默认值供应商
      Returns:
      success value or computed default
    • getOrElseThrow

      public <X extends Throwable> T getOrElseThrow(Function<? super Throwable, ? extends X> exceptionMapper) throws X
      Description copied from interface: Result
      Get the success value, or throw an exception mapped from the cause 获取成功值,或抛出从原因映射的异常
      Specified by:
      getOrElseThrow in interface Result<T>
      Type Parameters:
      X - exception type - 异常类型
      Parameters:
      exceptionMapper - function to map cause to throwable - 将原因映射为异常的函数
      Returns:
      success value
      Throws:
      X - if this is a Failure
    • toOptional

      public Optional<T> toOptional()
      Description copied from interface: Result
      Convert to Optional. Success becomes Optional.of(value), Failure becomes Optional.empty(). 转换为 Optional。Success 变为 Optional.of(value),Failure 变为 Optional.empty()。
      Specified by:
      toOptional in interface Result<T>
      Returns:
      Optional containing the success value, or empty if Failure
    • stream

      public Stream<T> stream()
      Description copied from interface: Result
      Convert to Stream. Success becomes a single-element Stream, Failure becomes an empty Stream. 转换为 Stream。Success 变为单元素 Stream,Failure 变为空 Stream。
      Specified by:
      stream in interface Result<T>
      Returns:
      Stream containing the success value, or empty if Failure
    • toString

      public String toString()
      Returns string representation showing only class name and message (no stack trace for security). 返回仅显示类名和消息的字符串表示(出于安全考虑不包含堆栈跟踪)。
      Specified by:
      toString in class Record
      Returns:
      string representation
    • 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.
    • cause

      public Throwable cause()
      Returns the value of the cause record component.
      Returns:
      the value of the cause record component