Skip to content

Commit

Permalink
add component decoder
Browse files Browse the repository at this point in the history
  • Loading branch information
yannicklamprecht committed Feb 12, 2024
1 parent f5cfc5d commit b9d563c
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -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.
*
* <p>For both serialization and deserialization, use {@link ComponentSerializer}</p>
*
* @param <S> the serialized type
* @param <O> the output component type
* @since 4.16.0
*/
public interface ComponentDecoder<S, O extends Component> {
/**
* 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}.
*
* <p>If {@code input} is {@code null}, then {@code null} will be returned.</p>
*
* @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}.
*
* <p>If {@code input} is {@code null}, then {@code fallback} will be returned.</p>
*
* @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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,15 @@
* @param <R> the serialized type
* @since 4.0.0
*/
public interface ComponentSerializer<I extends Component, O extends Component, R> extends ComponentEncoder<I, R> {
public interface ComponentSerializer<I extends Component, O extends Component, R> extends ComponentEncoder<I, R>, ComponentDecoder<R, O> {
/**
* Deserialize a component from input of type {@code R}.
*
* @param input the input
* @return the component
* @since 4.0.0
*/
@Override
@NotNull O deserialize(final @NotNull R input);

/**
Expand All @@ -61,7 +62,7 @@ public interface ComponentSerializer<I extends Component, O extends Component, R
@Contract(value = "!null -> !null; null -> null", pure = true)
@Deprecated
default @Nullable O deseializeOrNull(final @Nullable R input) {
return this.deserializeOrNull(input);
return ComponentDecoder.super.deserializeOrNull(input);
}

/**
Expand All @@ -74,8 +75,9 @@ public interface ComponentSerializer<I extends Component, O extends Component, R
* @since 4.8.0
*/
@Contract(value = "!null -> !null; null -> null", pure = true)
@Override
default @Nullable O deserializeOrNull(final @Nullable R input) {
return this.deserializeOr(input, null);
return ComponentDecoder.super.deserializeOr(input, null);
}

/**
Expand All @@ -89,10 +91,9 @@ public interface ComponentSerializer<I extends Component, O extends Component, R
* @since 4.7.0
*/
@Contract(value = "!null, _ -> !null; null, _ -> param2", pure = true)
@Override
default @Nullable O deserializeOr(final @Nullable R input, final @Nullable O fallback) {
if (input == null) return fallback;

return this.deserialize(input);
return ComponentDecoder.super.deserializeOr(input, fallback);
}

/**
Expand Down

0 comments on commit b9d563c

Please sign in to comment.