♻️ Remove “addPerformance” method
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 8 Mar 2024 10:47:10 +0000 (11:47 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 8 Mar 2024 10:47:10 +0000 (11:47 +0100)
This will allow easier converting the Movie class into a Kotlin data
class.

src/main/java/net/pterodactylus/rhynodge/filters/webpages/savoy/Movie.java
src/main/kotlin/net/pterodactylus/rhynodge/filters/webpages/savoy/MovieExtractor.kt
src/test/kotlin/net/pterodactylus/rhynodge/filters/webpages/savoy/MovieStateTest.kt
src/test/kotlin/net/pterodactylus/rhynodge/filters/webpages/savoy/SavoyMergerTest.kt

index 8d9f1f6..76c766f 100644 (file)
@@ -3,11 +3,13 @@ package net.pterodactylus.rhynodge.filters.webpages.savoy;
 import com.fasterxml.jackson.annotation.JsonProperty;
 
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.List;
 import java.util.Objects;
 import java.util.stream.Collectors;
 
 import static java.lang.String.format;
+import static java.util.Collections.emptyList;
 
 /**
  * Information about a movie.
@@ -37,9 +39,14 @@ public class Movie {
        }
 
        public Movie(String name, String imageUrl, String description) {
+               this(name, imageUrl, description, emptyList());
+       }
+
+       public Movie(String name, String imageUrl, String description, Collection<Performance> performances) {
                this.name = name;
                this.imageUrl = imageUrl;
                this.description = description;
+               this.performances.addAll(performances);
        }
 
        public String getName() {
@@ -58,10 +65,6 @@ public class Movie {
                return performances;
        }
 
-       public void addPerformance(Performance performance) {
-               performances.add(performance);
-       }
-
        @Override
        public String toString() {
                return format("%s (%s, %s, %s)", name, imageUrl, description, performances.stream().map(link -> String.format("%s: %s", link.getTime(), link.getLink())).collect(Collectors.joining(", ")));
index 39c3ab8..7f46077 100644 (file)
@@ -25,7 +25,7 @@ class MovieExtractor {
                .replace("&l;", "<")
                .replace("&g;", ">")
 
-       private fun JsonNode.extractMovie() = Movie(get("name").asText(), get("poster").get("original").asText().fixImageUrl(), get("description_long").asText()).apply {
+       private fun JsonNode.extractMovie() =
                this@extractMovie.get("performances")
                        .map { performance ->
                                val begin = LocalDateTime.parse(performance.get("begin").asText(), dateFormat)
@@ -33,8 +33,7 @@ class MovieExtractor {
                                val link = "https://savoy.premiumkino.de/vorstellung/${slug}/${String.format("%tY%<tm%<td", begin)}/${String.format("%tH%<tM", begin)}/${performance.get("crypt_id").asText()}"
                                Performance(begin, performance.get("release_type").asText(), link)
                        }
-                       .forEach(this::addPerformance)
-       }
+                       .let { Movie(get("name").asText(), get("poster").get("original").asText().fixImageUrl(), get("description_long").asText(), it) }
 
 }
 
index 77ff2d3..bb2fa9b 100644 (file)
@@ -20,9 +20,9 @@ class MovieStateTest {
        fun `summary contains date of earliest movie`() {
                val movieState = MovieState(
                        setOf(
-                               Movie("1", "", "").apply { addPerformance(Performance(LocalDateTime.of(2024, 2, 12, 18, 45), "", "")) },
-                               Movie("2", "", "").apply { addPerformance(Performance(LocalDateTime.of(2024, 3, 12, 15, 30), "", "")) },
-                               Movie("3", "", "").apply { addPerformance(Performance(LocalDateTime.of(2024, 2, 11, 21, 15), "", "")) }
+                               Movie("1", "", "", listOf(Performance(LocalDateTime.of(2024, 2, 12, 18, 45), "", ""))),
+                               Movie("2", "", "", listOf(Performance(LocalDateTime.of(2024, 3, 12, 15, 30), "", ""))),
+                               Movie("3", "", "", listOf(Performance(LocalDateTime.of(2024, 2, 11, 21, 15), "", "")))
                        ), emptySet()
                )
                assertThat(movieState.output(Reaction("", null, null, null)).summary(), containsString("2024-02-11"))
@@ -129,7 +129,7 @@ class MovieStateTest {
                val document = Jsoup.parse(html)
                assertThat(
                        document.select("section.daily-programmes li.performance")
-                               .map { performance -> listOf(".name", ".time", ".type").map { performance.select(it).text() }.joinToString(" - ")}, contains(
+                               .map { performance -> listOf(".name", ".time", ".type").map { performance.select(it).text() }.joinToString(" - ") }, contains(
                                "Movie 1 - 18:45 - 2D OV", "Movie 1 - 13:30 - 2D", "Movie 1 - 18:15 - 2D OmeU"
                        )
                )
@@ -145,10 +145,11 @@ private fun dateTime(dateTimeString: String) = LocalDateTime.of(
        dateTimeString.substring(11..12).toInt(),
 )
 
-private fun movie(name: String, imageUrl: String, description: String = "", vararg times: String) = Movie(name, imageUrl, description).apply {
-       times.map(::dateTime).map { Performance(it, "", "https://link/$it") }.forEach(this::addPerformance)
-}
+private fun movie(name: String, imageUrl: String, description: String = "", vararg times: String) =
+       times.map(::dateTime)
+               .map { Performance(it, "", "https://link/$it") }
+               .let { Movie(name, imageUrl, description, it) }
 
-private fun movie(name: String, imageUrl: String, description: String = "", vararg timesAndTypes: Pair<String, String>) = Movie(name, imageUrl, description).apply {
-       timesAndTypes.map { Performance(it.first.let(::dateTime), it.second, "https://link/${it.first}") }.forEach(this::addPerformance)
-}
+private fun movie(name: String, imageUrl: String, description: String = "", vararg timesAndTypes: Pair<String, String>) =
+       timesAndTypes.map { Performance(it.first.let(::dateTime), it.second, "https://link/${it.first}") }
+               .let { Movie(name, imageUrl, description, it) }
index 4afe2af..cd2c7e0 100644 (file)
@@ -52,50 +52,50 @@ class SavoyMergerTest {
 
        @Test
        fun `movies with different performances are still considered the same movie`() {
-               val oldState = MovieState(setOf(Movie("1", "").apply { addPerformance(Performance(LocalDateTime.of(2024, 2, 14, 18, 45), "", "")) }))
-               val newState = MovieState(setOf(Movie("1", "").apply { addPerformance(Performance(LocalDateTime.of(2024, 2, 15, 14, 30), "", "")) }))
+               val oldState = MovieState(setOf(Movie("1", "", "", listOf(Performance(LocalDateTime.of(2024, 2, 14, 18, 45), "", "")))))
+               val newState = MovieState(setOf(Movie("1", "", "", listOf(Performance(LocalDateTime.of(2024, 2, 15, 14, 30), "", "")))))
                val mergedState = merger.mergeStates(oldState, newState) as MovieState
                assertThat(mergedState.newMovies, empty())
        }
 
        @Test
        fun `merging states with movies starting the same day does not create a triggered state`() {
-               val oldState = MovieState(setOf(Movie("1", "").apply { addPerformance(Performance(LocalDateTime.of(2024, 2, 14, 18, 45), "", "")) }))
-               val newState = MovieState(setOf(Movie("1", "").apply { addPerformance(Performance(LocalDateTime.of(2024, 2, 14, 14, 30), "", "")) }))
+               val oldState = MovieState(setOf(Movie("1", "", "", listOf(Performance(LocalDateTime.of(2024, 2, 14, 18, 45), "", "")))))
+               val newState = MovieState(setOf(Movie("1", "", "", listOf(Performance(LocalDateTime.of(2024, 2, 14, 14, 30), "", "")))))
                val mergedState = merger.mergeStates(oldState, newState) as MovieState
                assertThat(mergedState.triggered(), equalTo(false))
        }
 
        @Test
        fun `merging states with movies starting on different days does create a triggered state`() {
-               val oldState = MovieState(setOf(Movie("1", "").apply { addPerformance(Performance(LocalDateTime.of(2024, 2, 14, 18, 45), "", "")) }))
-               val newState = MovieState(setOf(Movie("1", "").apply { addPerformance(Performance(LocalDateTime.of(2024, 2, 15, 14, 30), "", "")) }))
+               val oldState = MovieState(setOf(Movie("1", "", "", listOf(Performance(LocalDateTime.of(2024, 2, 14, 18, 45), "", "")))))
+               val newState = MovieState(setOf(Movie("1", "", "", listOf(Performance(LocalDateTime.of(2024, 2, 15, 14, 30), "", "")))))
                val mergedState = merger.mergeStates(oldState, newState) as MovieState
                assertThat(mergedState.triggered(), equalTo(true))
        }
 
        @Test
        fun `merging states where movies have different performances but still on the same day does not create a triggered state`() {
-               val oldState = MovieState(setOf(Movie("1", "").apply {
-                       addPerformance(Performance(LocalDateTime.of(2024, 2, 14, 14, 30), "", ""))
-                       addPerformance(Performance(LocalDateTime.of(2024, 2, 14, 18, 45), "", ""))
-               }))
-               val newState = MovieState(setOf(Movie("1", "").apply {
-                       addPerformance(Performance(LocalDateTime.of(2024, 2, 14, 18, 45), "", ""))
-               }))
+               val oldState = MovieState(setOf(Movie("1", "", "", listOf(
+                       Performance(LocalDateTime.of(2024, 2, 14, 14, 30), "", ""),
+                       Performance(LocalDateTime.of(2024, 2, 14, 18, 45), "", "")
+               ))))
+               val newState = MovieState(setOf(Movie("1", "", "", listOf(
+                       Performance(LocalDateTime.of(2024, 2, 14, 18, 45), "", "")
+               ))))
                val mergedState = merger.mergeStates(oldState, newState) as MovieState
                assertThat(mergedState.triggered(), equalTo(false))
        }
 
        @Test
        fun `merging states where movies have different performances and are not on the same day anymore does create a triggered state`() {
-               val oldState = MovieState(setOf(Movie("1", "").apply {
-                       addPerformance(Performance(LocalDateTime.of(2024, 2, 14, 18, 45), "", ""))
-                       addPerformance(Performance(LocalDateTime.of(2024, 2, 15, 14, 30), "", ""))
-               }))
-               val newState = MovieState(setOf(Movie("1", "").apply {
-                       addPerformance(Performance(LocalDateTime.of(2024, 2, 15, 14, 30), "", ""))
-               }))
+               val oldState = MovieState(setOf(Movie("1", "", "", listOf(
+                       Performance(LocalDateTime.of(2024, 2, 14, 18, 45), "", ""),
+                       Performance(LocalDateTime.of(2024, 2, 15, 14, 30), "", "")
+               ))))
+               val newState = MovieState(setOf(Movie("1", "", "", listOf(
+                       Performance(LocalDateTime.of(2024, 2, 15, 14, 30), "", "")
+               ))))
                val mergedState = merger.mergeStates(oldState, newState) as MovieState
                assertThat(mergedState.triggered(), equalTo(true))
        }