Add constructors.
[rhynodge.git] / src / main / java / net / pterodactylus / rhynodge / states / TorrentState.java
1 /*
2  * Rhynodge - TorrentState.java - Copyright © 2013 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.rhynodge.states;
19
20 import java.nio.charset.Charset;
21 import java.util.Collection;
22 import java.util.Collections;
23 import java.util.Iterator;
24 import java.util.List;
25
26 import net.pterodactylus.rhynodge.State;
27 import net.pterodactylus.rhynodge.states.TorrentState.TorrentFile;
28
29 import org.apache.http.NameValuePair;
30 import org.apache.http.client.utils.URLEncodedUtils;
31
32 import com.fasterxml.jackson.annotation.JsonProperty;
33 import com.google.common.collect.Lists;
34
35 /**
36  * {@link State} that contains information about an arbitrary number of torrent
37  * files.
38  *
39  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
40  */
41 public class TorrentState extends AbstractState implements Iterable<TorrentFile> {
42
43         /** The torrent files. */
44         @JsonProperty
45         private List<TorrentFile> files = Lists.newArrayList();
46
47         /**
48          * Creates a new torrent state without torrent files.
49          */
50         public TorrentState() {
51                 this(Collections.<TorrentFile> emptySet());
52         }
53
54         /**
55          * Creates a new torrent state containing the given torrent files.
56          *
57          * @param torrentFiles
58          *            The torrent files
59          */
60         public TorrentState(Collection<TorrentFile> torrentFiles) {
61                 files.addAll(torrentFiles);
62         }
63
64         //
65         // ACCESSORS
66         //
67
68         /**
69          * Returns all torrent files of this state.
70          *
71          * @return All torrent files of this state
72          */
73         public Collection<TorrentFile> torrentFiles() {
74                 return Collections.unmodifiableList(files);
75         }
76
77         /**
78          * Adds a torrent file to this state.
79          *
80          * @param torrentFile
81          *            The torrent file to add
82          * @return This state
83          */
84         public TorrentState addTorrentFile(TorrentFile torrentFile) {
85                 files.add(torrentFile);
86                 return this;
87         }
88
89         //
90         // ITERABLE METHODS
91         //
92
93         /**
94          * {@inheritDoc}
95          */
96         @Override
97         public Iterator<TorrentFile> iterator() {
98                 return files.iterator();
99         }
100
101         //
102         // OBJECT METHODS
103         //
104
105         /**
106          * {@inheritDoc}
107          */
108         @Override
109         public String toString() {
110                 return String.format("%s[files=%s]", getClass().getSimpleName(), files);
111         }
112
113         /**
114          * Container for torrent file data.
115          *
116          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
117          */
118         public static class TorrentFile {
119
120                 /** The name of the file. */
121                 @JsonProperty
122                 private final String name;
123
124                 /** The size of the file. */
125                 @JsonProperty
126                 private final String size;
127
128                 /** The magnet URI of the file. */
129                 @JsonProperty
130                 private final String magnetUri;
131
132                 /** The download URI of the file. */
133                 @JsonProperty
134                 private final String downloadUri;
135
136                 /** The number of files in this torrent. */
137                 @JsonProperty
138                 private final int fileCount;
139
140                 /** The number of seeds connected to this torrent. */
141                 @JsonProperty
142                 private final int seedCount;
143
144                 /** The number of leechers connected to this torrent. */
145                 @JsonProperty
146                 private final int leechCount;
147
148                 /**
149                  * No-arg constructor for deserialization.
150                  */
151                 @SuppressWarnings("unused")
152                 private TorrentFile() {
153                         this(null, null, null, null, 0, 0, 0);
154                 }
155
156                 /**
157                  * Creates a new torrent file.
158                  *
159                  * @param name
160                  *            The name of the file
161                  * @param size
162                  *            The size of the file
163                  * @param magnetUri
164                  *            The magnet URI of the file
165                  * @param downloadUri
166                  *            The download URI of the file
167                  * @param fileCount
168                  *            The number of files
169                  * @param seedCount
170                  *            The number of connected seeds
171                  * @param leechCount
172                  *            The number of connected leechers
173                  */
174                 public TorrentFile(String name, String size, String magnetUri, String downloadUri, int fileCount, int seedCount, int leechCount) {
175                         this.name = name;
176                         this.size = size;
177                         this.magnetUri = magnetUri;
178                         this.downloadUri = downloadUri;
179                         this.fileCount = fileCount;
180                         this.seedCount = seedCount;
181                         this.leechCount = leechCount;
182                 }
183
184                 //
185                 // ACCESSORS
186                 //
187
188                 /**
189                  * Returns the name of the file.
190                  *
191                  * @return The name of the file
192                  */
193                 public String name() {
194                         return name;
195                 }
196
197                 /**
198                  * Returns the size of the file. The returned size may included
199                  * non-numeric information, such as units (e. g. “860.46 MB”).
200                  *
201                  * @return The size of the file
202                  */
203                 public String size() {
204                         return size;
205                 }
206
207                 /**
208                  * Returns the magnet URI of the file.
209                  *
210                  * @return The magnet URI of the file, or {@code null} if there is no
211                  *         magnet URI for this torrent file
212                  */
213                 public String magnetUri() {
214                         return magnetUri;
215                 }
216
217                 /**
218                  * Returns the download URI of the file.
219                  *
220                  * @return The download URI of the file, or {@code null} if there is no
221                  *         download URI for this torrent file
222                  */
223                 public String downloadUri() {
224                         return downloadUri;
225                 }
226
227                 /**
228                  * Returns the number of files in this torrent.
229                  *
230                  * @return The number of files in this torrent
231                  */
232                 public int fileCount() {
233                         return fileCount;
234                 }
235
236                 /**
237                  * Returns the number of seeds connected to this torrent.
238                  *
239                  * @return The number of connected seeds
240                  */
241                 public int seedCount() {
242                         return seedCount;
243                 }
244
245                 /**
246                  * Returns the number of leechers connected to this torrent.
247                  *
248                  * @return The number of connected leechers
249                  */
250                 public int leechCount() {
251                         return leechCount;
252                 }
253
254                 //
255                 // PRIVATE METHODS
256                 //
257
258                 /**
259                  * Generates an ID for this file. If a {@link #magnetUri} is set, an ID
260                  * is {@link #extractId(String) extracted} from it. Otherwise the magnet
261                  * URI is used. If the {@link #magnetUri} is not set, the
262                  * {@link #downloadUri} is used. If that is not set either, the name of
263                  * the file is returned.
264                  *
265                  * @return The generated ID
266                  */
267                 private String generateId() {
268                         if (magnetUri != null) {
269                                 String id = extractId(magnetUri);
270                                 if (id != null) {
271                                         return id;
272                                 }
273                                 return magnetUri;
274                         }
275                         return (downloadUri != null) ? downloadUri : name;
276                 }
277
278                 //
279                 // STATIC METHODS
280                 //
281
282                 /**
283                  * Tries to extract the “exact target” of a magnet URI.
284                  *
285                  * @param magnetUri
286                  *            The magnet URI to extract the “xt” from
287                  * @return The extracted ID, or {@code null} if no ID could be found
288                  */
289                 private static String extractId(String magnetUri) {
290                         List<NameValuePair> parameters = URLEncodedUtils.parse(magnetUri.substring("magnet:?".length()), Charset.forName("UTF-8"));
291                         for (NameValuePair parameter : parameters) {
292                                 if (parameter.getName().equals("xt")) {
293                                         return parameter.getValue().toLowerCase();
294                                 }
295                         }
296                         return null;
297                 }
298
299                 //
300                 // OBJECT METHODS
301                 //
302
303                 /**
304                  * {@inheritDoc}
305                  */
306                 @Override
307                 public int hashCode() {
308                         return (generateId() != null) ? generateId().hashCode() : 0;
309                 }
310
311                 /**
312                  * {@inheritDoc}
313                  */
314                 @Override
315                 public boolean equals(Object object) {
316                         if (!(object instanceof TorrentFile)) {
317                                 return false;
318                         }
319                         if (generateId() != null) {
320                                 return generateId().equals(((TorrentFile) object).generateId());
321                         }
322                         return false;
323                 }
324
325                 /**
326                  * {@inheritDoc}
327                  */
328                 @Override
329                 public String toString() {
330                         return String.format("%s(%s,%s,%s)", name(), size(), magnetUri(), downloadUri());
331                 }
332
333         }
334
335 }