View Javadoc
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  /**
13   * Describes the result of running an external process.
14   *
15   * @since 3.7
16   */
17  public class ProcessResult {
18  	/**
19  	 * Status of a process' execution.
20  	 */
21  	public enum Status {
22  		/**
23  		 * The script was found and launched properly. It may still have exited
24  		 * with a non-zero {@link #exitCode}.
25  		 */
26  		OK,
27  
28  		/** The script was not found on disk and thus could not be launched. */
29  		NOT_PRESENT,
30  
31  		/**
32  		 * The script was found but could not be launched since it was not
33  		 * supported by the current {@link FS}.
34  		 */
35  		NOT_SUPPORTED;
36  	}
37  
38  	/** The exit code of the process. */
39  	private final int exitCode;
40  
41  	/** Status of the process' execution. */
42  	private final Status status;
43  
44  	/**
45  	 * Instantiates a process result with the given status and an exit code of
46  	 * <code>-1</code>.
47  	 *
48  	 * @param status
49  	 *            Status describing the execution of the external process.
50  	 */
51  	public ProcessResult(Status status) {
52  		this(-1, status);
53  	}
54  
55  	/**
56  	 * <p>Constructor for ProcessResult.</p>
57  	 *
58  	 * @param exitCode
59  	 *            Exit code of the process.
60  	 * @param status
61  	 *            Status describing the execution of the external process.
62  	 */
63  	public ProcessResult(int exitCode, Status status) {
64  		this.exitCode = exitCode;
65  		this.status = status;
66  	}
67  
68  	/**
69  	 * Get exit code of the process.
70  	 *
71  	 * @return The exit code of the process.
72  	 */
73  	public int getExitCode() {
74  		return exitCode;
75  	}
76  
77  	/**
78  	 * Get the status of the process' execution.
79  	 *
80  	 * @return The status of the process' execution.
81  	 */
82  	public Status getStatus() {
83  		return status;
84  	}
85  
86  	/**
87  	 * Whether the execution occurred and resulted in an error
88  	 *
89  	 * @return <code>true</code> if the execution occurred and resulted in a
90  	 *         return code different from 0, <code>false</code> otherwise.
91  	 * @since 4.0
92  	 */
93  	public boolean isExecutedWithError() {
94  		return getStatus() == ProcessResult.Status.OK && getExitCode() != 0;
95  	}
96  }