X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Freactor%2Fstates%2FTorrentState.java;h=9e9659d585e0ffd5ad13aac52d6877ea958cc81f;hb=e5a3f0f0500c9ef27e5c7c8726b73aa04c358e9b;hp=915559aa73bc0f9b67438cb5d486fe7a660a520a;hpb=d55738eb694404e3ddf671e8b5dbf3a5d06146aa;p=rhynodge.git diff --git a/src/main/java/net/pterodactylus/reactor/states/TorrentState.java b/src/main/java/net/pterodactylus/reactor/states/TorrentState.java index 915559a..9e9659d 100644 --- a/src/main/java/net/pterodactylus/reactor/states/TorrentState.java +++ b/src/main/java/net/pterodactylus/reactor/states/TorrentState.java @@ -17,12 +17,16 @@ package net.pterodactylus.reactor.states; +import java.nio.charset.Charset; import java.util.Iterator; import java.util.List; import net.pterodactylus.reactor.State; import net.pterodactylus.reactor.states.TorrentState.TorrentFile; +import org.apache.http.NameValuePair; +import org.apache.http.client.utils.URLEncodedUtils; + import com.google.common.collect.Lists; /** @@ -95,6 +99,15 @@ public class TorrentState extends AbstractState implements Iterable /** The download URI of the file. */ private final String downloadUri; + /** The number of files in this torrent. */ + private final int fileCount; + + /** The number of seeds connected to this torrent. */ + private final int seedCount; + + /** The number of leechers connected to this torrent. */ + private final int leechCount; + /** * Creates a new torrent file. * @@ -106,12 +119,21 @@ public class TorrentState extends AbstractState implements Iterable * The magnet URI of the file * @param downloadUri * The download URI of the file + * @param fileCount + * The number of files + * @param seedCount + * The number of connected seeds + * @param leechCount + * The number of connected leechers */ - public TorrentFile(String name, String size, String magnetUri, String downloadUri) { + public TorrentFile(String name, String size, String magnetUri, String downloadUri, int fileCount, int seedCount, int leechCount) { this.name = name; this.size = size; this.magnetUri = magnetUri; this.downloadUri = downloadUri; + this.fileCount = fileCount; + this.seedCount = seedCount; + this.leechCount = leechCount; } // @@ -155,6 +177,78 @@ public class TorrentState extends AbstractState implements Iterable return downloadUri; } + /** + * Returns the number of files in this torrent. + * + * @return The number of files in this torrent + */ + public int fileCount() { + return fileCount; + } + + /** + * Returns the number of seeds connected to this torrent. + * + * @return The number of connected seeds + */ + public int seedCount() { + return seedCount; + } + + /** + * Returns the number of leechers connected to this torrent. + * + * @return The number of connected leechers + */ + public int leechCount() { + return leechCount; + } + + // + // PRIVATE METHODS + // + + /** + * Generates an ID for this file. If a {@link #magnetUri} is set, an ID + * is {@link #extractId(String) extracted} from it. Otherwise the magnet + * URI is used. If the {@link #magnetUri} is not set, the + * {@link #downloadUri} is used. If that is not set either, the name of + * the file is returned. + * + * @return The generated ID + */ + private String generateId() { + if (magnetUri != null) { + String id = extractId(magnetUri); + if (id != null) { + return id; + } + return magnetUri; + } + return (downloadUri != null) ? downloadUri : name; + } + + // + // STATIC METHODS + // + + /** + * Tries to extract the “exact target” of a magnet URI. + * + * @param magnetUri + * The magnet URI to extract the “xt” from + * @return The extracted ID, or {@code null} if no ID could be found + */ + private static String extractId(String magnetUri) { + List parameters = URLEncodedUtils.parse(magnetUri.substring("magnet:?".length()), Charset.forName("UTF-8")); + for (NameValuePair parameter : parameters) { + if (parameter.getName().equals("xt")) { + return parameter.getValue(); + } + } + return null; + } + // // OBJECT METHODS // @@ -163,6 +257,28 @@ public class TorrentState extends AbstractState implements Iterable * {@inheritDoc} */ @Override + public int hashCode() { + return (generateId() != null) ? generateId().hashCode() : 0; + } + + /** + * {@inheritDoc} + */ + @Override + public boolean equals(Object object) { + if (!(object instanceof TorrentFile)) { + return false; + } + if (generateId() != null) { + return generateId().equals(((TorrentFile) object).generateId()); + } + return false; + } + + /** + * {@inheritDoc} + */ + @Override public String toString() { return String.format("%s(%s,%s,%s)", name(), size(), magnetUri(), downloadUri()); }