8532a790b6ebbd6917eee5e58410cc122f0e102d
[xudocci.git] / src / main / java / net / pterodactylus / xdcc / ui / stdin / Result.java
1 /*
2  * XdccDownloader - Result.java - Copyright © 2013 David Roden
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.pterodactylus.xdcc.ui.stdin;
19
20 import static com.google.common.collect.FluentIterable.from;
21
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;
27
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;
32
33 import com.google.common.base.Function;
34 import com.google.common.base.Predicate;
35 import com.google.common.collect.ComparisonChain;
36
37 /**
38  * Container for result information.
39  *
40  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
41  */
42 public class Result implements Comparable<Result> {
43
44         /** {@link Predicate} that matches {@link Result}s that contain an archive. */
45         private static final Predicate<Result> isArchive = new Predicate<Result>() {
46
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");
49
50                 @Override
51                 public boolean apply(Result result) {
52                         for (String suffix : archiveSuffixes) {
53                                 if (result.pack().name().toLowerCase().endsWith(suffix)) {
54                                         return true;
55                                 }
56                         }
57                         return false;
58                 }
59         };
60
61         /**
62          * {@link Comparator} for {@link Result}s that sorts archives (as per {@link
63          * #isArchive} to the back of the list.
64          */
65         private static final Comparator<Result> packArchiveComparator = new Comparator<Result>() {
66                 @Override
67                 public int compare(Result leftResult, Result rightResult) {
68                         if (isArchive.apply(leftResult) && !isArchive.apply(rightResult)) {
69                                 return 1;
70                         }
71                         if (!isArchive.apply(leftResult) && isArchive.apply(rightResult)) {
72                                 return -1;
73                         }
74                         return 0;
75                 }
76         };
77
78         /**
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.
82          */
83         private static final Comparator<Result> botNameComparator = new Comparator<Result>() {
84
85                 /** Regular expression pattern for preferred names. */
86                 private final Pattern preferredNames = Pattern.compile("(?i)[^\\w]EUR?[^\\w]");
87
88                 /** Regular expression pattern for disliked names. */
89                 private final Pattern dislikedNames = Pattern.compile("(?i)[^\\w]USA?[^\\w]");
90
91                 @Override
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()) {
97                                 return -1;
98                         }
99                         if (preferredNames.matcher(rightBotName).find() && !preferredNames.matcher(leftBotName).find()) {
100                                 return 1;
101                         }
102                         /* disliked names to the back. */
103                         if (dislikedNames.matcher(leftBotName).find() && !dislikedNames.matcher(rightBotName).find()) {
104                                 return 1;
105                         }
106                         if (dislikedNames.matcher(rightBotName).find() && !dislikedNames.matcher(leftBotName).find()) {
107                                 return -1;
108                         }
109                         return 0;
110                 }
111         };
112
113         /**
114          * {@link Comparator} for {@link Result}s that sorts them by the name of the
115          * {@link Pack}.
116          */
117         private static final Comparator<Result> packNameComparator = new Comparator<Result>() {
118                 @Override
119                 public int compare(Result leftResult, Result rightResult) {
120                         return leftResult.pack().name().compareToIgnoreCase(rightResult.pack().name());
121                 }
122         };
123
124         /** Comparator that sorts bots with running downloads to the back of the list. */
125         private final Comparator<Result> botsWithRunningTransfersComparator = new Comparator<Result>() {
126                 @Override
127                 public int compare(Result leftResult, Result rightResult) {
128                         Collection<Bot> botsWithTransfers = from(core.downloads()).transform(new Function<Download, Bot>() {
129                                 @Override
130                                 public Bot apply(Download download) {
131                                         return download.bot();
132                                 }
133                         }).toSet();
134                         boolean leftDownloading = botsWithTransfers.contains(leftResult.bot());
135                         boolean rightDownloading = botsWithTransfers.contains(rightResult.bot());
136                         if (leftDownloading && !rightDownloading) {
137                                 return 1;
138                         }
139                         if (!leftDownloading && rightDownloading) {
140                                 return -1;
141                         }
142                         return 0;
143                 }
144         };
145
146         /** The core. */
147         private final Core core;
148
149         /** The bot carrying the pack. */
150         private final Bot bot;
151
152         /** The pack. */
153         private final Pack pack;
154
155         /**
156          * Creates a new result.
157          *
158          * @param core
159          *              The core
160          * @param bot
161          *              The bot carrying the pack
162          * @param pack
163          *              The pack of the result
164          */
165         Result(Core core, Bot bot, Pack pack) {
166                 this.core = core;
167                 this.bot = bot;
168                 this.pack = pack;
169         }
170
171         //
172         // ACCESSORS
173         //
174
175         /**
176          * Returns the bot carrying the pack.
177          *
178          * @return The bot carrying the pack
179          */
180         public Bot bot() {
181                 return bot;
182         }
183
184         /**
185          * Returns the pack.
186          *
187          * @return The pack
188          */
189         public Pack pack() {
190                 return pack;
191         }
192
193         //
194         // COMPARABLE METHODS
195         //
196
197         @Override
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();
204         }
205
206 }