Class TaskResult<T>

java.lang.Object
cloud.opencode.base.parallel.structured.TaskResult<T>
Type Parameters:
T - the result type - 结果类型

public final class TaskResult<T> extends Object
Task Result - Structured Task Result Wrapper 任务结果 - 结构化任务结果包装器

Represents the result of a structured task execution, containing either a success value or a failure exception.

表示结构化任务执行的结果,包含成功值或失败异常。

Example | 示例:

TaskResult<String> result = TaskResult.success("data");
result.ifSuccess(data -> process(data))
      .ifFailure(ex -> log.error("Failed", ex));

String value = result.getOrDefault("fallback");

Features | 主要功能:

  • Success/Failure/Cancelled states - 成功/失败/取消状态
  • Functional transformation (map, flatMap, recover) - 函数式转换
  • Side effect handling (ifSuccess, ifFailure) - 副作用处理
  • Optional and default value access - Optional和默认值访问

Security | 安全性:

  • Thread-safe: Yes (immutable after construction) - 线程安全: 是(构造后不可变)
Since:
JDK 25, opencode-base-parallel V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • success

      public static <T> TaskResult<T> success(T value)
      Creates a successful result. 创建成功结果。
      Type Parameters:
      T - the result type - 结果类型
      Parameters:
      value - the value - 值
      Returns:
      the result - 结果
    • failure

      public static <T> TaskResult<T> failure(Throwable exception)
      Creates a failed result. 创建失败结果。
      Type Parameters:
      T - the result type - 结果类型
      Parameters:
      exception - the exception - 异常
      Returns:
      the result - 结果
    • cancelled

      public static <T> TaskResult<T> cancelled()
      Creates a cancelled result. 创建取消结果。
      Type Parameters:
      T - the result type - 结果类型
      Returns:
      the result - 结果
    • of

      public static <T> TaskResult<T> of(Callable<T> callable)
      Creates a result from a callable. 从 Callable 创建结果。
      Type Parameters:
      T - the result type - 结果类型
      Parameters:
      callable - the callable - Callable
      Returns:
      the result - 结果
    • isSuccess

      public boolean isSuccess()
      Checks if the task succeeded. 检查任务是否成功。
      Returns:
      true if success - 如果成功返回 true
    • isFailure

      public boolean isFailure()
      Checks if the task failed. 检查任务是否失败。
      Returns:
      true if failure - 如果失败返回 true
    • isCancelled

      public boolean isCancelled()
      Checks if the task was cancelled. 检查任务是否被取消。
      Returns:
      true if cancelled - 如果取消返回 true
    • getState

      public TaskResult.State getState()
      Gets the state. 获取状态。
      Returns:
      the state - 状态
    • get

      public T get()
      Gets the value, throwing if failed. 获取值,如果失败则抛出异常。
      Returns:
      the value - 值
      Throws:
      IllegalStateException - if not success - 如果不是成功状态
    • getOrNull

      public T getOrNull()
      Gets the value or null. 获取值或 null。
      Returns:
      the value or null - 值或 null
    • getOrDefault

      public T getOrDefault(T defaultValue)
      Gets the value or default. 获取值或默认值。
      Parameters:
      defaultValue - the default value - 默认值
      Returns:
      the value or default - 值或默认值
    • getOrElse

      public T getOrElse(Function<Throwable, T> mapper)
      Gets the value or computes from exception. 获取值或从异常计算。
      Parameters:
      mapper - the exception mapper - 异常映射器
      Returns:
      the value or mapped - 值或映射结果
    • toOptional

      public Optional<T> toOptional()
      Gets the value as Optional. 获取值为 Optional。
      Returns:
      the optional value - Optional 值
    • getException

      public Throwable getException()
      Gets the exception. 获取异常。
      Returns:
      the exception or null - 异常或 null
    • getExceptionOptional

      public Optional<Throwable> getExceptionOptional()
      Gets the exception as Optional. 获取异常为 Optional。
      Returns:
      the optional exception - Optional 异常
    • map

      public <R> TaskResult<R> map(Function<T,R> mapper)
      Maps the success value. 映射成功值。
      Type Parameters:
      R - the result type - 结果类型
      Parameters:
      mapper - the mapper function - 映射函数
      Returns:
      the mapped result - 映射结果
    • flatMap

      public <R> TaskResult<R> flatMap(Function<T, TaskResult<R>> mapper)
      Flat maps the success value. 扁平映射成功值。
      Type Parameters:
      R - the result type - 结果类型
      Parameters:
      mapper - the mapper function - 映射函数
      Returns:
      the flat mapped result - 扁平映射结果
    • recover

      public TaskResult<T> recover(Function<Throwable, T> recovery)
      Recovers from failure. 从失败中恢复。
      Parameters:
      recovery - the recovery function - 恢复函数
      Returns:
      the recovered result - 恢复结果
    • ifSuccess

      public TaskResult<T> ifSuccess(Consumer<T> action)
      Executes action if success. 如果成功执行动作。
      Parameters:
      action - the action - 动作
      Returns:
      this result - 此结果
    • ifFailure

      public TaskResult<T> ifFailure(Consumer<Throwable> action)
      Executes action if failure. 如果失败执行动作。
      Parameters:
      action - the action - 动作
      Returns:
      this result - 此结果
    • ifCancelled

      public TaskResult<T> ifCancelled(Runnable action)
      Executes action if cancelled. 如果取消执行动作。
      Parameters:
      action - the action - 动作
      Returns:
      this result - 此结果
    • 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