🎨 Replace image count comparator with Kotlin version
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sun, 16 Feb 2020 20:49:23 +0000 (21:49 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sun, 16 Feb 2020 20:49:23 +0000 (21:49 +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
src/test/kotlin/net/pterodactylus/sone/test/Mocks.kt

index 4dd9a8e..836baa1 100644 (file)
 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<Sone> {
                downloading,
        }
 
-       /** Comparator that sorts Sones by number of images (descending). */
-       public static final Comparator<Sone> IMAGE_COUNT_COMPARATOR = new Comparator<Sone>() {
-
-               /**
-                * {@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<Sone, List<Album>> toAllAlbums = new Function<Sone, List<Album>>() {
                @Override
                public List<Album> apply(@Nullable Sone sone) {
index c44d6e8..3f70ad7 100644 (file)
@@ -48,3 +48,6 @@ val postCountComparator: Comparator<Sone> =
                comparing<Sone, Int> { it.posts.size }
                                .thenComparing<Int> { it.replies.size }
                                .reversed()
+
+val imageCountComparator: Comparator<Sone> =
+               comparing<Sone, Int> { it.rootAlbum.allImages.size }.reversed()
index 392dd93..01d1d22 100644 (file)
@@ -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
index 6b7e51d..1429e54 100644 (file)
@@ -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))
+       }
+
 }
index b273b82..65f279a 100644 (file)
@@ -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()