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.google.common.collect.Lists;
33 * {@link State} that contains information about an arbitrary number of torrent
36 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
38 public class TorrentState extends AbstractState implements Iterable<TorrentFile> {
40 /** The torrent files. */
41 private final List<TorrentFile> files = Lists.newArrayList();
48 * Adds a torrent file to this state.
51 * The torrent file to add
54 public TorrentState addTorrentFile(TorrentFile torrentFile) {
55 files.add(torrentFile);
67 public Iterator<TorrentFile> iterator() {
68 return files.iterator();
79 public String toString() {
80 return String.format("%s[files=%s]", getClass().getSimpleName(), files);
84 * Container for torrent file data.
86 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
88 public static class TorrentFile {
90 /** The name of the file. */
91 private final String name;
93 /** The size of the file. */
94 private final String size;
96 /** The magnet URI of the file. */
97 private final String magnetUri;
99 /** The download URI of the file. */
100 private final String downloadUri;
102 /** The number of files in this torrent. */
103 private final int fileCount;
105 /** The number of seeds connected to this torrent. */
106 private final int seedCount;
108 /** The number of leechers connected to this torrent. */
109 private final int leechCount;
112 * Creates a new torrent file.
115 * The name of the file
117 * The size of the file
119 * The magnet URI of the file
121 * The download URI of the file
123 * The number of files
125 * The number of connected seeds
127 * The number of connected leechers
129 public TorrentFile(String name, String size, String magnetUri, String downloadUri, int fileCount, int seedCount, int leechCount) {
132 this.magnetUri = magnetUri;
133 this.downloadUri = downloadUri;
134 this.fileCount = fileCount;
135 this.seedCount = seedCount;
136 this.leechCount = leechCount;
144 * Returns the name of the file.
146 * @return The name of the file
148 public String name() {
153 * Returns the size of the file. The returned size may included
154 * non-numeric information, such as units (e. g. “860.46 MB”).
156 * @return The size of the file
158 public String size() {
163 * Returns the magnet URI of the file.
165 * @return The magnet URI of the file
167 public String magnetUri() {
172 * Returns the download URI of the file.
174 * @return The download URI of the file
176 public String downloadUri() {
181 * Returns the number of files in this torrent.
183 * @return The number of files in this torrent
185 public int fileCount() {
190 * Returns the number of seeds connected to this torrent.
192 * @return The number of connected seeds
194 public int seedCount() {
199 * Returns the number of leechers connected to this torrent.
201 * @return The number of connected leechers
203 public int leechCount() {
212 * Generates an ID for this file. If a {@link #magnetUri} is set, an ID
213 * is {@link #extractId(String) extracted} from it. Otherwise the magnet
214 * URI is used. If the {@link #magnetUri} is not set, the
215 * {@link #downloadUri} is used. If that is not set either, the name of
216 * the file is returned.
218 * @return The generated ID
220 private String generateId() {
221 if (magnetUri != null) {
222 String id = extractId(magnetUri);
228 return (downloadUri != null) ? downloadUri : name;
236 * Tries to extract the “exact target” of a magnet URI.
239 * The magnet URI to extract the “xt” from
240 * @return The extracted ID, or {@code null} if no ID could be found
242 private static String extractId(String magnetUri) {
243 List<NameValuePair> parameters = URLEncodedUtils.parse(magnetUri.substring("magnet:?".length()), Charset.forName("UTF-8"));
244 for (NameValuePair parameter : parameters) {
245 if (parameter.getName().equals("xt")) {
246 return parameter.getValue();
260 public int hashCode() {
261 return (generateId() != null) ? generateId().hashCode() : 0;
268 public boolean equals(Object object) {
269 if (!(object instanceof TorrentFile)) {
272 if (generateId() != null) {
273 return generateId().equals(((TorrentFile) object).generateId());
282 public String toString() {
283 return String.format("%s(%s,%s,%s)", name(), size(), magnetUri(), downloadUri());