From c00c2dea5bbe698a46c443d3610855720baa69a4 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Tue, 18 Feb 2020 16:57:00 +0100 Subject: [PATCH] =?utf8?q?=F0=9F=9A=9A=20Rename=20Album=20helper=20file?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../java/net/pterodactylus/sone/core/Core.java | 2 +- .../net/pterodactylus/sone/core/SoneInserter.java | 4 +- .../net/pterodactylus/sone/data/impl/SoneImpl.java | 4 +- .../kotlin/net/pterodactylus/sone/data/Album.kt | 38 ++++++ .../kotlin/net/pterodactylus/sone/data/Albums.kt | 38 ------ .../net/pterodactylus/sone/data/AlbumTest.kt | 136 +++++++++++++++++++++ .../net/pterodactylus/sone/data/AlbumsTest.kt | 136 --------------------- 7 files changed, 179 insertions(+), 179 deletions(-) create mode 100644 src/main/kotlin/net/pterodactylus/sone/data/Album.kt delete mode 100644 src/main/kotlin/net/pterodactylus/sone/data/Albums.kt create mode 100644 src/test/kotlin/net/pterodactylus/sone/data/AlbumTest.kt delete mode 100644 src/test/kotlin/net/pterodactylus/sone/data/AlbumsTest.kt diff --git a/src/main/java/net/pterodactylus/sone/core/Core.java b/src/main/java/net/pterodactylus/sone/core/Core.java index e186e6f..c4cc9cf 100644 --- a/src/main/java/net/pterodactylus/sone/core/Core.java +++ b/src/main/java/net/pterodactylus/sone/core/Core.java @@ -24,7 +24,7 @@ import static com.google.common.primitives.Longs.tryParse; import static java.lang.String.format; import static java.util.logging.Level.WARNING; import static java.util.logging.Logger.getLogger; -import static net.pterodactylus.sone.data.AlbumsKt.getAllImages; +import static net.pterodactylus.sone.data.AlbumKt.getAllImages; import java.util.ArrayList; import java.util.Collection; diff --git a/src/main/java/net/pterodactylus/sone/core/SoneInserter.java b/src/main/java/net/pterodactylus/sone/core/SoneInserter.java index e74c98d..651b62e 100644 --- a/src/main/java/net/pterodactylus/sone/core/SoneInserter.java +++ b/src/main/java/net/pterodactylus/sone/core/SoneInserter.java @@ -40,7 +40,7 @@ import net.pterodactylus.sone.core.event.InsertionDelayChangedEvent; 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.AlbumsKt; +import net.pterodactylus.sone.data.AlbumKt; 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", SoneKt.getAllAlbums(sone).stream().filter(AlbumsKt.notEmpty()::invoke).collect(toList())); + soneProperties.put("albums", SoneKt.getAllAlbums(sone).stream().filter(AlbumKt.notEmpty()::invoke).collect(toList())); manifestCreator = new ManifestCreator(core, soneProperties); } 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 87bd7a6..8f5a50f 100644 --- a/src/main/java/net/pterodactylus/sone/data/impl/SoneImpl.java +++ b/src/main/java/net/pterodactylus/sone/data/impl/SoneImpl.java @@ -37,7 +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.AlbumKt; import net.pterodactylus.sone.data.Client; import net.pterodactylus.sone.data.Post; import net.pterodactylus.sone.data.PostReply; @@ -650,7 +650,7 @@ public class SoneImpl implements Sone { hash.putString("Albums(", UTF_8); for (Album album : rootAlbum.getAlbums()) { - if (!AlbumsKt.notEmpty().invoke(album)) { + if (!AlbumKt.notEmpty().invoke(album)) { continue; } hash.putString(album.getFingerprint(), UTF_8); diff --git a/src/main/kotlin/net/pterodactylus/sone/data/Album.kt b/src/main/kotlin/net/pterodactylus/sone/data/Album.kt new file mode 100644 index 0000000..991c0ec --- /dev/null +++ b/src/main/kotlin/net/pterodactylus/sone/data/Album.kt @@ -0,0 +1,38 @@ +/** + * Sone - Album.kt - Copyright © 2019–2020 David ‘Bombe’ Roden + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.pterodactylus.sone.data + +/** Returns all images contained in this album and all its albums. */ +val Album.allImages: Collection + get() = + images + albums.flatMap(Album::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 + 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/main/kotlin/net/pterodactylus/sone/data/Albums.kt b/src/main/kotlin/net/pterodactylus/sone/data/Albums.kt deleted file mode 100644 index a38e9dd..0000000 --- a/src/main/kotlin/net/pterodactylus/sone/data/Albums.kt +++ /dev/null @@ -1,38 +0,0 @@ -/** - * Sone - Albums.kt - Copyright © 2019–2020 David ‘Bombe’ Roden - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package net.pterodactylus.sone.data - -/** Returns all images contained in this album and all its albums. */ -val Album.allImages: Collection - get() = - images + albums.flatMap(Album::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 - 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/AlbumTest.kt b/src/test/kotlin/net/pterodactylus/sone/data/AlbumTest.kt new file mode 100644 index 0000000..fe11c2c --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/sone/data/AlbumTest.kt @@ -0,0 +1,136 @@ +/** + * Sone - AlbumTest.kt - Copyright © 2019–2020 David ‘Bombe’ Roden + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.pterodactylus.sone.data + +import net.pterodactylus.sone.data.impl.* +import org.hamcrest.MatcherAssert.* +import org.hamcrest.Matchers.* +import kotlin.test.* + +/** + * Unit test for various helper method in `Album.kt`. + */ +class AlbumTest { + + @Test + fun `recursive list of all images for album is returned correctly`() { + val sone = IdOnlySone("sone") + val album = AlbumImpl(sone) + val firstNestedAlbum = AlbumImpl(sone) + val secondNestedAlbum = AlbumImpl(sone) + firstNestedAlbum.addImage(createImage(sone, "image-1")) + firstNestedAlbum.addImage(createImage(sone, "image-2")) + secondNestedAlbum.addImage(createImage(sone, "image-3")) + album.addImage(createImage(sone, "image-4")) + album.addAlbum(firstNestedAlbum) + album.addAlbum(secondNestedAlbum) + val images = album.allImages + assertThat(images.map(Image::id), containsInAnyOrder("image-1", "image-2", "image-3", "image-4")) + } + + 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`() { + 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, 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))) + } + + @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)) + } + + @Test + fun `allImages returns images from album`() { + val sone = IdOnlySone("sone") + val album = AlbumImpl(sone) + val image1 = createImage(sone, "1").also(album::addImage) + val image2 = createImage(sone, "2").also(album::addImage) + assertThat(album.allImages, contains(image1, image2)) + } + + @Test + fun `allImages returns images from subalbum`() { + val sone = IdOnlySone("sone") + val album1 = AlbumImpl(sone) + val album2 = AlbumImpl(sone).also(album1::addAlbum) + val image1 = createImage(sone, "1").also(album1::addImage) + val image2 = createImage(sone, "2").also(album2::addImage) + assertThat(album1.allImages, contains(image1, image2)) + } + +} diff --git a/src/test/kotlin/net/pterodactylus/sone/data/AlbumsTest.kt b/src/test/kotlin/net/pterodactylus/sone/data/AlbumsTest.kt deleted file mode 100644 index 6821a24..0000000 --- a/src/test/kotlin/net/pterodactylus/sone/data/AlbumsTest.kt +++ /dev/null @@ -1,136 +0,0 @@ -/** - * Sone - AlbumsTest.kt - Copyright © 2019–2020 David ‘Bombe’ Roden - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package net.pterodactylus.sone.data - -import net.pterodactylus.sone.data.impl.* -import org.hamcrest.MatcherAssert.* -import org.hamcrest.Matchers.* -import kotlin.test.* - -/** - * Unit test for various helper method in `Albums.kt`. - */ -class AlbumsTest { - - @Test - fun `recursive list of all images for album is returned correctly`() { - val sone = IdOnlySone("sone") - val album = AlbumImpl(sone) - val firstNestedAlbum = AlbumImpl(sone) - val secondNestedAlbum = AlbumImpl(sone) - firstNestedAlbum.addImage(createImage(sone, "image-1")) - firstNestedAlbum.addImage(createImage(sone, "image-2")) - secondNestedAlbum.addImage(createImage(sone, "image-3")) - album.addImage(createImage(sone, "image-4")) - album.addAlbum(firstNestedAlbum) - album.addAlbum(secondNestedAlbum) - val images = album.allImages - assertThat(images.map(Image::id), containsInAnyOrder("image-1", "image-2", "image-3", "image-4")) - } - - 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`() { - 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, 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))) - } - - @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)) - } - - @Test - fun `allImages returns images from album`() { - val sone = IdOnlySone("sone") - val album = AlbumImpl(sone) - val image1 = createImage(sone, "1").also(album::addImage) - val image2 = createImage(sone, "2").also(album::addImage) - assertThat(album.allImages, contains(image1, image2)) - } - - @Test - fun `allImages returns images from subalbum`() { - val sone = IdOnlySone("sone") - val album1 = AlbumImpl(sone) - val album2 = AlbumImpl(sone).also(album1::addAlbum) - val image1 = createImage(sone, "1").also(album1::addImage) - val image2 = createImage(sone, "2").also(album2::addImage) - assertThat(album1.allImages, contains(image1, image2)) - } - -} -- 2.7.4