Skip to content

Commit

Permalink
Abstract out style methods to StyleWriter/Reader
Browse files Browse the repository at this point in the history
  • Loading branch information
KingOfSquares committed Nov 21, 2021
1 parent 1a1a04b commit 39f0a87
Show file tree
Hide file tree
Showing 7 changed files with 378 additions and 57 deletions.
31 changes: 27 additions & 4 deletions api/src/main/java/net/kyori/adventure/text/Component.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
import net.kyori.adventure.text.event.HoverEvent;
import net.kyori.adventure.text.event.HoverEventSource;
import net.kyori.adventure.text.format.Style;
import net.kyori.adventure.text.format.StyleReader;
import net.kyori.adventure.text.format.StyleWriter;
import net.kyori.adventure.text.format.TextColor;
import net.kyori.adventure.text.format.TextDecoration;
import net.kyori.adventure.text.serializer.ComponentSerializer;
Expand Down Expand Up @@ -101,7 +103,7 @@
* @since 4.0.0
*/
@ApiStatus.NonExtendable
public interface Component extends ComponentBuilderApplicable, ComponentLike, Examinable, HoverEventSource<Component> {
public interface Component extends ComponentBuilderApplicable, ComponentLike, Examinable, HoverEventSource<Component>, StyleReader, StyleWriter<Component> {
/**
* A predicate that checks equality of two {@code Component}s using {@link Objects#equals(Object, Object)}.
*
Expand Down Expand Up @@ -1802,6 +1804,27 @@ default void detectCycle(final @NotNull Component that) {
return this.style(this.style().merge(that.style(), merges));
}

/**
* Gets the font.
*
* @return the font of this component
* @since 4.10.0
*/
default @Nullable Key font() {
return this.style().font();
}

/**
* Sets the font.
*
* @param key a font
* @return a component
* @since 4.10.0
*/
default @NotNull Component font(final @Nullable Key key) {
return this.style(this.style().font(key));
}

/**
* Gets the color of this component.
*
Expand Down Expand Up @@ -1846,7 +1869,7 @@ default void detectCycle(final @NotNull Component that) {
* @since 4.0.0
*/
default boolean hasDecoration(final @NotNull TextDecoration decoration) {
return this.decoration(decoration) == TextDecoration.State.TRUE;
return StyleReader.super.hasDecoration(decoration);
}

/**
Expand All @@ -1858,7 +1881,7 @@ default boolean hasDecoration(final @NotNull TextDecoration decoration) {
*/
@Contract(pure = true)
default @NotNull Component decorate(final @NotNull TextDecoration decoration) {
return this.decoration(decoration, TextDecoration.State.TRUE);
return StyleWriter.super.decorate(decoration);
}

/**
Expand All @@ -1885,7 +1908,7 @@ default boolean hasDecoration(final @NotNull TextDecoration decoration) {
*/
@Contract(pure = true)
default @NotNull Component decoration(final @NotNull TextDecoration decoration, final boolean flag) {
return this.decoration(decoration, TextDecoration.State.byBoolean(flag));
return StyleWriter.super.decoration(decoration, flag);
}

/**
Expand Down
19 changes: 7 additions & 12 deletions api/src/main/java/net/kyori/adventure/text/ComponentBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@
import net.kyori.adventure.key.Key;
import net.kyori.adventure.text.event.ClickEvent;
import net.kyori.adventure.text.event.HoverEventSource;
import net.kyori.adventure.text.format.MutableStyleWriter;
import net.kyori.adventure.text.format.Style;
import net.kyori.adventure.text.format.TextColor;
import net.kyori.adventure.text.format.TextDecoration;
import net.kyori.adventure.util.Buildable;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand All @@ -46,7 +48,8 @@
* @param <B> the builder type
* @since 4.0.0
*/
public interface ComponentBuilder<C extends BuildableComponent<C, B>, B extends ComponentBuilder<C, B>> extends Buildable.Builder<C>, ComponentBuilderApplicable, ComponentLike {
@ApiStatus.NonExtendable
public interface ComponentBuilder<C extends BuildableComponent<C, B>, B extends ComponentBuilder<C, B>> extends Buildable.Builder<C>, ComponentBuilderApplicable, ComponentLike, MutableStyleWriter<B> {
/**
* Appends a component to this component.
*
Expand Down Expand Up @@ -227,9 +230,7 @@ public interface ComponentBuilder<C extends BuildableComponent<C, B>, B extends
@Contract("_, _ -> this")
@SuppressWarnings("unchecked")
default @NotNull B decorations(final @NotNull Set<TextDecoration> decorations, final boolean flag) {
final TextDecoration.State state = TextDecoration.State.byBoolean(flag);
decorations.forEach(decoration -> this.decoration(decoration, state));
return (B) this;
return MutableStyleWriter.super.decorations(decorations, flag);
}

/**
Expand All @@ -254,10 +255,7 @@ public interface ComponentBuilder<C extends BuildableComponent<C, B>, B extends
@Contract("_ -> this")
@SuppressWarnings("unchecked")
default @NotNull B decorate(final @NotNull TextDecoration@NotNull... decorations) {
for (int i = 0, length = decorations.length; i < length; i++) {
this.decorate(decorations[i]);
}
return (B) this;
return MutableStyleWriter.super.decorate(decorations);
}

/**
Expand Down Expand Up @@ -286,10 +284,7 @@ public interface ComponentBuilder<C extends BuildableComponent<C, B>, B extends
@Contract("_ -> this")
@SuppressWarnings("unchecked")
default @NotNull B decorations(final @NotNull Map<TextDecoration, TextDecoration.State> decorations) {
for (final Map.Entry<TextDecoration, TextDecoration.State> entry : decorations.entrySet()) {
this.decoration(entry.getKey(), entry.getValue());
}
return (B) this;
return MutableStyleWriter.super.decorations(decorations);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package net.kyori.adventure.text.format;

import java.util.Map;
import java.util.Set;

import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;

/**
* Writes style properties to a mutable object. Used to override some default methods from {@link net.kyori.adventure.text.format.StyleWriter}
* 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.StyleWriter
* @since 4.10.0
*/
@ApiStatus.NonExtendable
public interface MutableStyleWriter<T extends MutableStyleWriter<?>> extends StyleWriter<T> {

/**
* Sets {@code decorations} to {@link TextDecoration.State#TRUE}.
*
* @param decorations the decorations
* @return a mutable object ({@code T})
* @since 4.10.0
*/
@Override
@Contract("_ -> this")
@SuppressWarnings("unchecked")
default @NotNull T decorate(final @NotNull TextDecoration@NotNull... decorations) {
for (int i = 0, length = decorations.length; i < length; i++) {
this.decorate(decorations[i]);
}
return (T) this;
}

/**
* Sets decorations using the specified {@code decorations} map.
*
* <p>If a given decoration does not have a value explicitly set, the value of that particular decoration is not changed.</p>
*
* @param decorations a map containing text decorations and their respective state.
* @return a mutable object ({@code T})
* @since 4.10.0
*/
@Override
@Contract("_ -> this")
@SuppressWarnings("unchecked")
default @NotNull T decorations(final @NotNull Map<TextDecoration, TextDecoration.State> decorations) {
for (final Map.Entry<TextDecoration, TextDecoration.State> entry : decorations.entrySet()) {
this.decoration(entry.getKey(), entry.getValue());
}
return (T) this;
}

/**
* Sets the state of a set of decorations to {@code flag}.
*
* @param decorations the decorations
* @param flag {@code true} if this mutable object should have the decorations, {@code false} if
* this mutable object should not have the decorations
* @return a mutable object ({@code T})
* @since 4.10.0
*/
@Override
@Contract("_, _ -> this")
@SuppressWarnings("unchecked")
default @NotNull T decorations(final @NotNull Set<TextDecoration> decorations, final boolean flag) {
final TextDecoration.State state = TextDecoration.State.byBoolean(flag);
decorations.forEach(decoration -> this.decoration(decoration, state));
return (T) this;
}
}
28 changes: 12 additions & 16 deletions api/src/main/java/net/kyori/adventure/text/format/Style.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
* @since 4.0.0
*/
@ApiStatus.NonExtendable
public interface Style extends Buildable<Style, Style.Builder>, Examinable {
public interface Style extends Buildable<Style, Style.Builder>, Examinable, StyleReader, StyleWriter<Style> {
/**
* The default font.
*
Expand Down Expand Up @@ -280,7 +280,7 @@ public interface Style extends Buildable<Style, Style.Builder>, Examinable {
* @since 4.0.0
*/
default boolean hasDecoration(final @NotNull TextDecoration decoration) {
return this.decoration(decoration) == TextDecoration.State.TRUE;
return StyleReader.super.hasDecoration(decoration);
}

/**
Expand All @@ -302,7 +302,7 @@ default boolean hasDecoration(final @NotNull TextDecoration decoration) {
* @since 4.0.0
*/
default @NotNull Style decorate(final @NotNull TextDecoration decoration) {
return this.decoration(decoration, TextDecoration.State.TRUE);
return StyleWriter.super.decorate(decoration);
}

/**
Expand All @@ -315,7 +315,7 @@ default boolean hasDecoration(final @NotNull TextDecoration decoration) {
* @since 4.0.0
*/
default @NotNull Style decoration(final @NotNull TextDecoration decoration, final boolean flag) {
return this.decoration(decoration, TextDecoration.State.byBoolean(flag));
return StyleWriter.super.decoration(decoration, flag);
}

/**
Expand All @@ -337,7 +337,9 @@ default boolean hasDecoration(final @NotNull TextDecoration decoration) {
* @return a set of decorations this style has
* @since 4.0.0
*/
@Unmodifiable @NotNull Map<TextDecoration, TextDecoration.State> decorations();
default @Unmodifiable @NotNull Map<TextDecoration, TextDecoration.State> decorations() {
return StyleReader.super.decorations();
}

/**
* Sets decorations for this style using the specified {@code decorations} map.
Expand Down Expand Up @@ -621,7 +623,7 @@ public enum Strategy {
*
* @since 4.0.0
*/
interface Builder extends Buildable.Builder<Style> {
interface Builder extends Buildable.Builder<Style>, MutableStyleWriter<Builder> {
/**
* Sets the font.
*
Expand Down Expand Up @@ -662,7 +664,7 @@ interface Builder extends Buildable.Builder<Style> {
*/
@Contract("_ -> this")
default @NotNull Builder decorate(final @NotNull TextDecoration decoration) {
return this.decoration(decoration, TextDecoration.State.TRUE);
return MutableStyleWriter.super.decorate(decoration);
}

/**
Expand All @@ -674,10 +676,7 @@ interface Builder extends Buildable.Builder<Style> {
*/
@Contract("_ -> this")
default @NotNull Builder decorate(final @NotNull TextDecoration@NotNull... decorations) {
for (int i = 0, length = decorations.length; i < length; i++) {
this.decorate(decorations[i]);
}
return this;
return MutableStyleWriter.super.decorate(decorations);
}

/**
Expand All @@ -691,7 +690,7 @@ interface Builder extends Buildable.Builder<Style> {
*/
@Contract("_, _ -> this")
default @NotNull Builder decoration(final @NotNull TextDecoration decoration, final boolean flag) {
return this.decoration(decoration, TextDecoration.State.byBoolean(flag));
return MutableStyleWriter.super.decoration(decoration, flag);
}

/**
Expand All @@ -705,10 +704,7 @@ interface Builder extends Buildable.Builder<Style> {
*/
@Contract("_ -> this")
default @NotNull Builder decorations(final @NotNull Map<TextDecoration, TextDecoration.State> decorations) {
for (final Map.Entry<TextDecoration, TextDecoration.State> entry : decorations.entrySet()) {
this.decoration(entry.getKey(), entry.getValue());
}
return this;
return MutableStyleWriter.super.decorations(decorations);
}

/**
Expand Down
25 changes: 0 additions & 25 deletions api/src/main/java/net/kyori/adventure/text/format/StyleImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
*/
package net.kyori.adventure.text.format;

import java.util.EnumMap;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
Expand Down Expand Up @@ -148,17 +147,6 @@ static void decorate(final Builder builder, final TextDecoration[] decorations)
throw new IllegalArgumentException(String.format("unknown decoration '%s'", decoration));
}

@Override
public @NotNull Map<TextDecoration, TextDecoration.State> decorations() {
final Map<TextDecoration, TextDecoration.State> decorations = new EnumMap<>(TextDecoration.class);
for (int i = 0, length = DECORATIONS.length; i < length; i++) {
final TextDecoration decoration = DECORATIONS[i];
final TextDecoration.State value = this.decoration(decoration);
decorations.put(decoration, value);
}
return decorations;
}

@Override
public @NotNull Style decorations(final @NotNull Map<TextDecoration, TextDecoration.State> decorations) {
final TextDecoration.State obfuscated = decorations.getOrDefault(TextDecoration.OBFUSCATED, this.obfuscated);
Expand Down Expand Up @@ -334,19 +322,6 @@ static final class BuilderImpl implements Builder {
return this;
}

@Override
public @NotNull Builder decorate(final @NotNull TextDecoration decoration) {
return this.decoration(decoration, TextDecoration.State.TRUE);
}

@Override
public @NotNull Builder decorate(final @NotNull TextDecoration@NotNull... decorations) {
for (int i = 0, length = decorations.length; i < length; i++) {
this.decorate(decorations[i]);
}
return this;
}

@Override
public @NotNull Builder decoration(final @NotNull TextDecoration decoration, final TextDecoration.@NotNull State state) {
requireNonNull(state, "state");
Expand Down
Loading

0 comments on commit 39f0a87

Please sign in to comment.