From 344c25bffd290586dbbb3f0d2b67e36308d6cd57 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Fri, 8 Mar 2024 11:47:10 +0100 Subject: [PATCH] =?utf8?q?=E2=99=BB=EF=B8=8F=20Remove=20=E2=80=9CaddPerfor?= =?utf8?q?mance=E2=80=9D=20method?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit This will allow easier converting the Movie class into a Kotlin data class. --- .../rhynodge/filters/webpages/savoy/Movie.java | 11 +++--- .../filters/webpages/savoy/MovieExtractor.kt | 5 ++- .../filters/webpages/savoy/MovieStateTest.kt | 21 ++++++------ .../filters/webpages/savoy/SavoyMergerTest.kt | 40 +++++++++++----------- 4 files changed, 40 insertions(+), 37 deletions(-) diff --git a/src/main/java/net/pterodactylus/rhynodge/filters/webpages/savoy/Movie.java b/src/main/java/net/pterodactylus/rhynodge/filters/webpages/savoy/Movie.java index 8d9f1f6..76c766f 100644 --- a/src/main/java/net/pterodactylus/rhynodge/filters/webpages/savoy/Movie.java +++ b/src/main/java/net/pterodactylus/rhynodge/filters/webpages/savoy/Movie.java @@ -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 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(", "))); diff --git a/src/main/kotlin/net/pterodactylus/rhynodge/filters/webpages/savoy/MovieExtractor.kt b/src/main/kotlin/net/pterodactylus/rhynodge/filters/webpages/savoy/MovieExtractor.kt index 39c3ab8..7f46077 100644 --- a/src/main/kotlin/net/pterodactylus/rhynodge/filters/webpages/savoy/MovieExtractor.kt +++ b/src/main/kotlin/net/pterodactylus/rhynodge/filters/webpages/savoy/MovieExtractor.kt @@ -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% 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) = 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) = + timesAndTypes.map { Performance(it.first.let(::dateTime), it.second, "https://link/${it.first}") } + .let { Movie(name, imageUrl, description, it) } diff --git a/src/test/kotlin/net/pterodactylus/rhynodge/filters/webpages/savoy/SavoyMergerTest.kt b/src/test/kotlin/net/pterodactylus/rhynodge/filters/webpages/savoy/SavoyMergerTest.kt index 4afe2af..cd2c7e0 100644 --- a/src/test/kotlin/net/pterodactylus/rhynodge/filters/webpages/savoy/SavoyMergerTest.kt +++ b/src/test/kotlin/net/pterodactylus/rhynodge/filters/webpages/savoy/SavoyMergerTest.kt @@ -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)) } -- 2.7.4