From 07d3fa7dac96e8a5eff41487d647974df8c26f8d 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:49:23 +0100 Subject: [PATCH] =?utf8?q?=F0=9F=8E=A8=20Replace=20image=20count=20compara?= =?utf8?q?tor=20with=20Kotlin=20version?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../java/net/pterodactylus/sone/data/Sone.java | 18 ------------------ .../kotlin/net/pterodactylus/sone/data/Sone.kt | 3 +++ .../pterodactylus/sone/web/pages/KnownSonesPage.kt | 2 +- .../kotlin/net/pterodactylus/sone/data/SoneTest.kt | 22 ++++++++++++++++++++++ .../kotlin/net/pterodactylus/sone/test/Mocks.kt | 3 +++ 5 files changed, 29 insertions(+), 19 deletions(-) diff --git a/src/main/java/net/pterodactylus/sone/data/Sone.java b/src/main/java/net/pterodactylus/sone/data/Sone.java index 4dd9a8e..836baa1 100644 --- a/src/main/java/net/pterodactylus/sone/data/Sone.java +++ b/src/main/java/net/pterodactylus/sone/data/Sone.java @@ -18,13 +18,11 @@ package net.pterodactylus.sone.data; import static com.google.common.collect.FluentIterable.from; -import static java.util.Arrays.asList; import static net.pterodactylus.sone.data.Album.FLATTENER; import static net.pterodactylus.sone.data.Album.IMAGES; import java.util.Collection; import java.util.Collections; -import java.util.Comparator; import java.util.List; import java.util.Set; @@ -36,7 +34,6 @@ import net.pterodactylus.sone.freenet.wot.Identity; import freenet.keys.FreenetURI; import com.google.common.base.Function; -import com.google.common.primitives.Ints; /** * A Sone defines everything about a user: her profile, her status updates, her @@ -62,21 +59,6 @@ public interface Sone extends Identified, Fingerprintable, Comparable { downloading, } - /** Comparator that sorts Sones by number of images (descending). */ - public static final Comparator IMAGE_COUNT_COMPARATOR = new Comparator() { - - /** - * {@inheritDoc} - */ - @Override - public int compare(Sone leftSone, Sone rightSone) { - int rightSoneImageCount = from(asList(rightSone.getRootAlbum())).transformAndConcat(FLATTENER).transformAndConcat(IMAGES).size(); - int leftSoneImageCount = from(asList(leftSone.getRootAlbum())).transformAndConcat(FLATTENER).transformAndConcat(IMAGES).size(); - /* sort descending. */ - return Ints.compare(rightSoneImageCount, leftSoneImageCount); - } - }; - public static final Function> toAllAlbums = new Function>() { @Override public List apply(@Nullable Sone sone) { diff --git a/src/main/kotlin/net/pterodactylus/sone/data/Sone.kt b/src/main/kotlin/net/pterodactylus/sone/data/Sone.kt index c44d6e8..3f70ad7 100644 --- a/src/main/kotlin/net/pterodactylus/sone/data/Sone.kt +++ b/src/main/kotlin/net/pterodactylus/sone/data/Sone.kt @@ -48,3 +48,6 @@ val postCountComparator: Comparator = comparing { it.posts.size } .thenComparing { it.replies.size } .reversed() + +val imageCountComparator: Comparator = + comparing { it.rootAlbum.allImages.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 392dd93..01d1d22 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/pages/KnownSonesPage.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/pages/KnownSonesPage.kt @@ -28,7 +28,7 @@ class KnownSonesPage @Inject constructor(webInterface: WebInterface, loaders: Lo .filterNot { soneRequest.parameters["filter"] == "not-own" && it.isLocal } .sortedWith( when (soneRequest.parameters["sort"]) { - "images" -> Sone.IMAGE_COUNT_COMPARATOR + "images" -> imageCountComparator "name" -> niceNameComparator.reversed() "posts" -> postCountComparator else -> lastActivityComparator diff --git a/src/test/kotlin/net/pterodactylus/sone/data/SoneTest.kt b/src/test/kotlin/net/pterodactylus/sone/data/SoneTest.kt index 6b7e51d..1429e54 100644 --- a/src/test/kotlin/net/pterodactylus/sone/data/SoneTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/data/SoneTest.kt @@ -120,4 +120,26 @@ class SoneTest { assertThat(postCountComparator.compare(sone1, sone2), equalTo(0)) } + @Test + fun `image count comparator sorts Sones correctly if number of images is different`() { + val sone1 = object : IdOnlySone("1") { + override fun getRootAlbum() = AlbumImpl(this).also { it.addImage(createImage(this)) } + } + val sone2 = object : IdOnlySone("2") { + override fun getRootAlbum() = AlbumImpl(this).also { it.addImage(createImage(this)); it.addImage(createImage(this)) } + } + assertThat(imageCountComparator.compare(sone1, sone2), greaterThan(0)) + } + + @Test + fun `image count comparator treats Sones as equal if number of images is the same`() { + val sone1 = object : IdOnlySone("1") { + override fun getRootAlbum() = AlbumImpl(this).also { it.addImage(createImage(this)) } + } + val sone2 = object : IdOnlySone("2") { + override fun getRootAlbum() = AlbumImpl(this).also { it.addImage(createImage(this)) } + } + assertThat(imageCountComparator.compare(sone1, sone2), equalTo(0)) + } + } diff --git a/src/test/kotlin/net/pterodactylus/sone/test/Mocks.kt b/src/test/kotlin/net/pterodactylus/sone/test/Mocks.kt index b273b82..65f279a 100644 --- a/src/test/kotlin/net/pterodactylus/sone/test/Mocks.kt +++ b/src/test/kotlin/net/pterodactylus/sone/test/Mocks.kt @@ -61,3 +61,6 @@ fun emptyPostReply(text: String = "", post: Post? = createPost(), sone: Sone = r override fun isKnown() = known override fun setKnown(known: Boolean): PostReply = this } + +fun createImage(sone: Sone): Image = + ImageImpl().modify().setSone(sone).update() -- 2.7.4