Class Try.Failure<T>

java.lang.Object
cloud.opencode.base.functional.monad.Try.Failure<T>
Type Parameters:
T - value type - 值类型
All Implemented Interfaces:
Try<T>
Enclosing interface:
Try<T>

public static final class Try.Failure<T> extends Object implements Try<T>
Failure - Represents a failed computation Failure - 表示失败的计算
Since:
JDK 25, opencode-base-functional V1.0.0
Author:
Leon Soo www.LeonSoo.com
  • Constructor Details

  • Method Details

    • cause

      public Throwable cause()
      Get the exception 获取异常
      Returns:
      the exception - 异常
    • isSuccess

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

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

      public T get()
      Description copied from interface: Try
      Get the value, throws if failure 获取值,失败时抛出异常
      Specified by:
      get in interface Try<T>
      Returns:
      the value - 值
    • getCause

      public Optional<Throwable> getCause()
      Description copied from interface: Try
      Get the exception if this is a Failure 获取异常(如果失败)
      Specified by:
      getCause in interface Try<T>
      Returns:
      Optional containing the exception, empty if Success
    • map

      public <U> Try<U> map(Function<? super T, ? extends U> mapper)
      Description copied from interface: Try
      Transform the value if Success 如果成功则转换值
      Specified by:
      map in interface Try<T>
      Type Parameters:
      U - result type - 结果类型
      Parameters:
      mapper - transformation function - 转换函数
      Returns:
      transformed Try
    • flatMap

      public <U> Try<U> flatMap(Function<? super T, Try<U>> mapper)
      Description copied from interface: Try
      Transform the value to another Try if Success 如果成功则转换为另一个 Try
      Specified by:
      flatMap in interface Try<T>
      Type Parameters:
      U - result type - 结果类型
      Parameters:
      mapper - transformation function returning Try - 返回 Try 的转换函数
      Returns:
      resulting Try
    • filter

      public Try<T> filter(Predicate<? super T> predicate)
      Description copied from interface: Try
      Filter the value with a predicate 使用谓词过滤值
      Specified by:
      filter in interface Try<T>
      Parameters:
      predicate - filter condition - 过滤条件
      Returns:
      filtered Try (Failure if predicate not satisfied)
    • mapFailure

      public Try<T> mapFailure(Function<Throwable, ? extends Throwable> mapper)
      Description copied from interface: Try
      Transform the exception if this is a Failure. If Success, return this unchanged. 如果是 Failure 则转换异常。如果是 Success 则原样返回。

      If the mapper itself throws, returns a new Try.Failure wrapping the mapper's exception, with the original cause added as a suppressed exception (mirroring try-with-resources semantics).

      如果 mapper 本身抛出异常,返回包装 mapper 异常的新 Try.Failure, 原始 cause 作为 suppressed 异常附加(类似 try-with-resources 语义)。

      Specified by:
      mapFailure in interface Try<T>
      Parameters:
      mapper - function to transform the exception - 转换异常的函数
      Returns:
      Try with mapped exception if Failure, or this if Success | 如果是 Failure 返回转换后的异常,否则返回此 Try
    • getOrElse

      public T getOrElse(T defaultValue)
      Description copied from interface: Try
      Get value or default if Failure 获取值或默认值(如果失败)
      Specified by:
      getOrElse in interface Try<T>
      Parameters:
      defaultValue - default value to use on failure - 失败时使用的默认值
      Returns:
      value or default
    • orElse

      public Try<T> orElse(Try<T> other)
      Description copied from interface: Try
      Return this or other Try if Failure 返回本 Try 或其他 Try(如果失败)
      Specified by:
      orElse in interface Try<T>
      Parameters:
      other - alternative Try - 备选 Try
      Returns:
      this if Success, other if Failure
    • recover

      public Try<T> recover(Function<Throwable, T> recovery)
      Description copied from interface: Try
      Recover from Failure with a function 使用函数从失败恢复
      Specified by:
      recover in interface Try<T>
      Parameters:
      recovery - recovery function - 恢复函数
      Returns:
      recovered Try
    • recoverWith

      public Try<T> recoverWith(Function<Throwable, Try<T>> recovery)
      Description copied from interface: Try
      Recover from Failure with a function returning Try 使用返回 Try 的函数从失败恢复
      Specified by:
      recoverWith in interface Try<T>
      Parameters:
      recovery - recovery function returning Try - 返回 Try 的恢复函数
      Returns:
      recovered Try
    • toOptional

      public Optional<T> toOptional()
      Description copied from interface: Try
      Convert to Optional (empty if Failure) 转换为 Optional(失败时为空)
      Specified by:
      toOptional in interface Try<T>
      Returns:
      Optional containing value or empty
    • toEither

      public Either<Throwable, T> toEither()
      Description copied from interface: Try
      Convert to Either (Left=exception, Right=value) 转换为 Either(Left=异常,Right=值)
      Specified by:
      toEither in interface Try<T>
      Returns:
      Either with exception or value
    • peek

      public Try<T> peek(Consumer<? super T> action)
      Description copied from interface: Try
      Execute action on the value if Success 如果成功则对值执行操作
      Specified by:
      peek in interface Try<T>
      Parameters:
      action - action to execute - 要执行的操作
      Returns:
      this Try for chaining
    • onFailure

      public Try<T> onFailure(Consumer<Throwable> action)
      Description copied from interface: Try
      Execute action on failure 失败时执行操作
      Specified by:
      onFailure in interface Try<T>
      Parameters:
      action - action to execute on exception - 对异常执行的操作
      Returns:
      this Try for chaining
    • onSuccess

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

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object