Record Class ProcessResult

java.lang.Object
java.lang.Record
cloud.opencode.base.core.process.ProcessResult
Record Components:
exitCode - the process exit code (0 typically means success) - 进程退出码(0 通常表示成功)
stdout - captured standard output - 捕获的标准输出
stderr - captured standard error - 捕获的标准错误
duration - wall-clock execution time - 挂钟执行时间
command - the command that was executed - 执行的命令

public record ProcessResult(int exitCode, String stdout, String stderr, Duration duration, List<String> command) extends Record
ProcessResult - Immutable result of a process execution ProcessResult - 进程执行的不可变结果

Captures the exit code, standard output, standard error, wall-clock duration, and the command that was executed.

捕获退出码、标准输出、标准错误、挂钟时间以及执行的命令。

Usage Examples | 使用示例:

ProcessResult result = ProcessManager.execute("ls", "-la");
if (result.isSuccess()) {
    System.out.println(result.stdout());
}

// Or throw on failure
ProcessManager.execute("git", "status").orThrow();

Thread Safety | 线程安全: This record is immutable and thread-safe. 此记录是不可变的,线程安全。

Since:
JDK 25, opencode-base-core V1.0.3
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Constructor Details

    • ProcessResult

      public ProcessResult(int exitCode, String stdout, String stderr, Duration duration, List<String> command)
      Compact constructor that enforces non-null invariants and defensive copies. 紧凑构造函数,确保非 null 不变量和防御性复制。
  • Method Details

    • of

      public static ProcessResult of(int exitCode, String stdout, String stderr, Duration duration, List<String> command)
      Creates a new ProcessResult. 创建新的 ProcessResult
      Parameters:
      exitCode - the exit code - 退出码
      stdout - standard output - 标准输出
      stderr - standard error - 标准错误
      duration - execution duration - 执行时间
      command - the command - 命令
      Returns:
      a new ProcessResult - 新的 ProcessResult
    • isSuccess

      public boolean isSuccess()
      Returns true if the process exited with code 0. 如果进程以退出码 0 退出,返回 true
      Returns:
      true if exit code is 0 - 退出码为 0 时返回 true
    • output

      public String output()
      Alias for stdout(). stdout() 的别名。
      Returns:
      the standard output - 标准输出
    • orThrow

      public ProcessResult orThrow()
      Returns this result if successful, otherwise throws an OpenException. 如果成功则返回此结果,否则抛出 OpenException

      The exception message includes the exit code, command, and a truncated stderr snippet. Callers should be cautious about exposing this exception to end users, as stderr may contain sensitive system information.

      异常消息包含退出码、命令和截断的 stderr 片段。 调用者应注意不要将此异常暴露给最终用户,因为 stderr 可能包含敏感系统信息。

      Returns:
      this result if exit code is 0 - 退出码为 0 时返回此结果
      Throws:
      OpenException - if exit code is not 0 - 退出码不为 0 时抛出
    • toString

      public final 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. Reference components are compared with Objects::equals(Object,Object); primitive components are compared with the compare method from their corresponding wrapper classes.
      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.
    • exitCode

      public int exitCode()
      Returns the value of the exitCode record component.
      Returns:
      the value of the exitCode record component
    • stdout

      public String stdout()
      Returns the value of the stdout record component.
      Returns:
      the value of the stdout record component
    • stderr

      public String stderr()
      Returns the value of the stderr record component.
      Returns:
      the value of the stderr record component
    • duration

      public Duration duration()
      Returns the value of the duration record component.
      Returns:
      the value of the duration record component
    • command

      public List<String> command()
      Returns the value of the command record component.
      Returns:
      the value of the command record component