From b9d563c7acbd6a6ed1249ff3f42d437dd1a5337f Mon Sep 17 00:00:00 2001 From: Yannick Lamprecht Date: Sat, 10 Feb 2024 03:36:09 +0100 Subject: [PATCH] add component decoder --- .../text/serializer/ComponentDecoder.java | 80 +++++++++++++++++++ .../text/serializer/ComponentSerializer.java | 13 +-- 2 files changed, 87 insertions(+), 6 deletions(-) create mode 100644 api/src/main/java/net/kyori/adventure/text/serializer/ComponentDecoder.java diff --git a/api/src/main/java/net/kyori/adventure/text/serializer/ComponentDecoder.java b/api/src/main/java/net/kyori/adventure/text/serializer/ComponentDecoder.java new file mode 100644 index 000000000..2588b14e1 --- /dev/null +++ b/api/src/main/java/net/kyori/adventure/text/serializer/ComponentDecoder.java @@ -0,0 +1,80 @@ +/* + * This file is part of adventure, licensed under the MIT License. + * + * Copyright (c) 2017-2023 KyoriPowered + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package net.kyori.adventure.text.serializer; + +import net.kyori.adventure.text.Component; +import org.jetbrains.annotations.Contract; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +/** + * A {@link Component} decoder, which provides deserialization, but without serialization. + * + *

For both serialization and deserialization, use {@link ComponentSerializer}

+ * + * @param the serialized type + * @param the output component type + * @since 4.16.0 + */ +public interface ComponentDecoder { + /** + * Deserialize a component from input of type {@code R}. + * + * @param input the input + * @return the component + * @since 4.16.0 + */ + @NotNull O deserialize(final @NotNull S input); + + /** + * Deserialize a component from input of type {@code R}. + * + *

If {@code input} is {@code null}, then {@code null} will be returned.

+ * + * @param input the input + * @return the component if {@code input} is non-null, otherwise {@code null} + * @since 4.16.0 + */ + @Contract(value = "!null -> !null; null -> null", pure = true) + default @Nullable O deserializeOrNull(final @Nullable S input) { + return this.deserializeOr(input, null); + } + + /** + * Deserialize a component from input of type {@code R}. + * + *

If {@code input} is {@code null}, then {@code fallback} will be returned.

+ * + * @param input the input + * @param fallback the fallback value + * @return the component if {@code input} is non-null, otherwise {@code fallback} + * @since 4.16.0 + */ + @Contract(value = "!null, _ -> !null; null, _ -> param2", pure = true) + default @Nullable O deserializeOr(final @Nullable S input, final @Nullable O fallback) { + if (input == null) return fallback; + + return this.deserialize(input); + } +} diff --git a/api/src/main/java/net/kyori/adventure/text/serializer/ComponentSerializer.java b/api/src/main/java/net/kyori/adventure/text/serializer/ComponentSerializer.java index 264d16c3a..9caf3ce0c 100644 --- a/api/src/main/java/net/kyori/adventure/text/serializer/ComponentSerializer.java +++ b/api/src/main/java/net/kyori/adventure/text/serializer/ComponentSerializer.java @@ -37,7 +37,7 @@ * @param the serialized type * @since 4.0.0 */ -public interface ComponentSerializer extends ComponentEncoder { +public interface ComponentSerializer extends ComponentEncoder, ComponentDecoder { /** * Deserialize a component from input of type {@code R}. * @@ -45,6 +45,7 @@ public interface ComponentSerializer