X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fxdcc%2Fui%2Fstdin%2FResult.java;h=47430ca1f5cbd452b0dc860f183a89dd26687d97;hb=8ccf6fb9615a48da74239b56be56f1d4772896a5;hp=4dea6c55dfa39a470792ee0ba001f13150d57236;hpb=1ca482cdf4956eb7dcdcfd75c4d0412cf0261fdf;p=xudocci.git diff --git a/src/main/java/net/pterodactylus/xdcc/ui/stdin/Result.java b/src/main/java/net/pterodactylus/xdcc/ui/stdin/Result.java index 4dea6c5..47430ca 100644 --- a/src/main/java/net/pterodactylus/xdcc/ui/stdin/Result.java +++ b/src/main/java/net/pterodactylus/xdcc/ui/stdin/Result.java @@ -17,15 +17,20 @@ package net.pterodactylus.xdcc.ui.stdin; -import java.util.Arrays; +import static com.google.common.collect.FluentIterable.from; +import static java.util.regex.Pattern.compile; + +import java.util.Collection; import java.util.Comparator; -import java.util.List; +import java.util.function.Predicate; import java.util.regex.Pattern; +import net.pterodactylus.xdcc.core.Core; import net.pterodactylus.xdcc.data.Bot; +import net.pterodactylus.xdcc.data.Download; import net.pterodactylus.xdcc.data.Pack; -import com.google.common.base.Predicate; +import com.google.common.base.Function; import com.google.common.collect.ComparisonChain; /** @@ -35,85 +40,90 @@ import com.google.common.collect.ComparisonChain; */ public class Result implements Comparable { - /** {@link Predicate} that matches {@link Result}s that contain an archive. */ - private static final Predicate isArchive = new Predicate() { - - /** All suffixes that are recognized as archives. */ - private final List archiveSuffixes = Arrays.asList("rar", "tar", "zip", "tar.gz", "tar.bz2", "tar.lzma", "7z"); + private static Predicate matches(String regex) { + Pattern pattern = compile(regex); + return new Predicate() { + @Override + public boolean test(String input) { + return pattern.matcher(input).find(); + } + }; + } - @Override - public boolean apply(Result result) { - for (String suffix : archiveSuffixes) { - if (result.pack().name().toLowerCase().endsWith(suffix)) { - return true; + private static Comparator preferredComparator(Predicate preferredName, Function stringExtractor) { + return new Comparator() { + @Override + public int compare(Result leftResult, Result rightResult) { + boolean leftStringMatches = preferredName.test(stringExtractor.apply(leftResult)); + boolean rightStringMatches = preferredName.test(stringExtractor.apply(rightResult)); + if (leftStringMatches && !rightStringMatches) { + return -1; + } else if (rightStringMatches && !leftStringMatches) { + return 1; } + return 0; } - 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 packArchiveComparator = new Comparator() { - @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 {@link Result}s that sorts archives to the back of the list. */ + private static final Comparator packArchiveComparator = preferredComparator(matches("(rar|tar|zip|tar\\.(gz|bz2|lzma)|7z)$").negate(), (result) -> result.pack().name()); - /** - * {@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 botNameComparator = new Comparator() { + /** {@link Comparator} for bot nicknames. */ + private static final Comparator botNameComparator = + sortEuropeanBotsToTheFront().thenComparing(sortAmericanBotsToTheBack()).thenComparing(sortPassiveBotsToTheBack()); - /** Regular expression pattern for preferred names. */ - private final Pattern preferredNames = Pattern.compile("(?i)[^\\w]EUR?[^\\w]"); + private static Comparator sortEuropeanBotsToTheFront() { + return preferredComparator(matches("(?i)[^\\w]EUR?[^\\w]"), (result) -> result.bot().name()); + } + + private static Comparator sortAmericanBotsToTheBack() { + return preferredComparator(matches("(?i)[^\\w]USA?[^\\w]").negate(), (result) -> result.bot().name()); + } - /** Regular expression pattern for disliked names. */ - private final Pattern dislikedNames = Pattern.compile("(?i)[^\\w]USA?[^\\w]"); + private static Comparator sortPassiveBotsToTheBack() { + return preferredComparator(matches("[-|]P[-|]").negate(), (result) -> result.bot().name()); + } + + private static final Comparator packNameComparator = sortRepacksFirst().thenComparing(sortPacksAlphabetically()); + private static Comparator sortRepacksFirst() { + return preferredComparator(matches("\\.(PROPER|REPACK)\\."), (result) -> result.pack().name()); + } + + private static Comparator sortPacksAlphabetically() { + return new Comparator() { + @Override + public int compare(Result leftResult, Result rightResult) { + return leftResult.pack().name().compareToIgnoreCase(rightResult.pack().name()); + } + }; + } + + /** Comparator that sorts bots with running downloads to the back of the list. */ + private final Comparator botsWithRunningTransfersComparator = new Comparator() { @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()) { + Collection botsWithTransfers = from(core.downloads()).transform(new Function() { + @Override + public Bot apply(Download download) { + return download.bot(); + } + }).toSet(); + boolean leftDownloading = botsWithTransfers.contains(leftResult.bot()); + boolean rightDownloading = botsWithTransfers.contains(rightResult.bot()); + if (leftDownloading && !rightDownloading) { return 1; } - if (dislikedNames.matcher(rightBotName).find() && !dislikedNames.matcher(leftBotName).find()) { + if (!leftDownloading && rightDownloading) { return -1; } return 0; } }; - /** - * {@link Comparator} for {@link Result}s that sorts them by the name of the - * {@link Pack}. - */ - private static final Comparator packNameComparator = new Comparator() { - @Override - public int compare(Result leftResult, Result rightResult) { - return leftResult.pack().name().compareToIgnoreCase(rightResult.pack().name()); - } - }; + /** The core. */ + private final Core core; /** The bot carrying the pack. */ private final Bot bot; @@ -124,12 +134,15 @@ public class Result implements Comparable { /** * Creates a new result. * + * @param core + * The core * @param bot * The bot carrying the pack * @param pack - * The pack + * The pack of the result */ - Result(Bot bot, Pack pack) { + Result(Core core, Bot bot, Pack pack) { + this.core = core; this.bot = bot; this.pack = pack; } @@ -163,9 +176,10 @@ public class Result implements Comparable { @Override public int compareTo(Result result) { return ComparisonChain.start() + .compare(this, result, botsWithRunningTransfersComparator) .compare(this, result, packArchiveComparator) - .compare(this, result, botNameComparator) - .compare(this, result, packNameComparator).result(); + .compare(this, result, packNameComparator) + .compare(this, result, botNameComparator).result(); } }