2 * Reactor - TorrentState.java - Copyright © 2013 David Roden
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.
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.
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/>.
18 package net.pterodactylus.reactor.states;
20 import java.nio.charset.Charset;
21 import java.util.Iterator;
22 import java.util.List;
24 import net.pterodactylus.reactor.State;
25 import net.pterodactylus.reactor.states.TorrentState.TorrentFile;
27 import org.apache.http.NameValuePair;
28 import org.apache.http.client.utils.URLEncodedUtils;
30 import com.fasterxml.jackson.annotation.JsonProperty;
31 import com.google.common.collect.Lists;
34 * {@link State} that contains information about an arbitrary number of torrent
37 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
39 public class TorrentState extends AbstractState implements Iterable<TorrentFile> {
41 /** The torrent files. */
43 private List<TorrentFile> files = Lists.newArrayList();
50 * Adds a torrent file to this state.
53 * The torrent file to add
56 public TorrentState addTorrentFile(TorrentFile torrentFile) {
57 files.add(torrentFile);
69 public Iterator<TorrentFile> iterator() {
70 return files.iterator();
81 public String toString() {
82 return String.format("%s[files=%s]", getClass().getSimpleName(), files);
86 * Container for torrent file data.
88 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
90 public static class TorrentFile {
92 /** The name of the file. */
94 private final String name;
96 /** The size of the file. */
98 private final String size;
100 /** The magnet URI of the file. */
102 private final String magnetUri;
104 /** The download URI of the file. */
106 private final String downloadUri;
108 /** The number of files in this torrent. */
110 private final int fileCount;
112 /** The number of seeds connected to this torrent. */
114 private final int seedCount;
116 /** The number of leechers connected to this torrent. */
118 private final int leechCount;
121 * No-arg constructor for deserialization.
123 @SuppressWarnings("unused")
124 private TorrentFile() {
125 this(null, null, null, null, 0, 0, 0);
129 * Creates a new torrent file.
132 * The name of the file
134 * The size of the file
136 * The magnet URI of the file
138 * The download URI of the file
140 * The number of files
142 * The number of connected seeds
144 * The number of connected leechers
146 public TorrentFile(String name, String size, String magnetUri, String downloadUri, int fileCount, int seedCount, int leechCount) {
149 this.magnetUri = magnetUri;
150 this.downloadUri = downloadUri;
151 this.fileCount = fileCount;
152 this.seedCount = seedCount;
153 this.leechCount = leechCount;
161 * Returns the name of the file.
163 * @return The name of the file
165 public String name() {
170 * Returns the size of the file. The returned size may included
171 * non-numeric information, such as units (e. g. “860.46 MB”).
173 * @return The size of the file
175 public String size() {
180 * Returns the magnet URI of the file.
182 * @return The magnet URI of the file
184 public String magnetUri() {
189 * Returns the download URI of the file.
191 * @return The download URI of the file
193 public String downloadUri() {
198 * Returns the number of files in this torrent.
200 * @return The number of files in this torrent
202 public int fileCount() {
207 * Returns the number of seeds connected to this torrent.
209 * @return The number of connected seeds
211 public int seedCount() {
216 * Returns the number of leechers connected to this torrent.
218 * @return The number of connected leechers
220 public int leechCount() {
229 * Generates an ID for this file. If a {@link #magnetUri} is set, an ID
230 * is {@link #extractId(String) extracted} from it. Otherwise the magnet
231 * URI is used. If the {@link #magnetUri} is not set, the
232 * {@link #downloadUri} is used. If that is not set either, the name of
233 * the file is returned.
235 * @return The generated ID
237 private String generateId() {
238 if (magnetUri != null) {
239 String id = extractId(magnetUri);
245 return (downloadUri != null) ? downloadUri : name;
253 * Tries to extract the “exact target” of a magnet URI.
256 * The magnet URI to extract the “xt” from
257 * @return The extracted ID, or {@code null} if no ID could be found
259 private static String extractId(String magnetUri) {
260 List<NameValuePair> parameters = URLEncodedUtils.parse(magnetUri.substring("magnet:?".length()), Charset.forName("UTF-8"));
261 for (NameValuePair parameter : parameters) {
262 if (parameter.getName().equals("xt")) {
263 return parameter.getValue();
277 public int hashCode() {
278 return (generateId() != null) ? generateId().hashCode() : 0;
285 public boolean equals(Object object) {
286 if (!(object instanceof TorrentFile)) {
289 if (generateId() != null) {
290 return generateId().equals(((TorrentFile) object).generateId());
299 public String toString() {
300 return String.format("%s(%s,%s,%s)", name(), size(), magnetUri(), downloadUri());