From: David ‘Bombe’ Roden Date: Tue, 19 Feb 2013 20:19:56 +0000 (+0100) Subject: Show new torrents first. X-Git-Tag: 0.1~5 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=11dfcd344c860bd34cf0ca5ee498a8a4f38d1538;p=rhynodge.git Show new torrents first. --- diff --git a/src/main/java/net/pterodactylus/rhynodge/triggers/NewTorrentTrigger.java b/src/main/java/net/pterodactylus/rhynodge/triggers/NewTorrentTrigger.java index 9e3e91e..f084c61 100644 --- a/src/main/java/net/pterodactylus/rhynodge/triggers/NewTorrentTrigger.java +++ b/src/main/java/net/pterodactylus/rhynodge/triggers/NewTorrentTrigger.java @@ -33,6 +33,7 @@ import net.pterodactylus.rhynodge.states.TorrentState.TorrentFile; import org.apache.commons.lang3.StringEscapeUtils; import com.google.common.collect.Lists; +import com.google.common.collect.Ordering; import com.google.common.collect.Sets; /** @@ -146,7 +147,7 @@ public class NewTorrentTrigger implements Trigger { htmlBuilder.append("\n"); htmlBuilder.append("\n"); htmlBuilder.append("\n"); - for (TorrentFile torrentFile : allTorrentFiles) { + for (TorrentFile torrentFile : sortNewFirst().sortedCopy(allTorrentFiles)) { if (newTorrentFiles.contains(torrentFile)) { htmlBuilder.append(""); } else { @@ -167,4 +168,27 @@ public class NewTorrentTrigger implements Trigger { return htmlBuilder.toString(); } + /** + * Returns an ordering that sorts torrent files by whether they are new + * (according to {@link #newTorrentFiles}) or not. New files will be sorted + * first. + * + * @return An ordering for “new files first” + */ + private Ordering sortNewFirst() { + return new Ordering() { + + @Override + public int compare(TorrentFile leftTorrentFile, TorrentFile rightTorrentFile) { + if (newTorrentFiles.contains(leftTorrentFile) && !newTorrentFiles.contains(rightTorrentFile)) { + return -1; + } + if (!newTorrentFiles.contains(leftTorrentFile) && newTorrentFiles.contains(rightTorrentFile)) { + return 1; + } + return 0; + } + }; + } + }