Update license to GPLv3, fix header comments
[jFCPlib.git] / src / main / java / net / pterodactylus / fcp / PersistentPutDir.java
1 /*
2  * jFCPlib - PersistentPutDir.java - Copyright © 2008–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;
19
20 /**
21  * A “PersistentPutDir” is the response to a {@link ClientPutDiskDir} message.
22  * It is also sent as a possible response to a {@link ListPersistentRequests}
23  * message.
24  *
25  * @author David ‘Bombe’ Roden &lt;bombe@freenetproject.org&gt;
26  */
27 public class PersistentPutDir extends BaseMessage implements Identifiable {
28
29         /**
30          * Creates a new “PersistentPutDir” message that wraps the received
31          * message.
32          *
33          * @param receivedMessage
34          *            The received message
35          */
36         public PersistentPutDir(FcpMessage receivedMessage) {
37                 super(receivedMessage);
38         }
39
40         /**
41          * Returns the identifier of the request.
42          *
43          * @return The identifier of the request
44          */
45         @Override
46         public String getIdentifier() {
47                 return getField("Identifier");
48         }
49
50         /**
51          * Returns the URI of the request.
52          *
53          * @return The URI of the request
54          */
55         public String getURI() {
56                 return getField("URI");
57         }
58
59         /**
60          * Returns the verbosity of the request.
61          *
62          * @return The verbosity of the request
63          */
64         public Verbosity getVerbosity() {
65                 return Verbosity.valueOf(getField("Verbosity"));
66         }
67
68         /**
69          * Returns the priority of the request.
70          *
71          * @return The priority of the request
72          */
73         public Priority getPriority() {
74                 return Priority.values()[FcpUtils.safeParseInt(getField("PriorityClass"), Priority.unknown.ordinal())];
75         }
76
77         /**
78          * Returns whether the request is on the global queue.
79          *
80          * @return <code>true</code> if the request is on the global queue,
81          *         <code>false</code> if it is on the client-local queue
82          */
83         public boolean isGlobal() {
84                 return Boolean.valueOf(getField("Global"));
85         }
86
87         /**
88          * Returns the maximum number of retries for failed blocks.
89          *
90          * @return The maximum number of retries, or <code>-1</code> for endless
91          *         retries, or <code>-2</code> if the number could not be parsed
92          */
93         public int getMaxRetries() {
94                 return FcpUtils.safeParseInt(getField("MaxRetries"), -2);
95         }
96
97         /**
98          * Returns the number of files in the request.
99          *
100          * @return The number of files in the request
101          */
102         public int getFileCount() {
103                 int fileCount = -1;
104                 while (getField("Files." + ++fileCount + ".UploadFrom") != null) {
105                         /* do nothing. */
106                 }
107                 return fileCount;
108         }
109
110         /**
111          * Returns the name of the file at the given index. The index is counted
112          * from <code>0</code>.
113          *
114          * @param fileIndex
115          *            The index of the file
116          * @return The name of the file at the given index
117          */
118         public String getFileName(int fileIndex) {
119                 return getField("Files." + fileIndex + ".Name");
120         }
121
122         /**
123          * Returns the length of the file at the given index. The index is counted
124          * from <code>0</code>.
125          *
126          * @param fileIndex
127          *            The index of the file
128          * @return The length of the file at the given index
129          */
130         public long getFileDataLength(int fileIndex) {
131                 return FcpUtils.safeParseLong(getField("Files." + fileIndex + ".DataLength"));
132         }
133
134         /**
135          * Returns the upload source of the file at the given index. The index is
136          * counted from <code>0</code>.
137          *
138          * @param fileIndex
139          *            The index of the file
140          * @return The upload source of the file at the given index
141          */
142         public UploadFrom getFileUploadFrom(int fileIndex) {
143                 return UploadFrom.valueOf(getField("Files." + fileIndex + ".UploadFrom"));
144         }
145
146         /**
147          * Returns the content type of the file at the given index. The index is
148          * counted from <code>0</code>.
149          *
150          * @param fileIndex
151          *            The index of the file
152          * @return The content type of the file at the given index
153          */
154         public String getFileMetadataContentType(int fileIndex) {
155                 return getField("Files." + fileIndex + ".Metadata.ContentType");
156         }
157
158         /**
159          * Returns the filename of the file at the given index. This value is only
160          * returned if {@link #getFileUploadFrom(int)} is returning
161          * {@link UploadFrom#disk}. The index is counted from <code>0</code>.
162          *
163          * @param fileIndex
164          *            The index of the file
165          * @return The filename of the file at the given index
166          */
167         public String getFileFilename(int fileIndex) {
168                 return getField("Files." + fileIndex + ".Filename");
169         }
170
171 }