From: David ‘Bombe’ Roden Date: Tue, 18 Feb 2020 14:36:46 +0000 (+0100) Subject: 🔥 Replace NOT_EMPTY with Kotlin version X-Git-Tag: v82^2~58 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=509b81185b3a1e82bb78308640d0d7b6b741d3e0;p=Sone.git 🔥 Replace NOT_EMPTY with Kotlin version --- diff --git a/src/main/java/net/pterodactylus/sone/core/Core.java b/src/main/java/net/pterodactylus/sone/core/Core.java index ce5daa2..7a8f5df 100644 --- a/src/main/java/net/pterodactylus/sone/core/Core.java +++ b/src/main/java/net/pterodactylus/sone/core/Core.java @@ -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 albums = FluentIterable.from(sone.getRootAlbum().getAlbums()).transformAndConcat(Album.FLATTENER).toList(); + List albums = AlbumsKt.getAllAlbums(sone.getRootAlbum()); int albumCounter = 0; for (Album album : albums) { diff --git a/src/main/java/net/pterodactylus/sone/core/SoneInserter.java b/src/main/java/net/pterodactylus/sone/core/SoneInserter.java index 311d006..a67c15d 100644 --- a/src/main/java/net/pterodactylus/sone/core/SoneInserter.java +++ b/src/main/java/net/pterodactylus/sone/core/SoneInserter.java @@ -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); } diff --git a/src/main/java/net/pterodactylus/sone/data/Album.java b/src/main/java/net/pterodactylus/sone/data/Album.java index 576c73a..efaa470 100644 --- a/src/main/java/net/pterodactylus/sone/data/Album.java +++ b/src/main/java/net/pterodactylus/sone/data/Album.java @@ -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 NOT_EMPTY = new Predicate() { - - @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() { - - @Override - public boolean apply(Album album) { - /* …contains any inserted images. */ - return !album.getImages().isEmpty() && FluentIterable.from(album.getImages()).allMatch(new Predicate() { - - @Override - public boolean apply(Image input) { - return input.isInserted(); - } - }); - } - }); - } - }; - - /** * Returns the ID of this album. * * @return The ID of this album diff --git a/src/main/java/net/pterodactylus/sone/data/impl/SoneImpl.java b/src/main/java/net/pterodactylus/sone/data/impl/SoneImpl.java index 093b038..87bd7a6 100644 --- a/src/main/java/net/pterodactylus/sone/data/impl/SoneImpl.java +++ b/src/main/java/net/pterodactylus/sone/data/impl/SoneImpl.java @@ -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); diff --git a/src/main/kotlin/net/pterodactylus/sone/data/Albums.kt b/src/main/kotlin/net/pterodactylus/sone/data/Albums.kt index 081a2fa..da71d62 100644 --- a/src/main/kotlin/net/pterodactylus/sone/data/Albums.kt +++ b/src/main/kotlin/net/pterodactylus/sone/data/Albums.kt @@ -29,3 +29,10 @@ val Album.allImages: Collection val Album.allAlbums: List 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) + } +} diff --git a/src/test/kotlin/net/pterodactylus/sone/data/AlbumsTest.kt b/src/test/kotlin/net/pterodactylus/sone/data/AlbumsTest.kt index 6044fd1..1f938a3 100644 --- a/src/test/kotlin/net/pterodactylus/sone/data/AlbumsTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/data/AlbumsTest.kt @@ -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)) + } + }