X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fxdcc%2Fui%2Fstdin%2FResult.java;h=b09b0619328d746077de2ac54e124abb6299ea00;hb=5a7d1c53bac5edd913e483852758660665413ffa;hp=4dea6c55dfa39a470792ee0ba001f13150d57236;hpb=1ca482cdf4956eb7dcdcfd75c4d0412cf0261fdf;p=xudocci.git diff --git a/src/main/java/net/pterodactylus/xdcc/ui/stdin/Result.java b/src/main/java/net/pterodactylus/xdcc/ui/stdin/Result.java index 4dea6c5..b09b061 100644 --- a/src/main/java/net/pterodactylus/xdcc/ui/stdin/Result.java +++ b/src/main/java/net/pterodactylus/xdcc/ui/stdin/Result.java @@ -17,15 +17,21 @@ package net.pterodactylus.xdcc.ui.stdin; +import static com.google.common.collect.FluentIterable.from; + import java.util.Arrays; +import java.util.Collection; import java.util.Comparator; import java.util.List; +import java.util.function.Predicate; import java.util.regex.Pattern; +import net.pterodactylus.xdcc.core.Core; import net.pterodactylus.xdcc.data.Bot; +import net.pterodactylus.xdcc.data.Download; import net.pterodactylus.xdcc.data.Pack; -import com.google.common.base.Predicate; +import com.google.common.base.Function; import com.google.common.collect.ComparisonChain; /** @@ -42,7 +48,7 @@ public class Result implements Comparable { private final List archiveSuffixes = Arrays.asList("rar", "tar", "zip", "tar.gz", "tar.bz2", "tar.lzma", "7z"); @Override - public boolean apply(Result result) { + public boolean test(Result result) { for (String suffix : archiveSuffixes) { if (result.pack().name().toLowerCase().endsWith(suffix)) { return true; @@ -59,10 +65,10 @@ public class Result implements Comparable { private static final Comparator packArchiveComparator = new Comparator() { @Override public int compare(Result leftResult, Result rightResult) { - if (isArchive.apply(leftResult) && !isArchive.apply(rightResult)) { + if (isArchive.test(leftResult) && !isArchive.test(rightResult)) { return 1; } - if (!isArchive.apply(leftResult) && isArchive.apply(rightResult)) { + if (!isArchive.test(leftResult) && isArchive.test(rightResult)) { return -1; } return 0; @@ -115,6 +121,31 @@ public class Result implements Comparable { } }; + /** Comparator that sorts bots with running downloads to the back of the list. */ + private final Comparator botsWithRunningTransfersComparator = new Comparator() { + @Override + public int compare(Result leftResult, Result rightResult) { + Collection botsWithTransfers = from(core.downloads()).transform(new Function() { + @Override + public Bot apply(Download download) { + return download.bot(); + } + }).toSet(); + boolean leftDownloading = botsWithTransfers.contains(leftResult.bot()); + boolean rightDownloading = botsWithTransfers.contains(rightResult.bot()); + if (leftDownloading && !rightDownloading) { + return 1; + } + if (!leftDownloading && rightDownloading) { + return -1; + } + return 0; + } + }; + + /** The core. */ + private final Core core; + /** The bot carrying the pack. */ private final Bot bot; @@ -124,12 +155,15 @@ public class Result implements Comparable { /** * Creates a new result. * + * @param core + * The core * @param bot * The bot carrying the pack * @param pack - * The pack + * The pack of the result */ - Result(Bot bot, Pack pack) { + Result(Core core, Bot bot, Pack pack) { + this.core = core; this.bot = bot; this.pack = pack; } @@ -163,6 +197,7 @@ public class Result implements Comparable { @Override public int compareTo(Result result) { return ComparisonChain.start() + .compare(this, result, botsWithRunningTransfersComparator) .compare(this, result, packArchiveComparator) .compare(this, result, botNameComparator) .compare(this, result, packNameComparator).result();