From 2b7b9baa754ba9d4a41b0abfaf946bd9146a10c9 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Tue, 18 Feb 2020 14:50:45 +0100 Subject: [PATCH] =?utf8?q?=F0=9F=94=A5=20Remove=20another=20function=20fro?= =?utf8?q?m=20Sone=20interface?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- src/main/java/net/pterodactylus/sone/data/Sone.java | 13 ------------- src/main/kotlin/net/pterodactylus/sone/data/Albums.kt | 8 ++++++++ .../sone/database/memory/MemoryDatabase.kt | 8 ++++---- .../kotlin/net/pterodactylus/sone/data/AlbumsTest.kt | 17 +++++++++++++++++ 4 files changed, 29 insertions(+), 17 deletions(-) diff --git a/src/main/java/net/pterodactylus/sone/data/Sone.java b/src/main/java/net/pterodactylus/sone/data/Sone.java index 843400e..2b0a2eb 100644 --- a/src/main/java/net/pterodactylus/sone/data/Sone.java +++ b/src/main/java/net/pterodactylus/sone/data/Sone.java @@ -17,10 +17,7 @@ package net.pterodactylus.sone.data; -import static net.pterodactylus.sone.data.Album.FLATTENER; - import java.util.Collection; -import java.util.Collections; import java.util.List; import java.util.Set; @@ -31,8 +28,6 @@ import net.pterodactylus.sone.freenet.wot.Identity; import freenet.keys.FreenetURI; -import com.google.common.base.Function; - /** * A Sone defines everything about a user: her profile, her status updates, her * replies, her likes and dislikes, etc. @@ -57,14 +52,6 @@ public interface Sone extends Identified, Fingerprintable, Comparable { downloading, } - public static final Function> toAllAlbums = new Function>() { - @Override - public List apply(@Nullable Sone sone) { - return (sone == null) ? Collections.emptyList() : FLATTENER.apply( - sone.getRootAlbum()); - } - }; - /** * Returns the identity of this Sone. * diff --git a/src/main/kotlin/net/pterodactylus/sone/data/Albums.kt b/src/main/kotlin/net/pterodactylus/sone/data/Albums.kt index 0c79a84..081a2fa 100644 --- a/src/main/kotlin/net/pterodactylus/sone/data/Albums.kt +++ b/src/main/kotlin/net/pterodactylus/sone/data/Albums.kt @@ -21,3 +21,11 @@ package net.pterodactylus.sone.data val Album.allImages: Collection get() = images + albums.flatMap { it.allImages } + +/** + * Returns this album and all albums contained in this album (recursively). + * A child album is always listed after its parent. + */ +val Album.allAlbums: List + get() = + listOf(this) + albums.flatMap(Album::allAlbums) diff --git a/src/main/kotlin/net/pterodactylus/sone/database/memory/MemoryDatabase.kt b/src/main/kotlin/net/pterodactylus/sone/database/memory/MemoryDatabase.kt index e62d968..df65a5a 100644 --- a/src/main/kotlin/net/pterodactylus/sone/database/memory/MemoryDatabase.kt +++ b/src/main/kotlin/net/pterodactylus/sone/database/memory/MemoryDatabase.kt @@ -30,7 +30,7 @@ import net.pterodactylus.sone.data.Post import net.pterodactylus.sone.data.PostReply import net.pterodactylus.sone.data.Reply.TIME_COMPARATOR import net.pterodactylus.sone.data.Sone -import net.pterodactylus.sone.data.Sone.toAllAlbums +import net.pterodactylus.sone.data.allAlbums import net.pterodactylus.sone.data.allImages import net.pterodactylus.sone.data.impl.AlbumBuilderImpl import net.pterodactylus.sone.data.impl.ImageBuilderImpl @@ -123,9 +123,9 @@ class MemoryDatabase @Inject constructor(private val configuration: Configuratio for (postReply in sone.replies) { allPostReplies[postReply.id] = postReply } - soneAlbums.putAll(sone.id, toAllAlbums.apply(sone)!!) - for (album in toAllAlbums.apply(sone)!!) { - allAlbums[album.id] = album + sone.rootAlbum.allAlbums.let { albums -> + soneAlbums.putAll(sone.id, albums) + albums.forEach { album -> allAlbums[album.id] = album } } sone.rootAlbum.allImages.let { images -> soneImages.putAll(sone.id, images) diff --git a/src/test/kotlin/net/pterodactylus/sone/data/AlbumsTest.kt b/src/test/kotlin/net/pterodactylus/sone/data/AlbumsTest.kt index 26af4e9..6044fd1 100644 --- a/src/test/kotlin/net/pterodactylus/sone/data/AlbumsTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/data/AlbumsTest.kt @@ -45,4 +45,21 @@ class AlbumsTest { private fun createImage(sone: IdOnlySone, id: String) = ImageImpl(id).modify().setSone(sone).update() + @Test + fun `allAlbums returns itself and all its subalbums`() { + val sone = IdOnlySone("sone") + val album = AlbumImpl(sone) + val firstNestedAlbum = AlbumImpl(sone) + val secondNestedAlbum = AlbumImpl(sone) + val albumNestedInFirst = AlbumImpl(sone) + album.addAlbum(firstNestedAlbum) + album.addAlbum(secondNestedAlbum) + firstNestedAlbum.addAlbum(albumNestedInFirst) + val albums = album.allAlbums + assertThat(albums, containsInAnyOrder(album, firstNestedAlbum, secondNestedAlbum, albumNestedInFirst)) + assertThat(albums.indexOf(firstNestedAlbum), greaterThan(albums.indexOf(album))) + assertThat(albums.indexOf(secondNestedAlbum), greaterThan(albums.indexOf(album))) + assertThat(albums.indexOf(albumNestedInFirst), greaterThan(albums.indexOf(firstNestedAlbum))) + } + } -- 2.7.4