From 11dfcd344c860bd34cf0ca5ee498a8a4f38d1538 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Tue, 19 Feb 2013 21:19:56 +0100 Subject: [PATCH] Show new torrents first. --- .../rhynodge/triggers/NewTorrentTrigger.java | 26 +++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) 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; + } + }; + } + } -- 2.7.4