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;
103 * Creates a new torrent file.
106 * The name of the file
108 * The size of the file
110 * The magnet URI of the file
112 * The download URI of the file
114 public TorrentFile(String name, String size, String magnetUri, String downloadUri) {
117 this.magnetUri = magnetUri;
118 this.downloadUri = downloadUri;
126 * Returns the name of the file.
128 * @return The name of the file
130 public String name() {
135 * Returns the size of the file. The returned size may included
136 * non-numeric information, such as units (e. g. “860.46 MB”).
138 * @return The size of the file
140 public String size() {
145 * Returns the magnet URI of the file.
147 * @return The magnet URI of the file
149 public String magnetUri() {
154 * Returns the download URI of the file.
156 * @return The download URI of the file
158 public String downloadUri() {
167 * Generates an ID for this file. If a {@link #magnetUri} is set, an ID
168 * is {@link #extractId(String) extracted} from it. Otherwise the magnet
169 * URI is used. If the {@link #magnetUri} is not set, the
170 * {@link #downloadUri} is used. If that is not set either, the name of
171 * the file is returned.
173 * @return The generated ID
175 private String generateId() {
176 if (magnetUri != null) {
177 String id = extractId(magnetUri);
183 return (downloadUri != null) ? downloadUri : name;
191 * Tries to extract the “exact target” of a magnet URI.
194 * The magnet URI to extract the “xt” from
195 * @return The extracted ID, or {@code null} if no ID could be found
197 private static String extractId(String magnetUri) {
198 List<NameValuePair> parameters = URLEncodedUtils.parse(magnetUri.substring("magnet:?".length()), Charset.forName("UTF-8"));
199 for (NameValuePair parameter : parameters) {
200 if (parameter.getName().equals("xt")) {
201 return parameter.getValue();
215 public int hashCode() {
216 return (generateId() != null) ? generateId().hashCode() : 0;
223 public boolean equals(Object object) {
224 if (!(object instanceof TorrentFile)) {
227 if (generateId() != null) {
228 return generateId().equals(((TorrentFile) object).generateId());
237 public String toString() {
238 return String.format("%s(%s,%s,%s)", name(), size(), magnetUri(), downloadUri());