From 48f60075be809da746516432e15545bbba662012 Mon Sep 17 00:00:00 2001 From: KingSimon <19822231+KingOfSquares@users.noreply.github.com> Date: Sat, 11 Jun 2022 23:29:03 +0200 Subject: [PATCH] Cleanup decorationIfAbsent code after review --- .../text/AbstractComponentBuilder.java | 6 +-- .../net/kyori/adventure/text/Component.java | 40 +++++++++---------- .../kyori/adventure/text/format/Style.java | 24 +++++------ .../adventure/text/format/StyleImpl.java | 21 +++++----- .../adventure/text/format/StyleSetter.java | 20 +++++----- .../text/ComponentDecorationTest.java | 2 +- 6 files changed, 55 insertions(+), 58 deletions(-) diff --git a/api/src/main/java/net/kyori/adventure/text/AbstractComponentBuilder.java b/api/src/main/java/net/kyori/adventure/text/AbstractComponentBuilder.java index c73f480626..a6add27c07 100644 --- a/api/src/main/java/net/kyori/adventure/text/AbstractComponentBuilder.java +++ b/api/src/main/java/net/kyori/adventure/text/AbstractComponentBuilder.java @@ -246,11 +246,7 @@ private void prepareChildren() { @Override @SuppressWarnings("unchecked") public @NotNull B decorationIfAbsent(final @NotNull TextDecoration decoration, final TextDecoration.@NotNull State state) { - requireNonNull(state, "state"); - final TextDecoration.@NotNull State thisState = this.buildStyle().decoration(decoration); - if (thisState == TextDecoration.State.NOT_SET) { - this.styleBuilder().decoration(decoration, state); - } + this.styleBuilder().decorationIfAbsent(decoration, state); return (B) this; } diff --git a/api/src/main/java/net/kyori/adventure/text/Component.java b/api/src/main/java/net/kyori/adventure/text/Component.java index a534d400d1..4efca88344 100644 --- a/api/src/main/java/net/kyori/adventure/text/Component.java +++ b/api/src/main/java/net/kyori/adventure/text/Component.java @@ -1988,6 +1988,26 @@ default boolean hasDecoration(final @NotNull TextDecoration decoration) { return this.style(this.style().decoration(decoration, state)); } + /** + * Sets the state of a decoration on this component to {@code state} if the current state of + * the decoration is {@link TextDecoration.State#NOT_SET}. + * + * @param decoration the decoration + * @param state the state + * @return a component + * @since 4.12.0 + */ + @Override + default @NotNull Component decorationIfAbsent(final @NotNull TextDecoration decoration, @NotNull final TextDecoration.State state) { + requireNonNull(state, "state"); + // Not delegating this method prevents object creation if decoration is NOT absent + final TextDecoration.@NotNull State oldState = this.decoration(decoration); + if (oldState == TextDecoration.State.NOT_SET) { + return this.style(this.style().decoration(decoration, state)); + } + return this; + } + /** * Gets a set of decorations this component has. * @@ -2014,26 +2034,6 @@ default boolean hasDecoration(final @NotNull TextDecoration decoration) { return this.style(this.style().decorations(decorations)); } - /** - * Sets the state of a decoration on this component to {@code state} if the current state of - * the decoration is {@link TextDecoration.State#NOT_SET}. - * - * @param decoration the decoration - * @param state the state - * @return a component - * @since 4.12.0 - */ - @Override - default @NotNull Component decorationIfAbsent(final @NotNull TextDecoration decoration, @NotNull final TextDecoration.State state) { - requireNonNull(state, "state"); - //Not delegating this method prevents object creation if decoration is NOT absent - final TextDecoration.@NotNull State thisState = this.decoration(decoration); - if (thisState == TextDecoration.State.NOT_SET) { - return this.style(this.style().decoration(decoration, state)); - } - return this; - } - /** * Gets the click event of this component. * diff --git a/api/src/main/java/net/kyori/adventure/text/format/Style.java b/api/src/main/java/net/kyori/adventure/text/format/Style.java index 024d04bd5f..55015acc40 100644 --- a/api/src/main/java/net/kyori/adventure/text/format/Style.java +++ b/api/src/main/java/net/kyori/adventure/text/format/Style.java @@ -341,6 +341,18 @@ default boolean hasDecoration(final @NotNull TextDecoration decoration) { @Override @NotNull Style decoration(final @NotNull TextDecoration decoration, final TextDecoration.@NotNull State state); + /** + * Sets the state of a decoration on this style to {@code state} if the current state of + * the decoration is {@link TextDecoration.State#NOT_SET}. + * + * @param decoration the decoration + * @param state the state + * @return a style + * @since 4.12.0 + */ + @Override + @NotNull Style decorationIfAbsent(final @NotNull TextDecoration decoration, @NotNull final TextDecoration.State state); + /** * Gets a map of decorations this style has. * @@ -364,18 +376,6 @@ default boolean hasDecoration(final @NotNull TextDecoration decoration) { @Override @NotNull Style decorations(final @NotNull Map decorations); - /** - * Sets the state of a decoration on this style to {@code state} if the current state of - * the decoration is {@link TextDecoration.State#NOT_SET}. - * - * @param decoration the decoration - * @param state the state - * @return a style - * @since 4.12.0 - */ - @Override - @NotNull Style decorationIfAbsent(final @NotNull TextDecoration decoration, @NotNull final TextDecoration.State state); - /** * Gets the click event. * diff --git a/api/src/main/java/net/kyori/adventure/text/format/StyleImpl.java b/api/src/main/java/net/kyori/adventure/text/format/StyleImpl.java index b4e6176386..1da636c091 100644 --- a/api/src/main/java/net/kyori/adventure/text/format/StyleImpl.java +++ b/api/src/main/java/net/kyori/adventure/text/format/StyleImpl.java @@ -112,16 +112,6 @@ final class StyleImpl implements Style { return new StyleImpl(this.font, this.color, this.decorations.with(decoration, state), this.clickEvent, this.hoverEvent, this.insertion); } - @Override - public @NotNull Map decorations() { - return this.decorations; - } - - @Override - public @NotNull Style decorations(final @NotNull Map decorations) { - return new StyleImpl(this.font, this.color, DecorationMap.merge(decorations, this.decorations), this.clickEvent, this.hoverEvent, this.insertion); - } - @Override public @NotNull Style decorationIfAbsent(final @NotNull TextDecoration decoration, final @NotNull TextDecoration.State state) { requireNonNull(state, "state"); @@ -135,6 +125,16 @@ final class StyleImpl implements Style { throw new IllegalArgumentException(String.format("unknown decoration '%s'", decoration)); } + @Override + public @NotNull Map decorations() { + return this.decorations; + } + + @Override + public @NotNull Style decorations(final @NotNull Map decorations) { + return new StyleImpl(this.font, this.color, DecorationMap.merge(decorations, this.decorations), this.clickEvent, this.hoverEvent, this.insertion); + } + @Override public @Nullable ClickEvent clickEvent() { return this.clickEvent; @@ -293,6 +293,7 @@ static final class BuilderImpl implements Builder { return this; } + @Override public @NotNull Builder decorationIfAbsent(final @NotNull TextDecoration decoration, final TextDecoration.@NotNull State state) { requireNonNull(state, "state"); final TextDecoration.@Nullable State thisState = this.decorations.get(decoration); diff --git a/api/src/main/java/net/kyori/adventure/text/format/StyleSetter.java b/api/src/main/java/net/kyori/adventure/text/format/StyleSetter.java index 7dfd1a5229..a0779caa62 100644 --- a/api/src/main/java/net/kyori/adventure/text/format/StyleSetter.java +++ b/api/src/main/java/net/kyori/adventure/text/format/StyleSetter.java @@ -125,6 +125,16 @@ public interface StyleSetter> { */ @NotNull T decoration(final @NotNull TextDecoration decoration, final TextDecoration.@NotNull State state); + /** + * Sets the state of a decoration to {@code state} if the current state of the decoration is {@link TextDecoration.State#NOT_SET}. + * + * @param decoration the decoration + * @param state the state + * @return an object ({@code T}) + * @since 4.12.0 + */ + @NotNull T decorationIfAbsent(final @NotNull TextDecoration decoration, final TextDecoration.@NotNull State state); + /** * Sets decorations using the specified {@code decorations} map. * @@ -149,16 +159,6 @@ public interface StyleSetter> { return this.decorations(decorations.stream().collect(Collectors.toMap(Function.identity(), decoration -> TextDecoration.State.byBoolean(flag)))); } - /** - * Sets the state of a decoration to {@code state} if the current state of the decoration is {@link TextDecoration.State#NOT_SET}. - * - * @param decoration the decoration - * @param state the state - * @return an object ({@code T}) - * @since 4.12.0 - */ - @NotNull T decorationIfAbsent(final @NotNull TextDecoration decoration, final TextDecoration.@NotNull State state); - /** * Sets the click event. * diff --git a/api/src/test/java/net/kyori/adventure/text/ComponentDecorationTest.java b/api/src/test/java/net/kyori/adventure/text/ComponentDecorationTest.java index 94136822a2..ac04f08356 100644 --- a/api/src/test/java/net/kyori/adventure/text/ComponentDecorationTest.java +++ b/api/src/test/java/net/kyori/adventure/text/ComponentDecorationTest.java @@ -30,7 +30,7 @@ import static net.kyori.adventure.text.TextAssertions.assertDecorations; -public class ComponentDecorationTest { +class ComponentDecorationTest { @Test void testDecorationIfAbsent() {