From 37ac474440aec3141cf609af25ee2332724dc7e4 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Fri, 21 Feb 2020 12:57:33 +0100 Subject: [PATCH] =?utf8?q?=F0=9F=8E=A8=20Replace=20NEWEST=5FFIRST=20compar?= =?utf8?q?ator=20with=20Kotlin=20version?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../net/pterodactylus/sone/core/SoneInserter.java | 3 ++- .../java/net/pterodactylus/sone/data/Post.java | 12 ----------- .../net/pterodactylus/sone/data/impl/SoneImpl.java | 3 ++- .../pterodactylus/sone/fcp/GetPostFeedCommand.java | 3 ++- .../kotlin/net/pterodactylus/sone/data/Post.kt | 8 ++++++++ .../kotlin/net/pterodactylus/sone/data/PostTest.kt | 23 ++++++++++++++++++++++ 6 files changed, 37 insertions(+), 15 deletions(-) diff --git a/src/main/java/net/pterodactylus/sone/core/SoneInserter.java b/src/main/java/net/pterodactylus/sone/core/SoneInserter.java index 651b62e..fff3052 100644 --- a/src/main/java/net/pterodactylus/sone/core/SoneInserter.java +++ b/src/main/java/net/pterodactylus/sone/core/SoneInserter.java @@ -22,6 +22,7 @@ import static java.lang.System.currentTimeMillis; import static java.util.concurrent.TimeUnit.*; import static java.util.logging.Logger.getLogger; import static java.util.stream.Collectors.toList; +import static net.pterodactylus.sone.data.PostKt.newestFirst; import java.io.*; import java.nio.charset.Charset; @@ -308,7 +309,7 @@ public class SoneInserter extends AbstractService { soneProperties.put("name", sone.getName()); soneProperties.put("time", currentTimeMillis()); soneProperties.put("profile", sone.getProfile()); - soneProperties.put("posts", Ordering.from(Post.NEWEST_FIRST).sortedCopy(sone.getPosts())); + soneProperties.put("posts", Ordering.from(newestFirst()).sortedCopy(sone.getPosts())); soneProperties.put("replies", Ordering.from(Reply.TIME_COMPARATOR).reverse().sortedCopy(sone.getReplies())); soneProperties.put("likedPostIds", new HashSet<>(sone.getLikedPostIds())); soneProperties.put("likedReplyIds", new HashSet<>(sone.getLikedReplyIds())); diff --git a/src/main/java/net/pterodactylus/sone/data/Post.java b/src/main/java/net/pterodactylus/sone/data/Post.java index 9914185..d4d34e6 100644 --- a/src/main/java/net/pterodactylus/sone/data/Post.java +++ b/src/main/java/net/pterodactylus/sone/data/Post.java @@ -19,8 +19,6 @@ package net.pterodactylus.sone.data; import static com.google.common.base.Optional.absent; -import java.util.Comparator; - import com.google.common.base.Optional; /** @@ -29,16 +27,6 @@ import com.google.common.base.Optional; */ public interface Post extends Identified { - /** Comparator for posts, sorts descending by time. */ - public static final Comparator NEWEST_FIRST = new Comparator() { - - @Override - public int compare(Post leftPost, Post rightPost) { - return (int) Math.max(Integer.MIN_VALUE, Math.min(Integer.MAX_VALUE, rightPost.getTime() - leftPost.getTime())); - } - - }; - // // ACCESSORS // diff --git a/src/main/java/net/pterodactylus/sone/data/impl/SoneImpl.java b/src/main/java/net/pterodactylus/sone/data/impl/SoneImpl.java index 8f5a50f..df8832f 100644 --- a/src/main/java/net/pterodactylus/sone/data/impl/SoneImpl.java +++ b/src/main/java/net/pterodactylus/sone/data/impl/SoneImpl.java @@ -21,6 +21,7 @@ import static com.google.common.base.Preconditions.checkNotNull; import static java.lang.String.format; import static java.nio.charset.StandardCharsets.UTF_8; import static java.util.logging.Logger.getLogger; +import static net.pterodactylus.sone.data.PostKt.newestFirst; import static net.pterodactylus.sone.data.SoneKt.*; import java.net.MalformedURLException; @@ -365,7 +366,7 @@ public class SoneImpl implements Sone { synchronized (this) { sortedPosts = new ArrayList<>(posts); } - Collections.sort(sortedPosts, Post.NEWEST_FIRST); + sortedPosts.sort(newestFirst()); return sortedPosts; } diff --git a/src/main/java/net/pterodactylus/sone/fcp/GetPostFeedCommand.java b/src/main/java/net/pterodactylus/sone/fcp/GetPostFeedCommand.java index 8197ba8..d340426 100644 --- a/src/main/java/net/pterodactylus/sone/fcp/GetPostFeedCommand.java +++ b/src/main/java/net/pterodactylus/sone/fcp/GetPostFeedCommand.java @@ -32,6 +32,7 @@ import com.google.common.collect.Collections2; import freenet.support.SimpleFieldSet; +import static net.pterodactylus.sone.data.PostKt.newestFirst; import static net.pterodactylus.sone.data.PostKt.noFuturePost; /** @@ -72,7 +73,7 @@ public class GetPostFeedCommand extends AbstractSoneCommand { allPosts = Collections2.filter(allPosts, noFuturePost()::invoke); List sortedPosts = new ArrayList<>(allPosts); - Collections.sort(sortedPosts, Post.NEWEST_FIRST); + sortedPosts.sort(newestFirst()); if (sortedPosts.size() < startPost) { return new Response("PostFeed", encodePosts(Collections. emptyList(), "Posts.", false)); diff --git a/src/main/kotlin/net/pterodactylus/sone/data/Post.kt b/src/main/kotlin/net/pterodactylus/sone/data/Post.kt index 7041561..955196a 100644 --- a/src/main/kotlin/net/pterodactylus/sone/data/Post.kt +++ b/src/main/kotlin/net/pterodactylus/sone/data/Post.kt @@ -1,8 +1,16 @@ package net.pterodactylus.sone.data +import java.util.Comparator.comparing + /** * Predicate that returns whether a post is _not_ from the future, * i.e. whether it should be visible now. */ @get:JvmName("noFuturePost") val noFuturePost: (Post) -> Boolean = { it.time <= System.currentTimeMillis() } + +/** + * Comparator that orders posts by their time, newest posts first. + */ +@get:JvmName("newestFirst") +val newestFirst: Comparator = comparing(Post::getTime).reversed() diff --git a/src/test/kotlin/net/pterodactylus/sone/data/PostTest.kt b/src/test/kotlin/net/pterodactylus/sone/data/PostTest.kt index e5d9e97..ebf96b1 100644 --- a/src/test/kotlin/net/pterodactylus/sone/data/PostTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/data/PostTest.kt @@ -3,6 +3,8 @@ package net.pterodactylus.sone.data import net.pterodactylus.sone.test.createPost import org.hamcrest.MatcherAssert.assertThat import org.hamcrest.Matchers.equalTo +import org.hamcrest.Matchers.greaterThan +import org.hamcrest.Matchers.lessThan import java.util.concurrent.TimeUnit.DAYS import kotlin.test.Test @@ -23,4 +25,25 @@ class PostTest { assertThat(noFuturePost(post), equalTo(true)) } + @Test + fun `newestFirst comparator returns less-than 0 if first is newer than second`() { + val newerPost = createPost(time = 2000) + val olderPost = createPost(time = 1000) + assertThat(newestFirst.compare(newerPost, olderPost), lessThan(0)) + } + + @Test + fun `newestFirst comparator returns greater-than 0 if first is older than second`() { + val newerPost = createPost(time = 2000) + val olderPost = createPost(time = 1000) + assertThat(newestFirst.compare(olderPost, newerPost), greaterThan(0)) + } + + @Test + fun `newestFirst comparator returns 0 if first and second are the same age`() { + val post1 = createPost(time = 1000) + val post2 = createPost(time = 1000) + assertThat(newestFirst.compare(post2, post1), equalTo(0)) + } + } -- 2.7.4