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;
25 import java.util.Optional;
27 import net.pterodactylus.rhynodge.State;
28 import net.pterodactylus.rhynodge.states.TorrentState.TorrentFile;
30 import org.apache.http.NameValuePair;
31 import org.apache.http.client.utils.URLEncodedUtils;
33 import com.fasterxml.jackson.annotation.JsonProperty;
34 import com.google.common.collect.Lists;
37 * {@link State} that contains information about an arbitrary number of torrent
40 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
42 public class TorrentState extends AbstractState implements Iterable<TorrentFile> {
44 /** The torrent files. */
46 private List<TorrentFile> files = Lists.newArrayList();
49 * Creates a new torrent state without torrent files.
51 public TorrentState() {
52 this(Collections.<TorrentFile> emptySet());
56 * Creates a new torrent state containing the given torrent files.
61 public TorrentState(Collection<TorrentFile> torrentFiles) {
62 files.addAll(torrentFiles);
70 public boolean isEmpty() {
71 return files.isEmpty();
75 * Returns all torrent files of this state.
77 * @return All torrent files of this state
79 public Collection<TorrentFile> torrentFiles() {
80 return Collections.unmodifiableList(files);
84 * Adds a torrent file to this state.
87 * The torrent file to add
90 public TorrentState addTorrentFile(TorrentFile torrentFile) {
91 files.add(torrentFile);
103 public Iterator<TorrentFile> iterator() {
104 return files.iterator();
115 public String toString() {
116 return String.format("%s[files=%s]", getClass().getSimpleName(), files);
120 * Container for torrent file data.
122 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
124 public static class TorrentFile {
126 /** The name of the file. */
128 private final String name;
130 /** The size of the file. */
132 private final String size;
134 /** The magnet URI of the file. */
136 private final String magnetUri;
138 /** The download URI of the file. */
140 private final String downloadUri;
142 /** The number of files in this torrent. */
144 private final int fileCount;
146 /** The number of seeds connected to this torrent. */
148 private final int seedCount;
150 /** The number of leechers connected to this torrent. */
152 private final int leechCount;
155 * No-arg constructor for deserialization.
157 @SuppressWarnings("unused")
158 private TorrentFile() {
159 this(null, null, null, null, 0, 0, 0);
163 * Creates a new torrent file.
166 * The name of the file
168 * The size of the file
170 * The magnet URI of the file
172 * The download URI of the file
174 * The number of files
176 * The number of connected seeds
178 * The number of connected leechers
180 public TorrentFile(String name, String size, String magnetUri, String downloadUri, int fileCount, int seedCount, int leechCount) {
183 this.magnetUri = magnetUri;
184 this.downloadUri = downloadUri;
185 this.fileCount = fileCount;
186 this.seedCount = seedCount;
187 this.leechCount = leechCount;
195 * Returns the name of the file.
197 * @return The name of the file
199 public String name() {
204 * Returns the size of the file. The returned size may included
205 * non-numeric information, such as units (e. g. “860.46 MB”).
207 * @return The size of the file
209 public String size() {
214 * Returns the magnet URI of the file.
216 * @return The magnet URI of the file, or {@code null} if there is no
217 * magnet URI for this torrent file
219 public String magnetUri() {
224 * Returns the download URI of the file.
226 * @return The download URI of the file, or {@code null} if there is no
227 * download URI for this torrent file
229 public String downloadUri() {
234 * Returns the number of files in this torrent.
236 * @return The number of files in this torrent
238 public int fileCount() {
243 * Returns the number of seeds connected to this torrent.
245 * @return The number of connected seeds
247 public int seedCount() {
252 * Returns the number of leechers connected to this torrent.
254 * @return The number of connected leechers
256 public int leechCount() {
265 * Generates an ID for this file. If a {@link #magnetUri} is set, an ID
266 * is {@link #extractId(String) extracted} from it. Otherwise the magnet
267 * URI is used. If the {@link #magnetUri} is not set, the
268 * {@link #downloadUri} is used. If that is not set either, the name of
269 * the file is returned.
271 * @return The generated ID
273 private String generateId() {
274 if (magnetUri != null) {
275 return extractId(magnetUri).orElse(magnetUri);
277 return (downloadUri != null) ? downloadUri : name;
285 * Tries to extract the “exact target” of a magnet URI.
288 * The magnet URI to extract the “xt” from
289 * @return The extracted ID, or {@code null} if no ID could be found
291 private static Optional<String> extractId(String magnetUri) {
292 if ((magnetUri == null) || (magnetUri.length() < 8)) {
293 return Optional.empty();
295 List<NameValuePair> parameters = URLEncodedUtils.parse(magnetUri.substring("magnet:?".length()), Charset.forName("UTF-8"));
296 for (NameValuePair parameter : parameters) {
297 if (parameter.getName().equals("xt")) {
298 return Optional.of(parameter.getValue().toLowerCase());
301 return Optional.empty();
312 public int hashCode() {
313 return (generateId() != null) ? generateId().hashCode() : 0;
320 public boolean equals(Object object) {
321 if (!(object instanceof TorrentFile)) {
324 if (generateId() != null) {
325 return generateId().equals(((TorrentFile) object).generateId());
334 public String toString() {
335 return String.format("%s(%s,%s,%s)", name(), size(), magnetUri(), downloadUri());