Skip to content

Commit

Permalink
Cleanup decorationIfAbsent code after review
Browse files Browse the repository at this point in the history
  • Loading branch information
KingOfSquares committed Jun 11, 2022
1 parent 50195bb commit 48f6007
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 58 deletions.
Expand Up @@ -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;
}

Expand Down
40 changes: 20 additions & 20 deletions api/src/main/java/net/kyori/adventure/text/Component.java
Expand Up @@ -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.
*
Expand All @@ -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.
*
Expand Down
24 changes: 12 additions & 12 deletions api/src/main/java/net/kyori/adventure/text/format/Style.java
Expand Up @@ -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.
*
Expand All @@ -364,18 +376,6 @@ default boolean hasDecoration(final @NotNull TextDecoration decoration) {
@Override
@NotNull Style decorations(final @NotNull Map<TextDecoration, TextDecoration.State> 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.
*
Expand Down
21 changes: 11 additions & 10 deletions api/src/main/java/net/kyori/adventure/text/format/StyleImpl.java
Expand Up @@ -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<TextDecoration, TextDecoration.State> decorations() {
return this.decorations;
}

@Override
public @NotNull Style decorations(final @NotNull Map<TextDecoration, TextDecoration.State> 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");
Expand All @@ -135,6 +125,16 @@ final class StyleImpl implements Style {
throw new IllegalArgumentException(String.format("unknown decoration '%s'", decoration));
}

@Override
public @NotNull Map<TextDecoration, TextDecoration.State> decorations() {
return this.decorations;
}

@Override
public @NotNull Style decorations(final @NotNull Map<TextDecoration, TextDecoration.State> 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;
Expand Down Expand Up @@ -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);
Expand Down
20 changes: 10 additions & 10 deletions api/src/main/java/net/kyori/adventure/text/format/StyleSetter.java
Expand Up @@ -125,6 +125,16 @@ public interface StyleSetter<T extends 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.
*
Expand All @@ -149,16 +159,6 @@ public interface StyleSetter<T extends 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.
*
Expand Down
Expand Up @@ -30,7 +30,7 @@

import static net.kyori.adventure.text.TextAssertions.assertDecorations;

public class ComponentDecorationTest {
class ComponentDecorationTest {

@Test
void testDecorationIfAbsent() {
Expand Down

0 comments on commit 48f6007

Please sign in to comment.