ProcessResult.java

  1. /*
  2.  * Copyright (C) 2014 Obeo. and others
  3.  *
  4.  * This program and the accompanying materials are made available under the
  5.  * terms of the Eclipse Distribution License v. 1.0 which is available at
  6.  * https://www.eclipse.org/org/documents/edl-v10.php.
  7.  *
  8.  * SPDX-License-Identifier: BSD-3-Clause
  9.  */
  10. package org.eclipse.jgit.util;

  11. /**
  12.  * Describes the result of running an external process.
  13.  *
  14.  * @since 3.7
  15.  */
  16. public class ProcessResult {
  17.     /**
  18.      * Status of a process' execution.
  19.      */
  20.     public enum Status {
  21.         /**
  22.          * The script was found and launched properly. It may still have exited
  23.          * with a non-zero {@link #exitCode}.
  24.          */
  25.         OK,

  26.         /** The script was not found on disk and thus could not be launched. */
  27.         NOT_PRESENT,

  28.         /**
  29.          * The script was found but could not be launched since it was not
  30.          * supported by the current {@link FS}.
  31.          */
  32.         NOT_SUPPORTED;
  33.     }

  34.     /** The exit code of the process. */
  35.     private final int exitCode;

  36.     /** Status of the process' execution. */
  37.     private final Status status;

  38.     /**
  39.      * Instantiates a process result with the given status and an exit code of
  40.      * <code>-1</code>.
  41.      *
  42.      * @param status
  43.      *            Status describing the execution of the external process.
  44.      */
  45.     public ProcessResult(Status status) {
  46.         this(-1, status);
  47.     }

  48.     /**
  49.      * <p>Constructor for ProcessResult.</p>
  50.      *
  51.      * @param exitCode
  52.      *            Exit code of the process.
  53.      * @param status
  54.      *            Status describing the execution of the external process.
  55.      */
  56.     public ProcessResult(int exitCode, Status status) {
  57.         this.exitCode = exitCode;
  58.         this.status = status;
  59.     }

  60.     /**
  61.      * Get exit code of the process.
  62.      *
  63.      * @return The exit code of the process.
  64.      */
  65.     public int getExitCode() {
  66.         return exitCode;
  67.     }

  68.     /**
  69.      * Get the status of the process' execution.
  70.      *
  71.      * @return The status of the process' execution.
  72.      */
  73.     public Status getStatus() {
  74.         return status;
  75.     }

  76.     /**
  77.      * Whether the execution occurred and resulted in an error
  78.      *
  79.      * @return <code>true</code> if the execution occurred and resulted in a
  80.      *         return code different from 0, <code>false</code> otherwise.
  81.      * @since 4.0
  82.      */
  83.     public boolean isExecutedWithError() {
  84.         return getStatus() == ProcessResult.Status.OK && getExitCode() != 0;
  85.     }
  86. }