- return String.format("%02d:%02d", (secondsLeft / 60) % 60, secondsLeft % 60);
- }
-
- /** Container for result information. */
- private static class Result implements Comparable<Result> {
-
- /** {@link Predicate} that matches {@link Result}s that contain an archive. */
- private static final Predicate<Result> isArchive = new Predicate<Result>() {
-
- /** All suffixes that are recognized as archives. */
- private final List<String> archiveSuffixes = Arrays.asList("rar", "tar", "zip", "tar.gz", "tar.bz2", "tar.lzma", "7z");
-
- @Override
- public boolean apply(Result result) {
- for (String suffix : archiveSuffixes) {
- if (result.pack().name().toLowerCase().endsWith(suffix)) {
- return true;
- }
- }
- 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<Result> packArchiveComparator = new Comparator<Result>() {
- @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 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<Result> botNameComparator = new Comparator<Result>() {
-
- /** Regular expression pattern for preferred names. */
- private final Pattern preferredNames = Pattern.compile("(?i)[^\\w]EUR?[^\\w]");
-
- /** Regular expression pattern for disliked names. */
- private final Pattern dislikedNames = Pattern.compile("(?i)[^\\w]USA?[^\\w]");
-
- @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()) {
- return 1;
- }
- if (dislikedNames.matcher(rightBotName).find() && !dislikedNames.matcher(leftBotName).find()) {
- return -1;
- }
- return 0;
- }
- };
-
- /**
- * {@link Comparator} for {@link Result}s that sorts them by the name of the
- * {@link Pack}.
- */
- private static final Comparator<Result> packNameComparator = new Comparator<Result>() {
- @Override
- public int compare(Result leftResult, Result rightResult) {
- return leftResult.pack().name().compareToIgnoreCase(rightResult.pack().name());
- }
- };
-
- /** The bot carrying the pack. */
- private final Bot bot;
-
- /** The pack. */
- private final Pack pack;
-
- /**
- * Creates a new result.
- *
- * @param bot
- * The bot carrying the pack
- * @param pack
- * The pack
- */
- private Result(Bot bot, Pack pack) {
- this.bot = bot;
- this.pack = pack;
- }
-
- //
- // ACCESSORS
- //
-
- /**
- * Returns the bot carrying the pack.
- *
- * @return The bot carrying the pack
- */
- public Bot bot() {
- return bot;
- }
-
- /**
- * Returns the pack.
- *
- * @return The pack
- */
- public Pack pack() {
- return pack;
- }
-
- //
- // COMPARABLE METHODS
- //
-
- @Override
- public int compareTo(Result result) {
- return ComparisonChain.start()
- .compare(this, result, packArchiveComparator)
- .compare(this, result, botNameComparator)
- .compare(this, result, packNameComparator).result();
- }
-
- }
-
- /**
- * Container for the current state of the command reader.
- *
- * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
- */
- private static class State {
-
- /** The last connections displayed. */
- private final List<Connection> lastConnections;
-
- /** The last results displayed. */
- private final List<Result> lastResults;
-
- /** The last downloads displayed. */
- private final List<Download> lastDownloads;
-
- /** Creates a new empty state. */
- public State() {
- this(Lists.<Connection>newArrayList(), Lists.<Result>newArrayList(), Lists.<Download>newArrayList());
- }
-
- /**
- * Creates a new state.
- *
- * @param lastConnections
- * The last connections
- * @param lastResults
- * The last results
- * @param lastDownloads
- * The last downloads
- */
- private State(List<Connection> lastConnections, List<Result> lastResults, List<Download> lastDownloads) {
- this.lastConnections = lastConnections;
- this.lastResults = lastResults;
- this.lastDownloads = lastDownloads;
- }
-
- //
- // ACCESSORS
- //
-
- /**
- * Returns the last connections displayed.
- *
- * @return The last connections displayed
- */
- public List<Connection> getLastConnections() {
- return lastConnections;
- }
-
- /**
- * Returns the last results displayed.
- *
- * @return The last results displayed
- */
- public List<Result> getLastResults() {
- return lastResults;
- }
-
- /**
- * Returns the last downloads displayed.
- *
- * @return The last downloads displayed
- */
- public List<Download> getLastDownloads() {
- return lastDownloads;
- }
-
- //
- // MUTATORS
- //
-
- /**
- * Returns a new state with the given last connections and the last downloads
- * and results of this state.
- *
- * @param lastConnections
- * The new last connections displayed
- * @return The new state
- */
- public State setLastConnections(List<Connection> lastConnections) {
- return new State(lastConnections, lastResults, lastDownloads);
- }
-
- /**
- * Returns a new state with the given last results and the last downloads and
- * connections of this state.
- *
- * @param lastResults
- * The new last results displayed
- * @return The new state
- */
- public State setLastResults(List<Result> lastResults) {
- return new State(lastConnections, lastResults, lastDownloads);
- }
-
- /**
- * Returns a new state with the given last downloads and the last connections
- * and results of this state.
- *
- * @param lastDownloads
- * The new last downloads displayed
- * @return The new state
- */
- public State setLastDownloads(List<Download> lastDownloads) {
- return new State(lastConnections, lastResults, lastDownloads);
- }
-