Class ParallelResult<T>

java.lang.Object
cloud.opencode.base.parallel.ParallelResult<T>
Type Parameters:
T - the type of success results - 成功结果的类型

public final class ParallelResult<T> extends Object
Parallel Result - Encapsulates both success results and failure exceptions from parallel execution. 并行结果 - 封装并行执行的成功结果和失败异常。

This is an immutable container that holds the outcome of parallel task execution, separating successes from failures. It provides query methods to inspect the result and terminal operations to enforce success requirements.

这是一个不可变容器,保存并行任务执行的结果,将成功与失败分离。 提供查询方法检查结果,以及终端操作来强制要求成功。

Example | 示例:

ParallelResult<String> result = Futures.settleAll(futures).join();

if (result.hasFailures()) {
    log.warn("Failed: {}/{}", result.failureCount(), result.totalCount());
}

// Or throw if any failed
List<String> values = result.getOrThrow();

Security | 安全性:

  • Thread-safe: Yes (immutable after construction) - 线程安全: 是(构造后不可变)
  • Defensive copying: Uses List.copyOf() - 防御性复制: 使用 List.copyOf()
Since:
JDK 25, opencode-base-parallel V1.0.3
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • of

      public static <T> ParallelResult<T> of(List<T> successes, List<Throwable> failures)
      Creates a parallel result with both successes and failures. 创建包含成功和失败的并行结果。
      Type Parameters:
      T - the type of success results - 成功结果的类型
      Parameters:
      successes - the success results (must not be null) - 成功结果(不能为 null)
      failures - the failure exceptions (must not be null) - 失败异常(不能为 null)
      Returns:
      the parallel result - 并行结果
      Throws:
      NullPointerException - if successes or failures is null - 如果 successes 或 failures 为 null
    • allSucceeded

      public static <T> ParallelResult<T> allSucceeded(List<T> results)
      Creates a parallel result where all tasks succeeded. 创建所有任务都成功的并行结果。
      Type Parameters:
      T - the type of success results - 成功结果的类型
      Parameters:
      results - the success results (must not be null) - 成功结果(不能为 null)
      Returns:
      the parallel result with no failures - 没有失败的并行结果
      Throws:
      NullPointerException - if results is null - 如果 results 为 null
    • allFailed

      public static <T> ParallelResult<T> allFailed(List<Throwable> failures)
      Creates a parallel result where all tasks failed. 创建所有任务都失败的并行结果。
      Type Parameters:
      T - the type of success results - 成功结果的类型
      Parameters:
      failures - the failure exceptions (must not be null) - 失败异常(不能为 null)
      Returns:
      the parallel result with no successes - 没有成功的并行结果
      Throws:
      NullPointerException - if failures is null - 如果 failures 为 null
    • successes

      public List<T> successes()
      Gets the unmodifiable list of success results. 获取不可修改的成功结果列表。
      Returns:
      the success results - 成功结果
    • failures

      public List<Throwable> failures()
      Gets the unmodifiable list of failure exceptions. 获取不可修改的失败异常列表。
      Returns:
      the failure exceptions - 失败异常
    • hasFailures

      public boolean hasFailures()
      Returns true if there are any failures. 如果存在任何失败,返回 true
      Returns:
      true if at least one task failed - 如果至少一个任务失败则为 true
    • isAllSuccessful

      public boolean isAllSuccessful()
      Returns true if all tasks succeeded (no failures). 如果所有任务都成功(无失败),返回 true
      Returns:
      true if all tasks succeeded - 如果所有任务都成功则为 true
    • isAllFailed

      public boolean isAllFailed()
      Returns true if all tasks failed (no successes). 如果所有任务都失败(无成功),返回 true
      Returns:
      true if all tasks failed - 如果所有任务都失败则为 true
    • successCount

      public int successCount()
      Gets the number of successful tasks. 获取成功的任务数。
      Returns:
      the success count - 成功数
    • failureCount

      public int failureCount()
      Gets the number of failed tasks. 获取失败的任务数。
      Returns:
      the failure count - 失败数
    • totalCount

      public int totalCount()
      Gets the total number of tasks (successes + failures). 获取总任务数(成功数 + 失败数)。
      Returns:
      the total count - 总数
    • throwIfAnyFailed

      public void throwIfAnyFailed()
      Throws OpenParallelException if any task failed. 如果任何任务失败,抛出 OpenParallelException

      If there are no failures, this method does nothing.

      如果没有失败,此方法不执行任何操作。

      Throws:
      OpenParallelException - if at least one task failed, with aggregated failures - 如果至少一个任务失败,包含聚合的失败信息
    • throwIfAllFailed

      public void throwIfAllFailed()
      Throws OpenParallelException only if ALL tasks failed. 仅当所有任务都失败时抛出 OpenParallelException

      If at least one task succeeded, this method does nothing.

      如果至少一个任务成功,此方法不执行任何操作。

      Throws:
      OpenParallelException - if all tasks failed - 如果所有任务都失败
    • getOrThrow

      public List<T> getOrThrow()
      Returns the success results if no failures occurred, otherwise throws. 如果没有失败则返回成功结果,否则抛出异常。
      Returns:
      the success results - 成功结果
      Throws:
      OpenParallelException - if any task failed - 如果任何任务失败
    • toString

      public String toString()
      Returns a string representation of this parallel result. 返回此并行结果的字符串表示。
      Overrides:
      toString in class Object
      Returns:
      the string representation - 字符串表示