From 9e329b575c9d3c8848858b2dd890232458f55a00 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Tue, 24 Sep 2013 22:06:35 +0200 Subject: [PATCH] List bots with active downloads last. --- .../net/pterodactylus/xdcc/ui/stdin/Result.java | 39 ++++++++++++++++++++-- .../pterodactylus/xdcc/ui/stdin/SearchCommand.java | 2 +- 2 files changed, 38 insertions(+), 3 deletions(-) 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..8532a79 100644 --- a/src/main/java/net/pterodactylus/xdcc/ui/stdin/Result.java +++ b/src/main/java/net/pterodactylus/xdcc/ui/stdin/Result.java @@ -17,14 +17,20 @@ 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.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.Function; import com.google.common.base.Predicate; import com.google.common.collect.ComparisonChain; @@ -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; } @@ -165,6 +199,7 @@ public class Result implements Comparable { return ComparisonChain.start() .compare(this, result, packArchiveComparator) .compare(this, result, botNameComparator) + .compare(this, result, botsWithRunningTransfersComparator) .compare(this, result, packNameComparator).result(); } diff --git a/src/main/java/net/pterodactylus/xdcc/ui/stdin/SearchCommand.java b/src/main/java/net/pterodactylus/xdcc/ui/stdin/SearchCommand.java index 9619f11..fb76f1f 100644 --- a/src/main/java/net/pterodactylus/xdcc/ui/stdin/SearchCommand.java +++ b/src/main/java/net/pterodactylus/xdcc/ui/stdin/SearchCommand.java @@ -82,7 +82,7 @@ public class SearchCommand implements Command { } } if (found) { - lastResult.add(new Result(bot, pack)); + lastResult.add(new Result(core, bot, pack)); } } } -- 2.7.4