From: David ‘Bombe’ Roden Date: Wed, 8 Oct 2025 16:17:14 +0000 (+0200) Subject: ♻️ Extract wrapper method for kotlinx.html X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=8303ee4983dd58237b7a3b0e20765f80d4d5b3d3;p=rhynodge.git ♻️ Extract wrapper method for kotlinx.html --- diff --git a/src/main/java/net/pterodactylus/rhynodge/states/EpisodeState.java b/src/main/java/net/pterodactylus/rhynodge/states/EpisodeState.java index 52d6f1a..e266212 100644 --- a/src/main/java/net/pterodactylus/rhynodge/states/EpisodeState.java +++ b/src/main/java/net/pterodactylus/rhynodge/states/EpisodeState.java @@ -29,10 +29,6 @@ import java.util.Map; import java.util.Set; import java.util.concurrent.atomic.AtomicReference; -import java.util.function.Consumer; -import kotlin.Unit; -import kotlin.jvm.functions.Function1; -import kotlinx.html.Tag; import net.pterodactylus.rhynodge.Reaction; import net.pterodactylus.rhynodge.State; import net.pterodactylus.rhynodge.filters.EpisodeFilter; @@ -55,6 +51,7 @@ import static kotlinx.html.Gen_tags_tKt.th; import static kotlinx.html.Gen_tags_tKt.thead; import static kotlinx.html.Gen_tags_tKt.tr; import static kotlinx.html.stream.StreamKt.createHTML; +import static net.pterodactylus.util.dom.KotlinXHtml.wrapper; /** * {@link State} implementation that stores episodes of TV shows, parsed via @@ -256,13 +253,6 @@ public class EpisodeState extends AbstractState implements Iterable { return String.format("%s[episodes=%s]", getClass().getSimpleName(), episodes); } - private static Function1 wrapper(Consumer tagConsumer) { - return t -> { - tagConsumer.accept(t); - return null; - }; - } - /** * Stores attributes for an episode. * diff --git a/src/main/java/net/pterodactylus/util/dom/KotlinXHtml.java b/src/main/java/net/pterodactylus/util/dom/KotlinXHtml.java new file mode 100644 index 0000000..31f5f18 --- /dev/null +++ b/src/main/java/net/pterodactylus/util/dom/KotlinXHtml.java @@ -0,0 +1,41 @@ +package net.pterodactylus.util.dom; + +import java.util.function.Consumer; +import kotlin.Unit; +import kotlin.jvm.functions.Function1; +import kotlinx.html.Tag; + +/** + * Helper class that collects methods for use of {@link kotlinx.html} from + * Java. + */ +public class KotlinXHtml { + + /** + * Can be used to wrap the block body during HTML construction. It + * removes the need to construct lambdas that require a + * {@code return null;} at the end to satisfy {@link Function1}’s + * second type parameter, {@link Unit}. + * + *

Usage

+ *
+	 * html(output, null, wrapper(html -> {
+	 *     body(html, null, wrapper(body -> {
+	 *         // add more HTML
+	 *     }));
+	 * }));
+	 * 
+ * + * @param tagConsumer The body to wrap + * @param The type of tag + * @return A Kotlin {@link Function1} wrapper around the given + * {@code tagConsumer} + */ + public static Function1 wrapper(Consumer tagConsumer) { + return t -> { + tagConsumer.accept(t); + return null; + }; + } + +}