Show downloads sorted by name and status.
[xudocci.git] / src / main / java / net / pterodactylus / xdcc / ui / stdin / CommandReader.java
index 95425d0..5feabd7 100644 (file)
@@ -17,6 +17,9 @@
 
 package net.pterodactylus.xdcc.ui.stdin;
 
+import static net.pterodactylus.xdcc.data.Download.BY_NAME;
+import static net.pterodactylus.xdcc.data.Download.BY_RUNNING;
+
 import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.Reader;
@@ -43,7 +46,10 @@ import net.pterodactylus.xdcc.data.Download;
 import net.pterodactylus.xdcc.data.Pack;
 
 import com.google.common.base.Predicate;
+import com.google.common.collect.ComparisonChain;
+import com.google.common.collect.FluentIterable;
 import com.google.common.collect.Lists;
+import com.google.common.collect.Ordering;
 import com.google.common.collect.Sets;
 import com.google.common.eventbus.Subscribe;
 import com.google.common.primitives.Ints;
@@ -124,7 +130,7 @@ public class CommandReader extends AbstractExecutionThreadService {
                                writeLine("End of Search.");
                        } else if (words[0].equalsIgnoreCase("dcc")) {
                                int counter = 0;
-                               for (Download download : core.downloads()) {
+                               for (Download download : FluentIterable.from(core.downloads()).toSortedList(Ordering.from(BY_NAME).compound(BY_RUNNING))) {
                                        DccReceiver dccReceiver = download.dccReceiver();
                                        if (dccReceiver == null) {
                                                /* download has not even started. */
@@ -330,11 +336,28 @@ public class CommandReader extends AbstractExecutionThreadService {
                };
 
                /**
+                * {@link Comparator} for {@link Result}s that sorts archives (as per {@link
+                * #isArchive} to the back of the list.
+                */
+               private static final Comparator<Result> packArchiveComparator = new Comparator<Result>() {
+                       @Override
+                       public int compare(Result leftResult, Result rightResult) {
+                               if (isArchive.apply(leftResult) && !isArchive.apply(rightResult)) {
+                                       return 1;
+                               }
+                               if (!isArchive.apply(leftResult) && isArchive.apply(rightResult)) {
+                                       return -1;
+                               }
+                               return 0;
+                       }
+               };
+
+               /**
                 * {@link Comparator} for bot nicknames. It comprises different strategies:
                 * one name pattern is preferred (and thus listed first), one pattern is
                 * disliked (and thus listed last), the rest is sorted alphabetically.
                 */
-               private static final Comparator<String> botNameComparator = new Comparator<String>() {
+               private static final Comparator<Result> botNameComparator = new Comparator<Result>() {
 
                        /** Regular expression pattern for preferred names. */
                        private final Pattern preferredNames = Pattern.compile("(?i)[^\\w]EUR?[^\\w]");
@@ -343,7 +366,9 @@ public class CommandReader extends AbstractExecutionThreadService {
                        private final Pattern dislikedNames = Pattern.compile("(?i)[^\\w]USA?[^\\w]");
 
                        @Override
-                       public int compare(String leftBotName, String rightBotName) {
+                       public int compare(Result leftResult, Result rightResult) {
+                               String leftBotName = leftResult.bot().name();
+                               String rightBotName = rightResult.bot().name();
                                /* preferred names to the front! */
                                if (preferredNames.matcher(leftBotName).find() && !preferredNames.matcher(rightBotName).find()) {
                                        return -1;
@@ -358,8 +383,18 @@ public class CommandReader extends AbstractExecutionThreadService {
                                if (dislikedNames.matcher(rightBotName).find() && !dislikedNames.matcher(leftBotName).find()) {
                                        return -1;
                                }
-                               /* rest is sorted by name. */
-                               return leftBotName.compareToIgnoreCase(rightBotName);
+                               return 0;
+                       }
+               };
+
+               /**
+                * {@link Comparator} for {@link Result}s that sorts them by the name of the
+                * {@link Pack}.
+                */
+               private static final Comparator<Result> packNameComparator = new Comparator<Result>() {
+                       @Override
+                       public int compare(Result leftResult, Result rightResult) {
+                               return leftResult.pack().name().compareToIgnoreCase(rightResult.pack().name());
                        }
                };
 
@@ -410,14 +445,10 @@ public class CommandReader extends AbstractExecutionThreadService {
 
                @Override
                public int compareTo(Result result) {
-                       if (isArchive.apply(this) && !isArchive.apply(result)) {
-                               return 1;
-                       }
-                       if (!isArchive.apply(this) && isArchive.apply(result)) {
-                               return -1;
-                       }
-                       /* sort by bot name. */
-                       return botNameComparator.compare(bot().name(), result.bot().name());
+                       return ComparisonChain.start()
+                                       .compare(this, result, packArchiveComparator)
+                                       .compare(this, result, botNameComparator)
+                                       .compare(this, result, packNameComparator).result();
                }
 
        }