import net.pterodactylus.sone.core.ConfigurationSoneParser.InvalidPostReplyFound;
import net.pterodactylus.sone.core.event.*;
import net.pterodactylus.sone.data.Album;
+import net.pterodactylus.sone.data.AlbumsKt;
import net.pterodactylus.sone.data.Client;
import net.pterodactylus.sone.data.Image;
import net.pterodactylus.sone.data.Post;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Stopwatch;
-import com.google.common.collect.FluentIterable;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;
import com.google.common.collect.Multimaps;
configuration.getStringValue(sonePrefix + "/Likes/Reply/" + replyLikeCounter + "/ID").setValue(null);
/* save albums. first, collect in a flat structure, top-level first. */
- List<Album> albums = FluentIterable.from(sone.getRootAlbum().getAlbums()).transformAndConcat(Album.FLATTENER).toList();
+ List<Album> albums = AlbumsKt.getAllAlbums(sone.getRootAlbum());
int albumCounter = 0;
for (Album album : albums) {
import static java.lang.System.currentTimeMillis;
import static java.util.concurrent.TimeUnit.*;
import static java.util.logging.Logger.getLogger;
-import static net.pterodactylus.sone.data.Album.NOT_EMPTY;
import java.io.*;
import java.nio.charset.Charset;
import net.pterodactylus.sone.core.event.SoneInsertedEvent;
import net.pterodactylus.sone.core.event.SoneInsertingEvent;
import net.pterodactylus.sone.data.Album;
+import net.pterodactylus.sone.data.AlbumsKt;
import net.pterodactylus.sone.data.Post;
import net.pterodactylus.sone.data.Reply;
import net.pterodactylus.sone.data.Sone;
soneProperties.put("replies", Ordering.from(Reply.TIME_COMPARATOR).reverse().sortedCopy(sone.getReplies()));
soneProperties.put("likedPostIds", new HashSet<>(sone.getLikedPostIds()));
soneProperties.put("likedReplyIds", new HashSet<>(sone.getLikedReplyIds()));
- soneProperties.put("albums", FluentIterable.from(sone.getRootAlbum().getAlbums()).transformAndConcat(Album.FLATTENER).filter(NOT_EMPTY).toList());
+ soneProperties.put("albums", FluentIterable.from(sone.getRootAlbum().getAlbums()).transformAndConcat(Album.FLATTENER).filter(AlbumsKt.notEmpty()::invoke).toList());
manifestCreator = new ManifestCreator(core, soneProperties);
}
};
/**
- * Filter that removes all albums that do not have any images in any album
- * below it.
- */
- Predicate<Album> NOT_EMPTY = new Predicate<Album>() {
-
- @Override
- public boolean apply(Album album) {
- /* so, we flatten all albums below the given one and check whether at least one album… */
- return FluentIterable.from(asList(album)).transformAndConcat(FLATTENER).anyMatch(new Predicate<Album>() {
-
- @Override
- public boolean apply(Album album) {
- /* …contains any inserted images. */
- return !album.getImages().isEmpty() && FluentIterable.from(album.getImages()).allMatch(new Predicate<Image>() {
-
- @Override
- public boolean apply(Image input) {
- return input.isInserted();
- }
- });
- }
- });
- }
- };
-
- /**
* Returns the ID of this album.
*
* @return The ID of this album
import javax.annotation.Nullable;
import net.pterodactylus.sone.data.Album;
+import net.pterodactylus.sone.data.AlbumsKt;
import net.pterodactylus.sone.data.Client;
import net.pterodactylus.sone.data.Post;
import net.pterodactylus.sone.data.PostReply;
hash.putString("Albums(", UTF_8);
for (Album album : rootAlbum.getAlbums()) {
- if (!Album.NOT_EMPTY.apply(album)) {
+ if (!AlbumsKt.notEmpty().invoke(album)) {
continue;
}
hash.putString(album.getFingerprint(), UTF_8);
val Album.allAlbums: List<Album>
get() =
listOf(this) + albums.flatMap(Album::allAlbums)
+
+@get:JvmName("notEmpty")
+val notEmpty: (Album) -> Boolean = { album ->
+ album.allImages.let { images ->
+ images.isNotEmpty() && images.any(Image::isInserted)
+ }
+}
assertThat(images.map(Image::id), containsInAnyOrder("image-1", "image-2", "image-3", "image-4"))
}
- private fun createImage(sone: IdOnlySone, id: String) = ImageImpl(id).modify().setSone(sone).update()
+ private fun createImage(sone: IdOnlySone, id: String, key: String? = null) = ImageImpl(id).modify().setSone(sone).setKey(key).update()
@Test
fun `allAlbums returns itself and all its subalbums`() {
assertThat(albums.indexOf(albumNestedInFirst), greaterThan(albums.indexOf(firstNestedAlbum)))
}
+ @Test
+ fun `notEmpty finds album without images is empty`() {
+ val sone = IdOnlySone("sone")
+ val album = AlbumImpl(sone)
+ assertThat(notEmpty(album), equalTo(false))
+ }
+
+ @Test
+ fun `notEmpty finds album with one inserted image is not empty`() {
+ val sone = IdOnlySone("sone")
+ val album = AlbumImpl(sone)
+ album.addImage(createImage(sone, "1", "key"))
+ assertThat(notEmpty(album), equalTo(true))
+ }
+
+ @Test
+ fun `notEmpty finds album with one not-inserted image is empty`() {
+ val sone = IdOnlySone("sone")
+ val album = AlbumImpl(sone)
+ album.addImage(createImage(sone, "1"))
+ assertThat(notEmpty(album), equalTo(false))
+ }
+
+ @Test
+ fun `notEmpty finds album with empty subalbums is empty`() {
+ val sone = IdOnlySone("sone")
+ val album = AlbumImpl(sone)
+ val firstNestedAlbum = AlbumImpl(sone)
+ album.addAlbum(firstNestedAlbum)
+ assertThat(notEmpty(album), equalTo(false))
+ }
+
+ @Test
+ fun `notEmpty finds album with subalbum with not inserted image is empty`() {
+ val sone = IdOnlySone("sone")
+ val album = AlbumImpl(sone)
+ val firstNestedAlbum = AlbumImpl(sone)
+ firstNestedAlbum.addImage(createImage(sone, "1"))
+ album.addAlbum(firstNestedAlbum)
+ assertThat(notEmpty(album), equalTo(false))
+ }
+
+ @Test
+ fun `notEmpty finds album with subalbum with inserted image is not empty`() {
+ val sone = IdOnlySone("sone")
+ val album = AlbumImpl(sone)
+ val firstNestedAlbum = AlbumImpl(sone)
+ firstNestedAlbum.addImage(createImage(sone, "1", "key"))
+ album.addAlbum(firstNestedAlbum)
+ assertThat(notEmpty(album), equalTo(true))
+ }
+
}