2 * Rhynodge - 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.rhynodge.states;
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;
26 import net.pterodactylus.rhynodge.State;
27 import net.pterodactylus.rhynodge.states.TorrentState.TorrentFile;
29 import org.apache.http.NameValuePair;
30 import org.apache.http.client.utils.URLEncodedUtils;
32 import com.fasterxml.jackson.annotation.JsonProperty;
33 import com.google.common.collect.Lists;
36 * {@link State} that contains information about an arbitrary number of torrent
39 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
41 public class TorrentState extends AbstractState implements Iterable<TorrentFile> {
43 /** The torrent files. */
45 private List<TorrentFile> files = Lists.newArrayList();
48 * Creates a new torrent state without torrent files.
50 public TorrentState() {
51 this(Collections.<TorrentFile> emptySet());
55 * Creates a new torrent state containing the given torrent files.
60 public TorrentState(Collection<TorrentFile> torrentFiles) {
61 files.addAll(torrentFiles);
69 * Returns all torrent files of this state.
71 * @return All torrent files of this state
73 public Collection<TorrentFile> torrentFiles() {
74 return Collections.unmodifiableList(files);
78 * Adds a torrent file to this state.
81 * The torrent file to add
84 public TorrentState addTorrentFile(TorrentFile torrentFile) {
85 files.add(torrentFile);
97 public Iterator<TorrentFile> iterator() {
98 return files.iterator();
109 public String toString() {
110 return String.format("%s[files=%s]", getClass().getSimpleName(), files);
114 * Container for torrent file data.
116 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
118 public static class TorrentFile {
120 /** The name of the file. */
122 private final String name;
124 /** The size of the file. */
126 private final String size;
128 /** The magnet URI of the file. */
130 private final String magnetUri;
132 /** The download URI of the file. */
134 private final String downloadUri;
136 /** The number of files in this torrent. */
138 private final int fileCount;
140 /** The number of seeds connected to this torrent. */
142 private final int seedCount;
144 /** The number of leechers connected to this torrent. */
146 private final int leechCount;
149 * No-arg constructor for deserialization.
151 @SuppressWarnings("unused")
152 private TorrentFile() {
153 this(null, null, null, null, 0, 0, 0);
157 * Creates a new torrent file.
160 * The name of the file
162 * The size of the file
164 * The magnet URI of the file
166 * The download URI of the file
168 * The number of files
170 * The number of connected seeds
172 * The number of connected leechers
174 public TorrentFile(String name, String size, String magnetUri, String downloadUri, int fileCount, int seedCount, int leechCount) {
177 this.magnetUri = magnetUri;
178 this.downloadUri = downloadUri;
179 this.fileCount = fileCount;
180 this.seedCount = seedCount;
181 this.leechCount = leechCount;
189 * Returns the name of the file.
191 * @return The name of the file
193 public String name() {
198 * Returns the size of the file. The returned size may included
199 * non-numeric information, such as units (e. g. “860.46 MB”).
201 * @return The size of the file
203 public String size() {
208 * Returns the magnet URI of the file.
210 * @return The magnet URI of the file, or {@code null} if there is no
211 * magnet URI for this torrent file
213 public String magnetUri() {
218 * Returns the download URI of the file.
220 * @return The download URI of the file, or {@code null} if there is no
221 * download URI for this torrent file
223 public String downloadUri() {
228 * Returns the number of files in this torrent.
230 * @return The number of files in this torrent
232 public int fileCount() {
237 * Returns the number of seeds connected to this torrent.
239 * @return The number of connected seeds
241 public int seedCount() {
246 * Returns the number of leechers connected to this torrent.
248 * @return The number of connected leechers
250 public int leechCount() {
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.
265 * @return The generated ID
267 private String generateId() {
268 if (magnetUri != null) {
269 String id = extractId(magnetUri);
275 return (downloadUri != null) ? downloadUri : name;
283 * Tries to extract the “exact target” of a magnet URI.
286 * The magnet URI to extract the “xt” from
287 * @return The extracted ID, or {@code null} if no ID could be found
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();
307 public int hashCode() {
308 return (generateId() != null) ? generateId().hashCode() : 0;
315 public boolean equals(Object object) {
316 if (!(object instanceof TorrentFile)) {
319 if (generateId() != null) {
320 return generateId().equals(((TorrentFile) object).generateId());
329 public String toString() {
330 return String.format("%s(%s,%s,%s)", name(), size(), magnetUri(), downloadUri());