Update license to GPLv3, fix header comments
[jFCPlib.git] / src / main / java / net / pterodactylus / fcp / highlevel / GetResult.java
1 /*
2  * jFCPlib - GetResult.java - Copyright © 2010–2016 David Roden
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 package net.pterodactylus.fcp.highlevel;
19
20 import java.io.InputStream;
21 import java.util.EventListener;
22
23 /**
24  * A get result encapsulates the result of {@link FcpClient#getURI(String)}. It
25  * is used to allow synchronous retrieval of a file without resorting to
26  * {@link EventListener} interfaces to notify the application of intermediary
27  * results, such as redirects.
28  *
29  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
30  */
31 public class GetResult {
32
33         /** Whether the request was successful. */
34         private boolean success;
35
36         /** The error code, if an error occured. */
37         private int errorCode;
38
39         /** The exception, if an exception occured. */
40         private Throwable exception;
41
42         /** The real URI, if a redirect was found. */
43         private String realUri;
44
45         /** The content type of the file. */
46         private String contentType;
47
48         /** The length of the file. */
49         private long contentLength;
50
51         /** An input stream containing the data of the file. */
52         private InputStream inputStream;
53
54         /**
55          * Returns whether the request was successful.
56          *
57          * @return {@code true} if the request was successful, {@code false}
58          *         otherwise
59          */
60         public boolean isSuccess() {
61                 return success;
62         }
63
64         /**
65          * Sets whether the request was successful.
66          *
67          * @param success
68          *            {@code true} if the request was successful, {@code false}
69          *            otherwise
70          * @return This result, to allow method chaning
71          */
72         GetResult success(boolean success) {
73                 this.success = success;
74                 return this;
75         }
76
77         /**
78          * Returns the error code of the request. The error code is the error code
79          * that is transferred in FCP’s “GetFailed” message. The error code is not
80          * valid if {@link #isSuccess()} is {@code true}. If an exception occured
81          * (i.e. if {@link #getException()} returns a non-{@code null} value) the
82          * error code might also be invalid.
83          *
84          * @return The error code of the request
85          */
86         public int getErrorCode() {
87                 return errorCode;
88         }
89
90         /**
91          * Sets the error code of the request.
92          *
93          * @param errorCode
94          *            The error code of the request
95          * @return This result, to allow method chaining
96          */
97         GetResult errorCode(int errorCode) {
98                 this.errorCode = errorCode;
99                 return this;
100         }
101
102         /**
103          * Returns the exception, if any occured.
104          *
105          * @return The occured exception, or {@code null} if there was no exception
106          */
107         public Throwable getException() {
108                 return exception;
109         }
110
111         /**
112          * Sets the exception that occured.
113          *
114          * @param exception
115          *            The occured exception
116          * @return This result, to allow method chaining
117          */
118         GetResult exception(Throwable exception) {
119                 this.exception = exception;
120                 return this;
121         }
122
123         /**
124          * Returns the real URI in case of a redirect.
125          *
126          * @return The real URI, or {@code null} if there was no redirect
127          */
128         public String getRealUri() {
129                 return realUri;
130         }
131
132         /**
133          * Sets the real URI in case of a redirect.
134          *
135          * @param realUri
136          *            The real URI
137          * @return This result, to allow method chaining
138          */
139         GetResult realUri(String realUri) {
140                 this.realUri = realUri;
141                 return this;
142         }
143
144         /**
145          * Returns the content type of the result.
146          *
147          * @return The content type of the result
148          */
149         public String getContentType() {
150                 return contentType;
151         }
152
153         /**
154          * Sets the content type of the result.
155          *
156          * @param contentType
157          *            The content type of the result
158          * @return This result, to allow method chaining
159          */
160         GetResult contentType(String contentType) {
161                 this.contentType = contentType;
162                 return this;
163         }
164
165         /**
166          * Returns the content length of the result.
167          *
168          * @return The content length of the result
169          */
170         public long getContentLength() {
171                 return contentLength;
172         }
173
174         /**
175          * Sets the content length of the result.
176          *
177          * @param contentLength
178          *            The content length of the result
179          * @return This result, to allow method chaining
180          */
181         GetResult contentLength(long contentLength) {
182                 this.contentLength = contentLength;
183                 return this;
184         }
185
186         /**
187          * Returns the data.
188          *
189          * @return The data
190          */
191         public InputStream getInputStream() {
192                 return inputStream;
193         }
194
195         /**
196          * Sets the input stream that will deliver the data.
197          *
198          * @param inputStream
199          *            The input stream containing the data
200          * @return This result, to allow method chaining
201          */
202         GetResult inputStream(InputStream inputStream) {
203                 this.inputStream = inputStream;
204                 return this;
205         }
206
207         //
208         // OBJECT METHODS
209         //
210
211         /**
212          * {@inheritDoc}
213          */
214         @Override
215         public String toString() {
216                 return getClass().getName() + "[success=" + success + ",errorCode=" + errorCode + ",exception=" + exception + ",realUri=" + realUri + ",contentType=" + contentType + ",contentLength=" + contentLength + ",inputStream=" + inputStream + "]";
217         }
218
219 }