Skip to content

Commit

Permalink
feat(api): add ComponentEncoder
Browse files Browse the repository at this point in the history
ComponentEncoder is akin to ComponentSerializer, except it doesn't
provide deserialization. This is intended to be used in #898.

ComponentSerializer now extends ComponentEncoder, but still overrides
the methods with exact copies, to keep the javadocs more clear.
These originals also retain their old `@since` tags.
  • Loading branch information
rymiel committed Apr 11, 2023
1 parent a7e7bc6 commit 1acc0f3
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 1 deletion.
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.encoder;

import net.kyori.adventure.text.Component;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
* A {@link Component} encoder, which provides serialization, but without deserialization.
*
* <p>For both serialization and deserialization, use {@link net.kyori.adventure.text.serializer.ComponentSerializer}</p>
*
* @param <I> the input component type
* @param <R> the serialized type
* @since 4.14.0
*/
public interface ComponentEncoder<I extends Component, R> {
/**
* Serializes a component into an output of type {@code R}.
*
* @param component the component
* @return the output
* @since 4.14.0
*/
@NotNull R serialize(final @NotNull I component);

/**
* Serializes a component into an output of type {@code R}.
*
* <p>If {@code component} is {@code null}, then {@code null} will be returned.</p>
*
* @param component the component
* @return the output if {@code component} is non-null, otherwise {@code null}
* @since 4.14.0
*/
@Contract(value = "!null -> !null; null -> null", pure = true)
default @Nullable R serializeOrNull(final @Nullable I component) {
return this.serializeOr(component, null);
}

/**
* Serializes a component into an output of type {@code R}.
*
* <p>If {@code component} is {@code null}, then {@code fallback} will be returned.</p>
*
* @param component the component
* @param fallback the fallback value
* @return the output if {@code component} is non-null, otherwise {@code fallback}
* @since 4.14.0
*/
@Contract(value = "!null, _ -> !null; null, _ -> param2", pure = true)
default @Nullable R serializeOr(final @Nullable I component, final @Nullable R fallback) {
if (component == null) return fallback;

return this.serialize(component);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* This file is part of adventure, licensed under the MIT License.
*
* Copyright (c) 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.
*/
/**
* Serialization for components.
*/
package net.kyori.adventure.text.encoder;
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
package net.kyori.adventure.text.serializer;

import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.encoder.ComponentEncoder;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
Expand All @@ -37,7 +38,7 @@
* @param <R> the serialized type
* @since 4.0.0
*/
public interface ComponentSerializer<I extends Component, O extends Component, R> {
public interface ComponentSerializer<I extends Component, O extends Component, R> extends ComponentEncoder<I, R> {
/**
* Deserialize a component from input of type {@code R}.
*
Expand Down Expand Up @@ -102,6 +103,7 @@ public interface ComponentSerializer<I extends Component, O extends Component, R
* @return the output
* @since 4.0.0
*/
@Override
@NotNull R serialize(final @NotNull I component);

/**
Expand All @@ -113,6 +115,7 @@ public interface ComponentSerializer<I extends Component, O extends Component, R
* @return the output if {@code component} is non-null, otherwise {@code null}
* @since 4.7.0
*/
@Override
@Contract(value = "!null -> !null; null -> null", pure = true)
default @Nullable R serializeOrNull(final @Nullable I component) {
return this.serializeOr(component, null);
Expand All @@ -128,6 +131,7 @@ public interface ComponentSerializer<I extends Component, O extends Component, R
* @return the output if {@code component} is non-null, otherwise {@code fallback}
* @since 4.7.0
*/
@Override
@Contract(value = "!null, _ -> !null; null, _ -> param2", pure = true)
default @Nullable R serializeOr(final @Nullable I component, final @Nullable R fallback) {
if (component == null) return fallback;
Expand Down

0 comments on commit 1acc0f3

Please sign in to comment.