🔥 Remove another function from Sone interface
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 18 Feb 2020 13:50:45 +0000 (14:50 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 18 Feb 2020 14:15:32 +0000 (15:15 +0100)
src/main/java/net/pterodactylus/sone/data/Sone.java
src/main/kotlin/net/pterodactylus/sone/data/Albums.kt
src/main/kotlin/net/pterodactylus/sone/database/memory/MemoryDatabase.kt
src/test/kotlin/net/pterodactylus/sone/data/AlbumsTest.kt

index 843400e..2b0a2eb 100644 (file)
 
 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<Sone> {
                downloading,
        }
 
-       public static final Function<Sone, List<Album>> toAllAlbums = new Function<Sone, List<Album>>() {
-               @Override
-               public List<Album> apply(@Nullable Sone sone) {
-                       return (sone == null) ? Collections.<Album>emptyList() : FLATTENER.apply(
-                                       sone.getRootAlbum());
-               }
-       };
-
        /**
         * Returns the identity of this Sone.
         *
index 0c79a84..081a2fa 100644 (file)
@@ -21,3 +21,11 @@ package net.pterodactylus.sone.data
 val Album.allImages: Collection<Image>
        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<Album>
+       get() =
+               listOf(this) + albums.flatMap(Album::allAlbums)
index e62d968..df65a5a 100644 (file)
@@ -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)
index 26af4e9..6044fd1 100644 (file)
@@ -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>(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)))
+       }
+
 }