Split command reader into separate commands.
[xudocci.git] / src / main / java / net / pterodactylus / xdcc / ui / stdin / Result.java
diff --git a/src/main/java/net/pterodactylus/xdcc/ui/stdin/Result.java b/src/main/java/net/pterodactylus/xdcc/ui/stdin/Result.java
new file mode 100644 (file)
index 0000000..4dea6c5
--- /dev/null
@@ -0,0 +1,171 @@
+/*
+ * XdccDownloader - Result.java - Copyright © 2013 David Roden
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package net.pterodactylus.xdcc.ui.stdin;
+
+import java.util.Arrays;
+import java.util.Comparator;
+import java.util.List;
+import java.util.regex.Pattern;
+
+import net.pterodactylus.xdcc.data.Bot;
+import net.pterodactylus.xdcc.data.Pack;
+
+import com.google.common.base.Predicate;
+import com.google.common.collect.ComparisonChain;
+
+/**
+ * Container for result information.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class Result implements Comparable<Result> {
+
+       /** {@link Predicate} that matches {@link Result}s that contain an archive. */
+       private static final Predicate<Result> isArchive = new Predicate<Result>() {
+
+               /** All suffixes that are recognized as archives. */
+               private final List<String> archiveSuffixes = Arrays.asList("rar", "tar", "zip", "tar.gz", "tar.bz2", "tar.lzma", "7z");
+
+               @Override
+               public boolean apply(Result result) {
+                       for (String suffix : archiveSuffixes) {
+                               if (result.pack().name().toLowerCase().endsWith(suffix)) {
+                                       return true;
+                               }
+                       }
+                       return false;
+               }
+       };
+
+       /**
+        * {@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<Result> botNameComparator = new Comparator<Result>() {
+
+               /** Regular expression pattern for preferred names. */
+               private final Pattern preferredNames = Pattern.compile("(?i)[^\\w]EUR?[^\\w]");
+
+               /** Regular expression pattern for disliked names. */
+               private final Pattern dislikedNames = Pattern.compile("(?i)[^\\w]USA?[^\\w]");
+
+               @Override
+               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;
+                       }
+                       if (preferredNames.matcher(rightBotName).find() && !preferredNames.matcher(leftBotName).find()) {
+                               return 1;
+                       }
+                       /* disliked names to the back. */
+                       if (dislikedNames.matcher(leftBotName).find() && !dislikedNames.matcher(rightBotName).find()) {
+                               return 1;
+                       }
+                       if (dislikedNames.matcher(rightBotName).find() && !dislikedNames.matcher(leftBotName).find()) {
+                               return -1;
+                       }
+                       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());
+               }
+       };
+
+       /** The bot carrying the pack. */
+       private final Bot bot;
+
+       /** The pack. */
+       private final Pack pack;
+
+       /**
+        * Creates a new result.
+        *
+        * @param bot
+        *              The bot carrying the pack
+        * @param pack
+        *              The pack
+        */
+       Result(Bot bot, Pack pack) {
+               this.bot = bot;
+               this.pack = pack;
+       }
+
+       //
+       // ACCESSORS
+       //
+
+       /**
+        * Returns the bot carrying the pack.
+        *
+        * @return The bot carrying the pack
+        */
+       public Bot bot() {
+               return bot;
+       }
+
+       /**
+        * Returns the pack.
+        *
+        * @return The pack
+        */
+       public Pack pack() {
+               return pack;
+       }
+
+       //
+       // COMPARABLE METHODS
+       //
+
+       @Override
+       public int compareTo(Result result) {
+               return ComparisonChain.start()
+                               .compare(this, result, packArchiveComparator)
+                               .compare(this, result, botNameComparator)
+                               .compare(this, result, packNameComparator).result();
+       }
+
+}