🎨 Replace post count comparator with Kotlin version
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sun, 16 Feb 2020 20:04:33 +0000 (21:04 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sun, 16 Feb 2020 20:10:11 +0000 (21:10 +0100)
src/main/java/net/pterodactylus/sone/data/Sone.java
src/main/kotlin/net/pterodactylus/sone/data/Sone.kt
src/main/kotlin/net/pterodactylus/sone/web/pages/KnownSonesPage.kt
src/test/kotlin/net/pterodactylus/sone/data/SoneTest.kt [new file with mode: 0644]

index fa6f980..4dd9a8e 100644 (file)
@@ -62,18 +62,6 @@ public interface Sone extends Identified, Fingerprintable, Comparable<Sone> {
                downloading,
        }
 
-       /** Comparator that sorts Sones by numbers of posts (descending). */
-       public static final Comparator<Sone> POST_COUNT_COMPARATOR = new Comparator<Sone>() {
-
-               /**
-                * {@inheritDoc}
-                */
-               @Override
-               public int compare(Sone leftSone, Sone rightSone) {
-                       return (leftSone.getPosts().size() != rightSone.getPosts().size()) ? (rightSone.getPosts().size() - leftSone.getPosts().size()) : (rightSone.getReplies().size() - leftSone.getReplies().size());
-               }
-       };
-
        /** Comparator that sorts Sones by number of images (descending). */
        public static final Comparator<Sone> IMAGE_COUNT_COMPARATOR = new Comparator<Sone>() {
 
index 78a17f5..c44d6e8 100644 (file)
@@ -37,3 +37,14 @@ val niceNameComparator: Comparator<Sone> =
 @get:JvmName("lastActivityComparator") // TODO: remove once Sone is 100% Kotlin
 val lastActivityComparator: Comparator<Sone> =
                comparing(Sone::getTime).reversed()
+
+/**
+ * Comparator that sorts Sones by their [post count][Sone.getPosts] (most posts
+ * first) and, failing that, by their [reply count][Sone.getReplies] (most
+ * replies first).
+ */
+@get:JvmName("postCountComparator") // TODO: remove once Sone is 100% Kotlin
+val postCountComparator: Comparator<Sone> =
+               comparing<Sone, Int> { it.posts.size }
+                               .thenComparing<Int> { it.replies.size }
+                               .reversed()
index 439a163..392dd93 100644 (file)
@@ -30,7 +30,7 @@ class KnownSonesPage @Inject constructor(webInterface: WebInterface, loaders: Lo
                                                        when (soneRequest.parameters["sort"]) {
                                                                "images" -> Sone.IMAGE_COUNT_COMPARATOR
                                                                "name" -> niceNameComparator.reversed()
-                                                               "posts" -> Sone.POST_COUNT_COMPARATOR
+                                                               "posts" -> postCountComparator
                                                                else -> lastActivityComparator
                                                        }.let { comparator ->
                                                                when (soneRequest.parameters["order"]) {
diff --git a/src/test/kotlin/net/pterodactylus/sone/data/SoneTest.kt b/src/test/kotlin/net/pterodactylus/sone/data/SoneTest.kt
new file mode 100644 (file)
index 0000000..5cc0ee2
--- /dev/null
@@ -0,0 +1,68 @@
+/**
+ * Sone - SoneTest.kt - Copyright Â© 2020 David â€˜Bombe’ Roden
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package net.pterodactylus.sone.data
+
+import net.pterodactylus.sone.data.impl.*
+import net.pterodactylus.sone.test.*
+import org.hamcrest.MatcherAssert.*
+import org.hamcrest.Matchers.*
+import kotlin.test.*
+
+/**
+ * Unit test for functions in Sone.
+ */
+class SoneTest {
+
+       @Test
+       fun `post count comparator sorts sones with different number of posts correctly`() {
+               val sone1 = object : IdOnlySone("1") {
+                       override fun getPosts() = listOf(createPost(), createPost())
+               }
+               val sone2 = object : IdOnlySone("2") {
+                       override fun getPosts() = listOf(createPost(), createPost(), createPost())
+               }
+               assertThat(postCountComparator.compare(sone1, sone2), greaterThan(0))
+       }
+
+       @Test
+       fun `post count comparator compares replies if posts are not different`() {
+               val sone1 = object : IdOnlySone("1") {
+                       override fun getPosts() = listOf(createPost(), createPost())
+                       override fun getReplies() = setOf(emptyPostReply(), emptyPostReply())
+               }
+               val sone2 = object : IdOnlySone("2") {
+                       override fun getPosts() = listOf(createPost(), createPost())
+                       override fun getReplies() = setOf(emptyPostReply(), emptyPostReply(), emptyPostReply())
+               }
+               assertThat(postCountComparator.compare(sone1, sone2), greaterThan(0))
+       }
+
+       @Test
+       fun `post count comparator sorts sone with same amount of posts and replies as equal`() {
+               val sone1 = object : IdOnlySone("1") {
+                       override fun getPosts() = listOf(createPost(), createPost())
+                       override fun getReplies() = setOf(emptyPostReply(), emptyPostReply())
+               }
+               val sone2 = object : IdOnlySone("2") {
+                       override fun getPosts() = listOf(createPost(), createPost())
+                       override fun getReplies() = setOf(emptyPostReply(), emptyPostReply())
+               }
+               assertThat(postCountComparator.compare(sone1, sone2), equalTo(0))
+       }
+
+}