X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fxdcc%2Fdata%2FDownload.java;h=9664f7c477834b9efdcf85cd55b98b2db5747a71;hb=d1fad6e229a7f6cc709ae2eaefe25394151436e2;hp=12b012947d79651d9b69f4a5cedac12c093e992c;hpb=99a4d802fbf7593c62c5cc36620e68cfc0b8c4b6;p=xudocci.git diff --git a/src/main/java/net/pterodactylus/xdcc/data/Download.java b/src/main/java/net/pterodactylus/xdcc/data/Download.java index 12b0129..9664f7c 100644 --- a/src/main/java/net/pterodactylus/xdcc/data/Download.java +++ b/src/main/java/net/pterodactylus/xdcc/data/Download.java @@ -21,11 +21,14 @@ import static com.google.common.base.Preconditions.checkNotNull; import java.io.OutputStream; import java.net.InetAddress; +import java.util.Comparator; import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicReference; import net.pterodactylus.irc.DccReceiver; +import com.google.common.base.Function; +import com.google.common.base.Optional; import com.google.common.base.Predicate; /** @@ -44,6 +47,40 @@ public class Download { } }; + /** {@link Comparator} that sorts downloads by their name. */ + public static final Comparator BY_NAME = new Comparator() { + + @Override + public int compare(Download leftDownload, Download rightDownload) { + return leftDownload.pack().name().compareToIgnoreCase(rightDownload.pack().name()); + } + }; + + /**  {@link Comparator} that sorts running downloads first. */ + public static final Comparator BY_RUNNING = new Comparator() { + + @Override + public int compare(Download leftDownload, Download rightDownload) { + return (leftDownload.dccReceiver() != null) ? -1 : (rightDownload.dccReceiver() != null) ? 1 : 0; + } + }; + + /** + * Converts a download into the number of seconds left in the transfer at the + * current rate. + */ + public static final Function> SECONDS_LEFT = new Function>() { + @Override + public Optional apply(Download download) { + DccReceiver dccReceiver = download.dccReceiver(); + if ((dccReceiver == null) || (dccReceiver.size() == -1) || (dccReceiver.currentRate() == 0)) { + return Optional.absent(); + } + long secondsLeft = (dccReceiver.size() - dccReceiver.progress()) / dccReceiver.currentRate(); + return Optional.of(secondsLeft); + } + }; + /** The bot that is being downloaded from. */ private final Bot bot;