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;
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
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) {
comparing<Sone, Int> { it.posts.size }
.thenComparing<Int> { it.replies.size }
.reversed()
+
+val imageCountComparator: Comparator<Sone> =
+ comparing<Sone, Int> { it.rootAlbum.allImages.size }.reversed()
.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
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))
+ }
+
}
override fun isKnown() = known
override fun setKnown(known: Boolean): PostReply = this
}
+
+fun createImage(sone: Sone): Image =
+ ImageImpl().modify().setSone(sone).update()