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;
21 import static java.util.regex.Pattern.compile;
23 import java.util.Collection;
24 import java.util.Comparator;
25 import java.util.function.Predicate;
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.collect.ComparisonChain;
37 * Container for result information.
39 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
41 public class Result implements Comparable<Result> {
43 private static Predicate<String> matches(String regex) {
44 Pattern pattern = compile(regex);
45 return new Predicate<String>() {
47 public boolean test(String input) {
48 return pattern.matcher(input).find();
53 private static Comparator<Result> preferredComparator(Predicate<String> preferredName, Function<Result, String> stringExtractor) {
54 return new Comparator<Result>() {
56 public int compare(Result leftResult, Result rightResult) {
57 boolean leftStringMatches = preferredName.test(stringExtractor.apply(leftResult));
58 boolean rightStringMatches = preferredName.test(stringExtractor.apply(rightResult));
59 if (leftStringMatches && !rightStringMatches) {
61 } else if (rightStringMatches && !leftStringMatches) {
69 /** {@link Comparator} for {@link Result}s that sorts archives to the back of the list. */
70 private static final Comparator<Result> packArchiveComparator = preferredComparator(matches("(rar|tar|zip|tar\\.(gz|bz2|lzma)|7z)$").negate(), (result) -> result.pack().name());
72 /** {@link Comparator} for bot nicknames. */
73 private static final Comparator<Result> botNameComparator =
74 sortEuropeanBotsToTheFront().thenComparing(sortAmericanBotsToTheBack()).thenComparing(sortPassiveBotsToTheBack());
76 private static Comparator<Result> sortEuropeanBotsToTheFront() {
77 return preferredComparator(matches("(?i)[^\\w]EUR?[^\\w]"), (result) -> result.bot().name());
80 private static Comparator<Result> sortAmericanBotsToTheBack() {
81 return preferredComparator(matches("(?i)[^\\w]USA?[^\\w]").negate(), (result) -> result.bot().name());
84 private static Comparator<Result> sortPassiveBotsToTheBack() {
85 return preferredComparator(matches("[-|]P[-|]").negate(), (result) -> result.bot().name());
88 private static final Comparator<Result> packNameComparator = sortRepacksFirst().thenComparing(sortPacksAlphabetically());
90 private static Comparator<Result> sortRepacksFirst() {
91 return preferredComparator(matches("\\.(PROPER|REPACK)\\."), (result) -> result.pack().name());
94 private static Comparator<Result> sortPacksAlphabetically() {
95 return new Comparator<Result>() {
97 public int compare(Result leftResult, Result rightResult) {
98 return leftResult.pack().name().compareToIgnoreCase(rightResult.pack().name());
103 /** Comparator that sorts bots with running downloads to the back of the list. */
104 private final Comparator<Result> botsWithRunningTransfersComparator = new Comparator<Result>() {
106 public int compare(Result leftResult, Result rightResult) {
107 Collection<Bot> botsWithTransfers = from(core.downloads()).transform(new Function<Download, Bot>() {
109 public Bot apply(Download download) {
110 return download.bot();
113 boolean leftDownloading = botsWithTransfers.contains(leftResult.bot());
114 boolean rightDownloading = botsWithTransfers.contains(rightResult.bot());
115 if (leftDownloading && !rightDownloading) {
118 if (!leftDownloading && rightDownloading) {
126 private final Core core;
128 /** The bot carrying the pack. */
129 private final Bot bot;
132 private final Pack pack;
135 * Creates a new result.
140 * The bot carrying the pack
142 * The pack of the result
144 Result(Core core, Bot bot, Pack pack) {
155 * Returns the bot carrying the pack.
157 * @return The bot carrying the pack
173 // COMPARABLE METHODS
177 public int compareTo(Result result) {
178 return ComparisonChain.start()
179 .compare(this, result, botsWithRunningTransfersComparator)
180 .compare(this, result, packArchiveComparator)
181 .compare(this, result, botNameComparator)
182 .compare(this, result, packNameComparator).result();