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.Iterator;
22 import java.util.List;
24 import net.pterodactylus.rhynodge.State;
25 import net.pterodactylus.rhynodge.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, or {@code null} if there is no
183 * magnet URI for this torrent file
185 public String magnetUri() {
190 * Returns the download URI of the file.
192 * @return The download URI of the file, or {@code null} if there is no
193 * download URI for this torrent file
195 public String downloadUri() {
200 * Returns the number of files in this torrent.
202 * @return The number of files in this torrent
204 public int fileCount() {
209 * Returns the number of seeds connected to this torrent.
211 * @return The number of connected seeds
213 public int seedCount() {
218 * Returns the number of leechers connected to this torrent.
220 * @return The number of connected leechers
222 public int leechCount() {
231 * Generates an ID for this file. If a {@link #magnetUri} is set, an ID
232 * is {@link #extractId(String) extracted} from it. Otherwise the magnet
233 * URI is used. If the {@link #magnetUri} is not set, the
234 * {@link #downloadUri} is used. If that is not set either, the name of
235 * the file is returned.
237 * @return The generated ID
239 private String generateId() {
240 if (magnetUri != null) {
241 String id = extractId(magnetUri);
247 return (downloadUri != null) ? downloadUri : name;
255 * Tries to extract the âexact targetâ of a magnet URI.
258 * The magnet URI to extract the âxtâ from
259 * @return The extracted ID, or {@code null} if no ID could be found
261 private static String extractId(String magnetUri) {
262 List<NameValuePair> parameters = URLEncodedUtils.parse(magnetUri.substring("magnet:?".length()), Charset.forName("UTF-8"));
263 for (NameValuePair parameter : parameters) {
264 if (parameter.getName().equals("xt")) {
265 return parameter.getValue();
279 public int hashCode() {
280 return (generateId() != null) ? generateId().hashCode() : 0;
287 public boolean equals(Object object) {
288 if (!(object instanceof TorrentFile)) {
291 if (generateId() != null) {
292 return generateId().equals(((TorrentFile) object).generateId());
301 public String toString() {
302 return String.format("%s(%s,%s,%s)", name(), size(), magnetUri(), downloadUri());