List bots with active downloads last.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 24 Sep 2013 20:06:35 +0000 (22:06 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 24 Sep 2013 20:06:35 +0000 (22:06 +0200)
src/main/java/net/pterodactylus/xdcc/ui/stdin/Result.java
src/main/java/net/pterodactylus/xdcc/ui/stdin/SearchCommand.java

index 4dea6c5..8532a79 100644 (file)
 
 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<Result> {
                }
        };
 
+       /** Comparator that sorts bots with running downloads to the back of the list. */
+       private final Comparator<Result> botsWithRunningTransfersComparator = new Comparator<Result>() {
+               @Override
+               public int compare(Result leftResult, Result rightResult) {
+                       Collection<Bot> botsWithTransfers = from(core.downloads()).transform(new Function<Download, Bot>() {
+                               @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<Result> {
        /**
         * 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<Result> {
                return ComparisonChain.start()
                                .compare(this, result, packArchiveComparator)
                                .compare(this, result, botNameComparator)
+                               .compare(this, result, botsWithRunningTransfersComparator)
                                .compare(this, result, packNameComparator).result();
        }
 
index 9619f11..fb76f1f 100644 (file)
@@ -82,7 +82,7 @@ public class SearchCommand implements Command {
                                        }
                                }
                                if (found) {
-                                       lastResult.add(new Result(bot, pack));
+                                       lastResult.add(new Result(core, bot, pack));
                                }
                        }
                }