1 /*
2  * Copyright (C) 2010 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.android.tradefed.util;
17 
18 
19 /**
20  * Contains the result of a command.
21  */
22 public class CommandResult {
23 
24     private CommandStatus mCmdStatus = CommandStatus.TIMED_OUT;
25     private String mStdout = null;
26     private String mStderr = null;
27     private Integer mExitCode = null;
28     private boolean mCached = false;
29 
30     /**
31      * Create a {@link CommandResult} with the default {@link CommandStatus#TIMED_OUT} status.
32      */
CommandResult()33     public CommandResult() {
34     }
35 
36     /**
37      * Create a {@link CommandResult} with the given status.
38      *
39      * @param status the {@link CommandStatus}
40      */
CommandResult(CommandStatus status)41     public CommandResult(CommandStatus status) {
42         mCmdStatus = status;
43     }
44 
45     /**
46      * Get status of command.
47      *
48      * @return the {@link CommandStatus}
49      */
getStatus()50     public CommandStatus getStatus() {
51         return mCmdStatus;
52     }
53 
setStatus(CommandStatus status)54     public void setStatus(CommandStatus status) {
55         mCmdStatus = status;
56     }
57 
58     /** Returns whether this result is a cached result or not. */
isCached()59     public boolean isCached() {
60         return mCached;
61     }
62 
setCached(boolean cached)63     public void setCached(boolean cached) {
64         mCached = cached;
65     }
66 
67     /**
68      * Get the standard output produced by command.
69      *
70      * @return the standard output or <code>null</code> if output could not be retrieved
71      */
getStdout()72     public String getStdout() {
73         return mStdout;
74     }
75 
setStdout(String stdout)76     public void setStdout(String stdout) {
77         mStdout = stdout;
78     }
79 
80     /**
81      * Get the standard error output produced by command.
82      *
83      * @return the standard error or <code>null</code> if output could not be retrieved
84      */
getStderr()85     public String getStderr() {
86         return mStderr;
87     }
88 
setStderr(String stderr)89     public void setStderr(String stderr) {
90         mStderr = stderr;
91     }
92 
93     /**
94      * Get the exit/return code produced by command.
95      *
96      * @return the exit code or <code>null</code> if it is unset
97      */
getExitCode()98     public Integer getExitCode() {
99         return mExitCode;
100     }
101 
setExitCode(int exitCode)102     public void setExitCode(int exitCode) {
103         mExitCode = exitCode;
104     }
105 
106     /** Returns a string representation of this object. Stdout/err can be very large. */
107     @Override
toString()108     public String toString() {
109         return String.format(
110                 "CommandResult: exit code=%d, out=%s, err=%s", mExitCode, mStdout, mStderr);
111     }
112 }
113