From 9b0b5b760adf41d52603a4b65f3e5f3220da28eb Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sun, 16 Feb 2020 21:04:33 +0100 Subject: [PATCH] =?utf8?q?=F0=9F=8E=A8=20Replace=20post=20count=20comparat?= =?utf8?q?or=20with=20Kotlin=20version?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../java/net/pterodactylus/sone/data/Sone.java | 12 ---- .../kotlin/net/pterodactylus/sone/data/Sone.kt | 11 ++++ .../pterodactylus/sone/web/pages/KnownSonesPage.kt | 2 +- .../kotlin/net/pterodactylus/sone/data/SoneTest.kt | 68 ++++++++++++++++++++++ 4 files changed, 80 insertions(+), 13 deletions(-) create mode 100644 src/test/kotlin/net/pterodactylus/sone/data/SoneTest.kt diff --git a/src/main/java/net/pterodactylus/sone/data/Sone.java b/src/main/java/net/pterodactylus/sone/data/Sone.java index fa6f980..4dd9a8e 100644 --- a/src/main/java/net/pterodactylus/sone/data/Sone.java +++ b/src/main/java/net/pterodactylus/sone/data/Sone.java @@ -62,18 +62,6 @@ public interface Sone extends Identified, Fingerprintable, Comparable { downloading, } - /** Comparator that sorts Sones by numbers of posts (descending). */ - public static final Comparator POST_COUNT_COMPARATOR = new Comparator() { - - /** - * {@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 IMAGE_COUNT_COMPARATOR = new Comparator() { diff --git a/src/main/kotlin/net/pterodactylus/sone/data/Sone.kt b/src/main/kotlin/net/pterodactylus/sone/data/Sone.kt index 78a17f5..c44d6e8 100644 --- a/src/main/kotlin/net/pterodactylus/sone/data/Sone.kt +++ b/src/main/kotlin/net/pterodactylus/sone/data/Sone.kt @@ -37,3 +37,14 @@ val niceNameComparator: Comparator = @get:JvmName("lastActivityComparator") // TODO: remove once Sone is 100% Kotlin val lastActivityComparator: Comparator = 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 = + comparing { it.posts.size } + .thenComparing { it.replies.size } + .reversed() diff --git a/src/main/kotlin/net/pterodactylus/sone/web/pages/KnownSonesPage.kt b/src/main/kotlin/net/pterodactylus/sone/web/pages/KnownSonesPage.kt index 439a163..392dd93 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/pages/KnownSonesPage.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/pages/KnownSonesPage.kt @@ -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 index 0000000..5cc0ee2 --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/sone/data/SoneTest.kt @@ -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 . + */ + +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)) + } + +} -- 2.7.4