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 java.util.Arrays;
21 import java.util.Comparator;
22 import java.util.List;
23 import java.util.regex.Pattern;
25 import net.pterodactylus.xdcc.data.Bot;
26 import net.pterodactylus.xdcc.data.Pack;
28 import com.google.common.base.Predicate;
29 import com.google.common.collect.ComparisonChain;
32 * Container for result information.
34 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
36 public class Result implements Comparable<Result> {
38 /** {@link Predicate} that matches {@link Result}s that contain an archive. */
39 private static final Predicate<Result> isArchive = new Predicate<Result>() {
41 /** All suffixes that are recognized as archives. */
42 private final List<String> archiveSuffixes = Arrays.asList("rar", "tar", "zip", "tar.gz", "tar.bz2", "tar.lzma", "7z");
45 public boolean apply(Result result) {
46 for (String suffix : archiveSuffixes) {
47 if (result.pack().name().toLowerCase().endsWith(suffix)) {
56 * {@link Comparator} for {@link Result}s that sorts archives (as per {@link
57 * #isArchive} to the back of the list.
59 private static final Comparator<Result> packArchiveComparator = new Comparator<Result>() {
61 public int compare(Result leftResult, Result rightResult) {
62 if (isArchive.apply(leftResult) && !isArchive.apply(rightResult)) {
65 if (!isArchive.apply(leftResult) && isArchive.apply(rightResult)) {
73 * {@link Comparator} for bot nicknames. It comprises different strategies: one
74 * name pattern is preferred (and thus listed first), one pattern is disliked
75 * (and thus listed last), the rest is sorted alphabetically.
77 private static final Comparator<Result> botNameComparator = new Comparator<Result>() {
79 /** Regular expression pattern for preferred names. */
80 private final Pattern preferredNames = Pattern.compile("(?i)[^\\w]EUR?[^\\w]");
82 /** Regular expression pattern for disliked names. */
83 private final Pattern dislikedNames = Pattern.compile("(?i)[^\\w]USA?[^\\w]");
86 public int compare(Result leftResult, Result rightResult) {
87 String leftBotName = leftResult.bot().name();
88 String rightBotName = rightResult.bot().name();
89 /* preferred names to the front! */
90 if (preferredNames.matcher(leftBotName).find() && !preferredNames.matcher(rightBotName).find()) {
93 if (preferredNames.matcher(rightBotName).find() && !preferredNames.matcher(leftBotName).find()) {
96 /* disliked names to the back. */
97 if (dislikedNames.matcher(leftBotName).find() && !dislikedNames.matcher(rightBotName).find()) {
100 if (dislikedNames.matcher(rightBotName).find() && !dislikedNames.matcher(leftBotName).find()) {
108 * {@link Comparator} for {@link Result}s that sorts them by the name of the
111 private static final Comparator<Result> packNameComparator = new Comparator<Result>() {
113 public int compare(Result leftResult, Result rightResult) {
114 return leftResult.pack().name().compareToIgnoreCase(rightResult.pack().name());
118 /** The bot carrying the pack. */
119 private final Bot bot;
122 private final Pack pack;
125 * Creates a new result.
128 * The bot carrying the pack
132 Result(Bot bot, Pack pack) {
142 * Returns the bot carrying the pack.
144 * @return The bot carrying the pack
160 // COMPARABLE METHODS
164 public int compareTo(Result result) {
165 return ComparisonChain.start()
166 .compare(this, result, packArchiveComparator)
167 .compare(this, result, botNameComparator)
168 .compare(this, result, packNameComparator).result();