diff --git a/api/src/main/java/net/kyori/adventure/text/format/TextDecoration.java b/api/src/main/java/net/kyori/adventure/text/format/TextDecoration.java index 915629f44..e5a7b830b 100644 --- a/api/src/main/java/net/kyori/adventure/text/format/TextDecoration.java +++ b/api/src/main/java/net/kyori/adventure/text/format/TextDecoration.java @@ -25,9 +25,12 @@ import net.kyori.adventure.text.Component; import net.kyori.adventure.util.Index; +import net.kyori.adventure.util.TriState; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import static java.util.Objects.requireNonNull; + /** * An enumeration of decorations which may be applied to a {@link Component}. * @@ -166,5 +169,22 @@ public String toString() { public static @NotNull State byBoolean(final @Nullable Boolean flag) { return flag == null ? NOT_SET : byBoolean(flag.booleanValue()); } + + /** + * Gets a state from a {@link net.kyori.adventure.util.TriState}. + * + * @param flag the tristate + * @return the state + * @since 4.10.0 + */ + public static @NotNull State byTriState(final @NotNull TriState flag) { + requireNonNull(flag); + switch (flag) { + case TRUE: return TRUE; + case FALSE: return FALSE; + case NOT_SET: return NOT_SET; + } + throw new IllegalArgumentException("Unable to turn TriState: " + flag + " into a TextDecoration.State"); + } } } diff --git a/api/src/test/java/net/kyori/adventure/text/format/TextDecorationTest.java b/api/src/test/java/net/kyori/adventure/text/format/TextDecorationTest.java index 490d58bdc..3eabdb3d4 100644 --- a/api/src/test/java/net/kyori/adventure/text/format/TextDecorationTest.java +++ b/api/src/test/java/net/kyori/adventure/text/format/TextDecorationTest.java @@ -23,9 +23,13 @@ */ package net.kyori.adventure.text.format; +import java.util.Arrays; +import net.kyori.adventure.util.TriState; import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; class TextDecorationTest { @Test @@ -34,4 +38,14 @@ void testByBoolean() { assertEquals(TextDecoration.State.FALSE, TextDecoration.State.byBoolean(false)); assertEquals(TextDecoration.State.TRUE, TextDecoration.State.byBoolean(true)); } + + @Test + void testByTristate() { + assertEquals(TextDecoration.State.NOT_SET, TextDecoration.State.byTriState(TriState.NOT_SET)); + assertEquals(TextDecoration.State.FALSE, TextDecoration.State.byTriState(TriState.FALSE)); + assertEquals(TextDecoration.State.TRUE, TextDecoration.State.byTriState(TriState.TRUE)); + + assertThrows(NullPointerException.class, () -> TextDecoration.State.byTriState(null)); + assertDoesNotThrow(() -> Arrays.stream(TriState.values()).forEach(TextDecoration.State::byTriState)); + } }