Skip to content

Commit

Permalink
api: Some cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
kashike committed Dec 19, 2021
1 parent 67b619b commit dc7e0f0
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 29 deletions.
18 changes: 18 additions & 0 deletions api/src/main/java/net/kyori/adventure/text/Component.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand All @@ -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));
}
Expand All @@ -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();
}
Expand All @@ -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));
}
Expand All @@ -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;
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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));
}
Expand All @@ -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<TextDecoration, TextDecoration.State> decorations() {
return this.style().decorations();
}
Expand All @@ -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<TextDecoration, TextDecoration.State> decorations) {
return this.style(this.style().decorations(decorations));
}
Expand All @@ -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();
}
Expand All @@ -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));
}
Expand All @@ -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();
}
Expand All @@ -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));
}
Expand All @@ -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();
}
Expand All @@ -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));
}
Expand Down
15 changes: 12 additions & 3 deletions api/src/main/java/net/kyori/adventure/text/ComponentBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ public interface ComponentBuilder<C extends BuildableComponent<C, B>, B extends
* @since 4.0.0
*/
@Contract("_ -> this")
@Override
@NotNull B font(final @Nullable Key font);

/**
Expand All @@ -206,6 +207,7 @@ public interface ComponentBuilder<C extends BuildableComponent<C, B>, B extends
* @since 4.0.0
*/
@Contract("_ -> this")
@Override
@NotNull B color(final @Nullable TextColor color);

/**
Expand All @@ -216,6 +218,7 @@ public interface ComponentBuilder<C extends BuildableComponent<C, B>, B extends
* @since 4.0.0
*/
@Contract("_ -> this")
@Override
@NotNull B colorIfAbsent(final @Nullable TextColor color);

/**
Expand All @@ -228,7 +231,7 @@ public interface ComponentBuilder<C extends BuildableComponent<C, B>, B extends
* @since 4.0.0
*/
@Contract("_, _ -> this")
@SuppressWarnings("unchecked")
@Override
default @NotNull B decorations(final @NotNull Set<TextDecoration> decorations, final boolean flag) {
return MutableStyleSetter.super.decorations(decorations, flag);
}
Expand All @@ -241,6 +244,7 @@ public interface ComponentBuilder<C extends BuildableComponent<C, B>, B extends
* @since 4.0.0
*/
@Contract("_ -> this")
@Override
default @NotNull B decorate(final @NotNull TextDecoration decoration) {
return this.decoration(decoration, TextDecoration.State.TRUE);
}
Expand All @@ -253,7 +257,7 @@ public interface ComponentBuilder<C extends BuildableComponent<C, B>, 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);
}
Expand All @@ -268,6 +272,7 @@ public interface ComponentBuilder<C extends BuildableComponent<C, B>, 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));
}
Expand All @@ -282,7 +287,7 @@ public interface ComponentBuilder<C extends BuildableComponent<C, B>, B extends
* @since 4.10.0
*/
@Contract("_ -> this")
@SuppressWarnings("unchecked")
@Override
default @NotNull B decorations(final @NotNull Map<TextDecoration, TextDecoration.State> decorations) {
return MutableStyleSetter.super.decorations(decorations);
}
Expand All @@ -299,6 +304,7 @@ public interface ComponentBuilder<C extends BuildableComponent<C, B>, B extends
* @since 4.0.0
*/
@Contract("_, _ -> this")
@Override
@NotNull B decoration(final @NotNull TextDecoration decoration, final TextDecoration.@NotNull State state);

/**
Expand All @@ -309,6 +315,7 @@ public interface ComponentBuilder<C extends BuildableComponent<C, B>, B extends
* @since 4.0.0
*/
@Contract("_ -> this")
@Override
@NotNull B clickEvent(final @Nullable ClickEvent event);

/**
Expand All @@ -319,6 +326,7 @@ public interface ComponentBuilder<C extends BuildableComponent<C, B>, B extends
* @since 4.0.0
*/
@Contract("_ -> this")
@Override
@NotNull B hoverEvent(final @Nullable HoverEventSource<?> source);

/**
Expand All @@ -329,6 +337,7 @@ public interface ComponentBuilder<C extends BuildableComponent<C, B>, B extends
* @since 4.0.0
*/
@Contract("_ -> this")
@Override
@NotNull B insertion(final @Nullable String insertion);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,23 @@

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;

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 <T> The type implementing this interface e.g. {@link net.kyori.adventure.text.Component}
* @see net.kyori.adventure.text.format.StyleSetter
* @param <T> The type implementing this interface e.g. {@link Component}
* @see StyleSetter
* @since 4.10.0
*/
@ApiStatus.NonExtendable
public interface MutableStyleSetter<T extends MutableStyleSetter<?>> extends StyleSetter<T> {

/**
* Sets {@code decorations} to {@link TextDecoration.State#TRUE}.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public interface Style extends Buildable<Style, Style.Builder>, 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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
31 changes: 14 additions & 17 deletions api/src/main/java/net/kyori/adventure/text/format/StyleImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<Merge> merges) {
if (that.isEmpty() || strategy == Merge.Strategy.NEVER || merges.isEmpty()) {
// nothing to merge
if (nothingToMerge(that, strategy, merges)) {
return this;
}

Expand All @@ -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<Merge> 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;
Expand Down Expand Up @@ -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?
Expand Down Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
*/
@ApiStatus.NonExtendable
public interface StyleSetter<T extends StyleSetter<?>> {

/**
* Sets the font.
*
Expand Down Expand Up @@ -147,7 +146,7 @@ public interface StyleSetter<T extends StyleSetter<?>> {
* @since 4.10.0
*/
default @NotNull T decorations(final @NotNull Set<TextDecoration> 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))));
}

/**
Expand Down

0 comments on commit dc7e0f0

Please sign in to comment.