🔥 Replace NOT_EMPTY with Kotlin version
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 18 Feb 2020 14:36:46 +0000 (15:36 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 18 Feb 2020 15:43:44 +0000 (16:43 +0100)
src/main/java/net/pterodactylus/sone/core/Core.java
src/main/java/net/pterodactylus/sone/core/SoneInserter.java
src/main/java/net/pterodactylus/sone/data/Album.java
src/main/java/net/pterodactylus/sone/data/impl/SoneImpl.java
src/main/kotlin/net/pterodactylus/sone/data/Albums.kt
src/test/kotlin/net/pterodactylus/sone/data/AlbumsTest.kt

index ce5daa2..7a8f5df 100644 (file)
@@ -53,6 +53,7 @@ import net.pterodactylus.sone.core.ConfigurationSoneParser.InvalidPostFound;
 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;
@@ -90,7 +91,6 @@ import net.pterodactylus.util.thread.NamedThreadFactory;
 
 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;
@@ -1392,7 +1392,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                        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) {
index 311d006..a67c15d 100644 (file)
@@ -21,7 +21,6 @@ import static java.lang.String.format;
 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;
@@ -41,6 +40,7 @@ import net.pterodactylus.sone.core.event.SoneInsertAbortedEvent;
 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;
@@ -312,7 +312,7 @@ public class SoneInserter extends AbstractService {
                        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);
                }
 
index 576c73a..efaa470 100644 (file)
@@ -64,32 +64,6 @@ public interface Album extends Identified, Fingerprintable {
        };
 
        /**
-        * 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
index 093b038..87bd7a6 100644 (file)
@@ -37,6 +37,7 @@ import javax.annotation.Nonnull;
 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;
@@ -649,7 +650,7 @@ public class SoneImpl implements Sone {
 
                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);
index 081a2fa..da71d62 100644 (file)
@@ -29,3 +29,10 @@ val Album.allImages: Collection<Image>
 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)
+       }
+}
index 6044fd1..1f938a3 100644 (file)
@@ -43,7 +43,7 @@ class AlbumsTest {
                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`() {
@@ -62,4 +62,56 @@ class AlbumsTest {
                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))
+       }
+
 }