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