Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

api: add TextDecoration#withState and add missing null pointer check #490

Merged
merged 5 commits into from Dec 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -81,27 +81,64 @@ public enum TextDecoration implements StyleBuilderApplicable, TextFormat {
}

/**
* Creates a {@link TextDecorationAndState}.
* Creates a {@link TextDecorationAndState}, annotating this decoration with the given {@code state}.
*
* @param state the state
* @return a {@link TextDecorationAndState}
* @since 4.8.0
* @deprecated for removal since 4.10.0, use {@link #withState(boolean)} instead
*/
@Deprecated
public final @NotNull TextDecorationAndState as(final boolean state) {
return this.as(State.byBoolean(state));
return this.withState(state);
}

/**
* Creates a {@link TextDecorationAndState}.
* Creates a {@link TextDecorationAndState}, annotating this decoration with the given {@code state}.
*
* @param state the state
* @return a {@link TextDecorationAndState}
* @since 4.8.0
* @deprecated for removal since 4.10.0, use {@link #withState(State)} instead
*/
@Deprecated
public final @NotNull TextDecorationAndState as(final @NotNull State state) {
return this.withState(state);
}

/**
* An alias for {@link #as(boolean)}.
*
* @param state the state
* @return a {@link TextDecorationAndState}
* @since 4.10.0
*/
public final @NotNull TextDecorationAndState withState(final boolean state) {
return new TextDecorationAndStateImpl(this, State.byBoolean(state));
}

/**
* An alias for {@link #as(State)}.
*
* @param state the state
* @return a {@link TextDecorationAndState}
* @since 4.10.0
*/
public final @NotNull TextDecorationAndState withState(final @NotNull State state) {
return new TextDecorationAndStateImpl(this, state);
}

/**
* An alias for {@link #as(State)}.
*
* @param state the state
* @return a {@link TextDecorationAndState}
* @since 4.10.0
*/
public final @NotNull TextDecorationAndState withState(final @NotNull TriState state) {
return new TextDecorationAndStateImpl(this, State.byTriState(state));
}
zml2008 marked this conversation as resolved.
Show resolved Hide resolved

@Override
public void styleApply(final Style.@NotNull Builder style) {
style.decorate(this);
Expand Down
Expand Up @@ -27,13 +27,17 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import static java.util.Objects.requireNonNull;

final class TextDecorationAndStateImpl implements TextDecorationAndState {
private final TextDecoration decoration;
private final TextDecoration.State state;

TextDecorationAndStateImpl(final TextDecoration decoration, final TextDecoration.State state) {
// no null check is required on the decoration since this constructor is always invoked in such a way that
// decoration is always non-null
this.decoration = decoration;
this.state = state;
this.state = requireNonNull(state, "state");
}

@Override
Expand Down
16 changes: 8 additions & 8 deletions api/src/test/java/net/kyori/adventure/text/format/StyleTest.java
Expand Up @@ -90,27 +90,27 @@ void testOfApplicables() {
@Test
void testOfTextDecorationAndState() {
final Style s0 = Style.style(
TextDecoration.BOLD.as(TextDecoration.State.TRUE),
TextDecoration.ITALIC.as(TextDecoration.State.FALSE)
TextDecoration.BOLD.withState(TextDecoration.State.TRUE),
TextDecoration.ITALIC.withState(TextDecoration.State.FALSE)
);
assertDecorations(s0, ImmutableSet.of(TextDecoration.BOLD), ImmutableSet.of(TextDecoration.ITALIC));
final Style s1 = Style.style(
TextDecoration.BOLD.as(true),
TextDecoration.ITALIC.as(false)
TextDecoration.BOLD.withState(true),
TextDecoration.ITALIC.withState(false)
);
assertDecorations(s1, ImmutableSet.of(TextDecoration.BOLD), ImmutableSet.of(TextDecoration.ITALIC));
}

@Test
void testOfTextDecorationAndStateOverridesWhenSame() {
final Style s0 = Style.style(
TextDecoration.BOLD.as(TextDecoration.State.TRUE),
TextDecoration.BOLD.as(TextDecoration.State.FALSE)
TextDecoration.BOLD.withState(TextDecoration.State.TRUE),
TextDecoration.BOLD.withState(TextDecoration.State.FALSE)
);
assertDecorations(s0, ImmutableSet.of(), ImmutableSet.of(TextDecoration.BOLD));
final Style s1 = Style.style(
TextDecoration.BOLD.as(true),
TextDecoration.BOLD.as(false)
TextDecoration.BOLD.withState(true),
TextDecoration.BOLD.withState(false)
);
assertDecorations(s1, ImmutableSet.of(), ImmutableSet.of(TextDecoration.BOLD));
}
Expand Down
Expand Up @@ -39,6 +39,12 @@ void testByBoolean() {
assertEquals(TextDecoration.State.TRUE, TextDecoration.State.byBoolean(true));
}

@Test
void testWithStateThrowsOnNull() {
assertThrows(NullPointerException.class, () -> TextDecoration.BOLD.withState((TextDecoration.State) null));
assertThrows(NullPointerException.class, () -> TextDecoration.BOLD.withState((TriState) null));
}

@Test
void testByTristate() {
assertEquals(TextDecoration.State.NOT_SET, TextDecoration.State.byTriState(TriState.NOT_SET));
Expand Down