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;
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.
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.
*
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)
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
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)
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)))
+ }
+
}