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();
52 * Returns all torrent files of this state.
54 * @return All torrent files of this state
56 public Collection<TorrentFile> torrentFiles() {
57 return Collections.unmodifiableList(files);
61 * Adds a torrent file to this state.
64 * The torrent file to add
67 public TorrentState addTorrentFile(TorrentFile torrentFile) {
68 files.add(torrentFile);
80 public Iterator<TorrentFile> iterator() {
81 return files.iterator();
92 public String toString() {
93 return String.format("%s[files=%s]", getClass().getSimpleName(), files);
97 * Container for torrent file data.
99 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
101 public static class TorrentFile {
103 /** The name of the file. */
105 private final String name;
107 /** The size of the file. */
109 private final String size;
111 /** The magnet URI of the file. */
113 private final String magnetUri;
115 /** The download URI of the file. */
117 private final String downloadUri;
119 /** The number of files in this torrent. */
121 private final int fileCount;
123 /** The number of seeds connected to this torrent. */
125 private final int seedCount;
127 /** The number of leechers connected to this torrent. */
129 private final int leechCount;
132 * No-arg constructor for deserialization.
134 @SuppressWarnings("unused")
135 private TorrentFile() {
136 this(null, null, null, null, 0, 0, 0);
140 * Creates a new torrent file.
143 * The name of the file
145 * The size of the file
147 * The magnet URI of the file
149 * The download URI of the file
151 * The number of files
153 * The number of connected seeds
155 * The number of connected leechers
157 public TorrentFile(String name, String size, String magnetUri, String downloadUri, int fileCount, int seedCount, int leechCount) {
160 this.magnetUri = magnetUri;
161 this.downloadUri = downloadUri;
162 this.fileCount = fileCount;
163 this.seedCount = seedCount;
164 this.leechCount = leechCount;
172 * Returns the name of the file.
174 * @return The name of the file
176 public String name() {
181 * Returns the size of the file. The returned size may included
182 * non-numeric information, such as units (e. g. “860.46 MB”).
184 * @return The size of the file
186 public String size() {
191 * Returns the magnet URI of the file.
193 * @return The magnet URI of the file, or {@code null} if there is no
194 * magnet URI for this torrent file
196 public String magnetUri() {
201 * Returns the download URI of the file.
203 * @return The download URI of the file, or {@code null} if there is no
204 * download URI for this torrent file
206 public String downloadUri() {
211 * Returns the number of files in this torrent.
213 * @return The number of files in this torrent
215 public int fileCount() {
220 * Returns the number of seeds connected to this torrent.
222 * @return The number of connected seeds
224 public int seedCount() {
229 * Returns the number of leechers connected to this torrent.
231 * @return The number of connected leechers
233 public int leechCount() {
242 * Generates an ID for this file. If a {@link #magnetUri} is set, an ID
243 * is {@link #extractId(String) extracted} from it. Otherwise the magnet
244 * URI is used. If the {@link #magnetUri} is not set, the
245 * {@link #downloadUri} is used. If that is not set either, the name of
246 * the file is returned.
248 * @return The generated ID
250 private String generateId() {
251 if (magnetUri != null) {
252 String id = extractId(magnetUri);
258 return (downloadUri != null) ? downloadUri : name;
266 * Tries to extract the “exact target” of a magnet URI.
269 * The magnet URI to extract the “xt” from
270 * @return The extracted ID, or {@code null} if no ID could be found
272 private static String extractId(String magnetUri) {
273 List<NameValuePair> parameters = URLEncodedUtils.parse(magnetUri.substring("magnet:?".length()), Charset.forName("UTF-8"));
274 for (NameValuePair parameter : parameters) {
275 if (parameter.getName().equals("xt")) {
276 return parameter.getValue().toLowerCase();
290 public int hashCode() {
291 return (generateId() != null) ? generateId().hashCode() : 0;
298 public boolean equals(Object object) {
299 if (!(object instanceof TorrentFile)) {
302 if (generateId() != null) {
303 return generateId().equals(((TorrentFile) object).generateId());
312 public String toString() {
313 return String.format("%s(%s,%s,%s)", name(), size(), magnetUri(), downloadUri());