2 * XdccDownloader - Result.java - Copyright © 2013 David Roden
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package net.pterodactylus.xdcc.ui.stdin;
20 import static com.google.common.collect.FluentIterable.from;
22 import java.util.Arrays;
23 import java.util.Collection;
24 import java.util.Comparator;
25 import java.util.List;
26 import java.util.regex.Pattern;
28 import net.pterodactylus.xdcc.core.Core;
29 import net.pterodactylus.xdcc.data.Bot;
30 import net.pterodactylus.xdcc.data.Download;
31 import net.pterodactylus.xdcc.data.Pack;
33 import com.google.common.base.Function;
34 import com.google.common.base.Predicate;
35 import com.google.common.collect.ComparisonChain;
38 * Container for result information.
40 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
42 public class Result implements Comparable<Result> {
44 /** {@link Predicate} that matches {@link Result}s that contain an archive. */
45 private static final Predicate<Result> isArchive = new Predicate<Result>() {
47 /** All suffixes that are recognized as archives. */
48 private final List<String> archiveSuffixes = Arrays.asList("rar", "tar", "zip", "tar.gz", "tar.bz2", "tar.lzma", "7z");
51 public boolean apply(Result result) {
52 for (String suffix : archiveSuffixes) {
53 if (result.pack().name().toLowerCase().endsWith(suffix)) {
62 * {@link Comparator} for {@link Result}s that sorts archives (as per {@link
63 * #isArchive} to the back of the list.
65 private static final Comparator<Result> packArchiveComparator = new Comparator<Result>() {
67 public int compare(Result leftResult, Result rightResult) {
68 if (isArchive.apply(leftResult) && !isArchive.apply(rightResult)) {
71 if (!isArchive.apply(leftResult) && isArchive.apply(rightResult)) {
79 * {@link Comparator} for bot nicknames. It comprises different strategies: one
80 * name pattern is preferred (and thus listed first), one pattern is disliked
81 * (and thus listed last), the rest is sorted alphabetically.
83 private static final Comparator<Result> botNameComparator = new Comparator<Result>() {
85 /** Regular expression pattern for preferred names. */
86 private final Pattern preferredNames = Pattern.compile("(?i)[^\\w]EUR?[^\\w]");
88 /** Regular expression pattern for disliked names. */
89 private final Pattern dislikedNames = Pattern.compile("(?i)[^\\w]USA?[^\\w]");
92 public int compare(Result leftResult, Result rightResult) {
93 String leftBotName = leftResult.bot().name();
94 String rightBotName = rightResult.bot().name();
95 /* preferred names to the front! */
96 if (preferredNames.matcher(leftBotName).find() && !preferredNames.matcher(rightBotName).find()) {
99 if (preferredNames.matcher(rightBotName).find() && !preferredNames.matcher(leftBotName).find()) {
102 /* disliked names to the back. */
103 if (dislikedNames.matcher(leftBotName).find() && !dislikedNames.matcher(rightBotName).find()) {
106 if (dislikedNames.matcher(rightBotName).find() && !dislikedNames.matcher(leftBotName).find()) {
114 * {@link Comparator} for {@link Result}s that sorts them by the name of the
117 private static final Comparator<Result> packNameComparator = new Comparator<Result>() {
119 public int compare(Result leftResult, Result rightResult) {
120 return leftResult.pack().name().compareToIgnoreCase(rightResult.pack().name());
124 /** Comparator that sorts bots with running downloads to the back of the list. */
125 private final Comparator<Result> botsWithRunningTransfersComparator = new Comparator<Result>() {
127 public int compare(Result leftResult, Result rightResult) {
128 Collection<Bot> botsWithTransfers = from(core.downloads()).transform(new Function<Download, Bot>() {
130 public Bot apply(Download download) {
131 return download.bot();
134 boolean leftDownloading = botsWithTransfers.contains(leftResult.bot());
135 boolean rightDownloading = botsWithTransfers.contains(rightResult.bot());
136 if (leftDownloading && !rightDownloading) {
139 if (!leftDownloading && rightDownloading) {
147 private final Core core;
149 /** The bot carrying the pack. */
150 private final Bot bot;
153 private final Pack pack;
156 * Creates a new result.
161 * The bot carrying the pack
163 * The pack of the result
165 Result(Core core, Bot bot, Pack pack) {
176 * Returns the bot carrying the pack.
178 * @return The bot carrying the pack
194 // COMPARABLE METHODS
198 public int compareTo(Result result) {
199 return ComparisonChain.start()
200 .compare(this, result, packArchiveComparator)
201 .compare(this, result, botNameComparator)
202 .compare(this, result, botsWithRunningTransfersComparator)
203 .compare(this, result, packNameComparator).result();