From dc7e0f0adcf11bc70761b45580eb3ff8a9a247f4 Mon Sep 17 00:00:00 2001 From: Riley Park Date: Thu, 16 Dec 2021 16:17:09 -0800 Subject: [PATCH] api: Some cleanup --- .../net/kyori/adventure/text/Component.java | 18 +++++++++++ .../adventure/text/ComponentBuilder.java | 15 +++++++-- .../text/format/MutableStyleSetter.java | 8 ++--- .../kyori/adventure/text/format/Style.java | 2 +- .../adventure/text/format/StyleGetter.java | 3 +- .../adventure/text/format/StyleImpl.java | 31 +++++++++---------- .../adventure/text/format/StyleSetter.java | 3 +- 7 files changed, 51 insertions(+), 29 deletions(-) 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 2bf4782a3..0025a3e51 100644 --- a/api/src/main/java/net/kyori/adventure/text/Component.java +++ b/api/src/main/java/net/kyori/adventure/text/Component.java @@ -1810,6 +1810,7 @@ default void detectCycle(final @NotNull Component that) { * @return the font of this component * @since 4.10.0 */ + @Override default @Nullable Key font() { return this.style().font(); } @@ -1821,6 +1822,7 @@ default void detectCycle(final @NotNull Component that) { * @return a component * @since 4.10.0 */ + @Override default @NotNull Component font(final @Nullable Key key) { return this.style(this.style().font(key)); } @@ -1831,6 +1833,7 @@ default void detectCycle(final @NotNull Component that) { * @return the color of this component * @since 4.0.0 */ + @Override default @Nullable TextColor color() { return this.style().color(); } @@ -1843,6 +1846,7 @@ default void detectCycle(final @NotNull Component that) { * @since 4.0.0 */ @Contract(pure = true) + @Override default @NotNull Component color(final @Nullable TextColor color) { return this.style(this.style().color(color)); } @@ -1855,6 +1859,7 @@ default void detectCycle(final @NotNull Component that) { * @since 4.0.0 */ @Contract(pure = true) + @Override default @NotNull Component colorIfAbsent(final @Nullable TextColor color) { if (this.color() == null) return this.color(color); return this; @@ -1868,6 +1873,7 @@ default void detectCycle(final @NotNull Component that) { * component does not have the decoration * @since 4.0.0 */ + @Override default boolean hasDecoration(final @NotNull TextDecoration decoration) { return StyleGetter.super.hasDecoration(decoration); } @@ -1880,6 +1886,7 @@ default boolean hasDecoration(final @NotNull TextDecoration decoration) { * @since 4.0.0 */ @Contract(pure = true) + @Override default @NotNull Component decorate(final @NotNull TextDecoration decoration) { return StyleSetter.super.decorate(decoration); } @@ -1893,6 +1900,7 @@ default boolean hasDecoration(final @NotNull TextDecoration decoration) { * and {@link TextDecoration.State#NOT_SET} if not set * @since 4.0.0 */ + @Override default TextDecoration.@NotNull State decoration(final @NotNull TextDecoration decoration) { return this.style().decoration(decoration); } @@ -1907,6 +1915,7 @@ default boolean hasDecoration(final @NotNull TextDecoration decoration) { * @since 4.0.0 */ @Contract(pure = true) + @Override default @NotNull Component decoration(final @NotNull TextDecoration decoration, final boolean flag) { return StyleSetter.super.decoration(decoration, flag); } @@ -1923,6 +1932,7 @@ default boolean hasDecoration(final @NotNull TextDecoration decoration) { * @since 4.0.0 */ @Contract(pure = true) + @Override default @NotNull Component decoration(final @NotNull TextDecoration decoration, final TextDecoration.@NotNull State state) { return this.style(this.style().decoration(decoration, state)); } @@ -1933,6 +1943,7 @@ default boolean hasDecoration(final @NotNull TextDecoration decoration) { * @return a set of decorations this component has * @since 4.0.0 */ + @Override default @NotNull Map decorations() { return this.style().decorations(); } @@ -1947,6 +1958,7 @@ default boolean hasDecoration(final @NotNull TextDecoration decoration) { * @since 4.0.0 */ @Contract(pure = true) + @Override default @NotNull Component decorations(final @NotNull Map decorations) { return this.style(this.style().decorations(decorations)); } @@ -1957,6 +1969,7 @@ default boolean hasDecoration(final @NotNull TextDecoration decoration) { * @return the click event * @since 4.0.0 */ + @Override default @Nullable ClickEvent clickEvent() { return this.style().clickEvent(); } @@ -1969,6 +1982,7 @@ default boolean hasDecoration(final @NotNull TextDecoration decoration) { * @since 4.0.0 */ @Contract(pure = true) + @Override default @NotNull Component clickEvent(final @Nullable ClickEvent event) { return this.style(this.style().clickEvent(event)); } @@ -1979,6 +1993,7 @@ default boolean hasDecoration(final @NotNull TextDecoration decoration) { * @return the hover event * @since 4.0.0 */ + @Override default @Nullable HoverEvent hoverEvent() { return this.style().hoverEvent(); } @@ -1991,6 +2006,7 @@ default boolean hasDecoration(final @NotNull TextDecoration decoration) { * @since 4.0.0 */ @Contract(pure = true) + @Override default @NotNull Component hoverEvent(final @Nullable HoverEventSource source) { return this.style(this.style().hoverEvent(source)); } @@ -2001,6 +2017,7 @@ default boolean hasDecoration(final @NotNull TextDecoration decoration) { * @return the insertion string * @since 4.0.0 */ + @Override default @Nullable String insertion() { return this.style().insertion(); } @@ -2013,6 +2030,7 @@ default boolean hasDecoration(final @NotNull TextDecoration decoration) { * @since 4.0.0 */ @Contract(pure = true) + @Override default @NotNull Component insertion(final @Nullable String insertion) { return this.style(this.style().insertion(insertion)); } diff --git a/api/src/main/java/net/kyori/adventure/text/ComponentBuilder.java b/api/src/main/java/net/kyori/adventure/text/ComponentBuilder.java index ee9ab27b4..d30f5d898 100644 --- a/api/src/main/java/net/kyori/adventure/text/ComponentBuilder.java +++ b/api/src/main/java/net/kyori/adventure/text/ComponentBuilder.java @@ -196,6 +196,7 @@ public interface ComponentBuilder, B extends * @since 4.0.0 */ @Contract("_ -> this") + @Override @NotNull B font(final @Nullable Key font); /** @@ -206,6 +207,7 @@ public interface ComponentBuilder, B extends * @since 4.0.0 */ @Contract("_ -> this") + @Override @NotNull B color(final @Nullable TextColor color); /** @@ -216,6 +218,7 @@ public interface ComponentBuilder, B extends * @since 4.0.0 */ @Contract("_ -> this") + @Override @NotNull B colorIfAbsent(final @Nullable TextColor color); /** @@ -228,7 +231,7 @@ public interface ComponentBuilder, B extends * @since 4.0.0 */ @Contract("_, _ -> this") - @SuppressWarnings("unchecked") + @Override default @NotNull B decorations(final @NotNull Set decorations, final boolean flag) { return MutableStyleSetter.super.decorations(decorations, flag); } @@ -241,6 +244,7 @@ public interface ComponentBuilder, B extends * @since 4.0.0 */ @Contract("_ -> this") + @Override default @NotNull B decorate(final @NotNull TextDecoration decoration) { return this.decoration(decoration, TextDecoration.State.TRUE); } @@ -253,7 +257,7 @@ public interface ComponentBuilder, B extends * @since 4.0.0 */ @Contract("_ -> this") - @SuppressWarnings("unchecked") + @Override default @NotNull B decorate(final @NotNull TextDecoration@NotNull... decorations) { return MutableStyleSetter.super.decorate(decorations); } @@ -268,6 +272,7 @@ public interface ComponentBuilder, B extends * @since 4.0.0 */ @Contract("_, _ -> this") + @Override default @NotNull B decoration(final @NotNull TextDecoration decoration, final boolean flag) { return this.decoration(decoration, TextDecoration.State.byBoolean(flag)); } @@ -282,7 +287,7 @@ public interface ComponentBuilder, B extends * @since 4.10.0 */ @Contract("_ -> this") - @SuppressWarnings("unchecked") + @Override default @NotNull B decorations(final @NotNull Map decorations) { return MutableStyleSetter.super.decorations(decorations); } @@ -299,6 +304,7 @@ public interface ComponentBuilder, B extends * @since 4.0.0 */ @Contract("_, _ -> this") + @Override @NotNull B decoration(final @NotNull TextDecoration decoration, final TextDecoration.@NotNull State state); /** @@ -309,6 +315,7 @@ public interface ComponentBuilder, B extends * @since 4.0.0 */ @Contract("_ -> this") + @Override @NotNull B clickEvent(final @Nullable ClickEvent event); /** @@ -319,6 +326,7 @@ public interface ComponentBuilder, B extends * @since 4.0.0 */ @Contract("_ -> this") + @Override @NotNull B hoverEvent(final @Nullable HoverEventSource source); /** @@ -329,6 +337,7 @@ public interface ComponentBuilder, B extends * @since 4.0.0 */ @Contract("_ -> this") + @Override @NotNull B insertion(final @Nullable String insertion); /** diff --git a/api/src/main/java/net/kyori/adventure/text/format/MutableStyleSetter.java b/api/src/main/java/net/kyori/adventure/text/format/MutableStyleSetter.java index 991882f61..b32c75ce2 100644 --- a/api/src/main/java/net/kyori/adventure/text/format/MutableStyleSetter.java +++ b/api/src/main/java/net/kyori/adventure/text/format/MutableStyleSetter.java @@ -25,6 +25,7 @@ import java.util.Map; import java.util.Set; +import net.kyori.adventure.text.Component; import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.NotNull; @@ -32,16 +33,15 @@ import static java.util.Objects.requireNonNull; /** - * Writes style properties to a mutable object. Used to override some default methods from {@link net.kyori.adventure.text.format.StyleSetter} + * Writes style properties to a mutable object. Used to override some default methods from {@link StyleSetter} * with faster alternatives that only work for mutable objects. * - * @param The type implementing this interface e.g. {@link net.kyori.adventure.text.Component} - * @see net.kyori.adventure.text.format.StyleSetter + * @param The type implementing this interface e.g. {@link Component} + * @see StyleSetter * @since 4.10.0 */ @ApiStatus.NonExtendable public interface MutableStyleSetter> extends StyleSetter { - /** * Sets {@code decorations} to {@link TextDecoration.State#TRUE}. * 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 5ff9758b3..a236efe2f 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 @@ -134,7 +134,7 @@ public interface Style extends Buildable, Examinable, Styl static @NotNull Style style(final @Nullable TextColor color, final TextDecoration@NotNull... decorations) { final Builder builder = style(); builder.color(color); - StyleImpl.decorate(builder, decorations); + builder.decorate(decorations); return builder.build(); } diff --git a/api/src/main/java/net/kyori/adventure/text/format/StyleGetter.java b/api/src/main/java/net/kyori/adventure/text/format/StyleGetter.java index f5ae11809..2797b4014 100644 --- a/api/src/main/java/net/kyori/adventure/text/format/StyleGetter.java +++ b/api/src/main/java/net/kyori/adventure/text/format/StyleGetter.java @@ -36,12 +36,11 @@ /** * Reads style properties from an object. * - * @see net.kyori.adventure.text.format.Style + * @see Style * @since 4.10.0 */ @ApiStatus.NonExtendable public interface StyleGetter { - /** * Gets the font. * 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 9441923d5..a8827ae0a 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 @@ -53,13 +53,6 @@ final class StyleImpl implements Style { final @Nullable HoverEvent hoverEvent; final @Nullable String insertion; - static void decorate(final Builder builder, final TextDecoration[] decorations) { - for (int i = 0, length = decorations.length; i < length; i++) { - final TextDecoration decoration = decorations[i]; - builder.decoration(decoration, true); - } - } - StyleImpl( final @Nullable Key font, final @Nullable TextColor color, @@ -190,8 +183,7 @@ static void decorate(final Builder builder, final TextDecoration[] decorations) @Override public @NotNull Style merge(final @NotNull Style that, final Merge.@NotNull Strategy strategy, final @NotNull Set merges) { - if (that.isEmpty() || strategy == Merge.Strategy.NEVER || merges.isEmpty()) { - // nothing to merge + if (nothingToMerge(that, strategy, merges)) { return this; } @@ -206,6 +198,14 @@ static void decorate(final Builder builder, final TextDecoration[] decorations) return builder.build(); } + @SuppressWarnings("RedundantIfStatement") + static boolean nothingToMerge(final @NotNull Style mergeFrom, final Merge.@NotNull Strategy strategy, final @NotNull Set merges) { + if (strategy == Merge.Strategy.NEVER) return true; + if (mergeFrom.isEmpty()) return true; + if (merges.isEmpty()) return true; + return false; + } + @Override public boolean isEmpty() { return this == EMPTY; @@ -327,21 +327,18 @@ static final class BuilderImpl implements Builder { requireNonNull(state, "state"); if (decoration == TextDecoration.BOLD) { this.bold = state; - return this; } else if (decoration == TextDecoration.ITALIC) { this.italic = state; - return this; } else if (decoration == TextDecoration.UNDERLINED) { this.underlined = state; - return this; } else if (decoration == TextDecoration.STRIKETHROUGH) { this.strikethrough = state; - return this; } else if (decoration == TextDecoration.OBFUSCATED) { this.obfuscated = state; - return this; + } else { + throw new IllegalArgumentException(String.format("unknown decoration '%s'", decoration)); } - throw new IllegalArgumentException(String.format("unknown decoration '%s'", decoration)); + return this; } // todo(kashike): promote to public api? @@ -399,8 +396,8 @@ static final class BuilderImpl implements Builder { requireNonNull(that, "style"); requireNonNull(strategy, "strategy"); requireNonNull(merges, "merges"); - if (strategy == Merge.Strategy.NEVER || that.isEmpty() || merges.isEmpty()) { - // nothing to merge + + if (nothingToMerge(that, strategy, merges)) { return this; } 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 06acf3bdb..abe7d301d 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 @@ -45,7 +45,6 @@ */ @ApiStatus.NonExtendable public interface StyleSetter> { - /** * Sets the font. * @@ -147,7 +146,7 @@ public interface StyleSetter> { * @since 4.10.0 */ default @NotNull T decorations(final @NotNull Set decorations, final boolean flag) { - return this.decorations(decorations.stream().collect(Collectors.toMap(Function.identity(), d -> TextDecoration.State.byBoolean(flag)))); + return this.decorations(decorations.stream().collect(Collectors.toMap(Function.identity(), decoration -> TextDecoration.State.byBoolean(flag)))); } /**