From 4e565d4d6a2d19b2bc99c328d1d61b0ab5df9efd Mon Sep 17 00:00:00 2001 From: Bjarne Koll Date: Sun, 24 Oct 2021 02:18:37 +0200 Subject: [PATCH] Add standard join configurations While using join configurations is already rather simple, they tend to clutter up already rather verbose source code. While the initial creation of the JoinConfiguration already included a null configuration, accessible through 'JoinConfiguration#noSeparators', other standards or often used layouts were not added. This commit adds three more defaults/standards to the join configuration implementation that are exposed through static accessors in the join configuration interface. Specifically: newLines: A straight forward instance of the join configuraiton that uses the new line component to join together components. commas: Another straight forward instance of the join configuration that uses a single comma to join together components, creating a CSV like layout. arrayLike: A more complex join configuration that recreates the layout used by the java.util.Arrays#toString method. Resolves: #466 --- .../adventure/text/JoinConfiguration.java | 47 +++++++++++++++++++ .../adventure/text/JoinConfigurationImpl.java | 8 ++++ .../net/kyori/adventure/text/JoinTest.java | 47 +++++++++++++++++++ 3 files changed, 102 insertions(+) diff --git a/api/src/main/java/net/kyori/adventure/text/JoinConfiguration.java b/api/src/main/java/net/kyori/adventure/text/JoinConfiguration.java index 41c36df383..8fd58e8d55 100644 --- a/api/src/main/java/net/kyori/adventure/text/JoinConfiguration.java +++ b/api/src/main/java/net/kyori/adventure/text/JoinConfiguration.java @@ -99,6 +99,53 @@ public interface JoinConfiguration extends Buildable + * A purely text based example of this syntax, without introducing the concepts of components, would join the two strings 'hello' and 'there' together, + * creating the following output: 'hello\nthere'. + *

+ * + * @return the join configuration. + * @since 4.10.0 + */ + static @NotNull JoinConfiguration newlines() { + return JoinConfigurationImpl.STANDARD_NEW_LINES; + } + + /** + * Provides a join configuration with no prefix or suffix that simply joins the components together using a single comma, matching a CSV like layout. + * + *

+ * A purely text based example of this syntax, without introducing the concepts of components, would join the two strings 'hello' and 'there' together, + * creating the following output: 'hello,there'. + *

+ * + * @return the join configuration. + * @since 4.10.0 + */ + static @NotNull JoinConfiguration commas() { + return JoinConfigurationImpl.STANDARD_COMMA_SEPARATED; + } + + /** + * Provides a join configuration that joins components together in the same manner {@link java.util.Arrays#toString(Object[])} stringifies an array. + * Specifically, the join configuration prefixes and suffixes the components with an open or closed square bracket respectively. + * Components themselves are joined together using a comma and a space. + * +

+ * A purely text based example of this syntax, without introducing the concepts of components, would join the two strings 'hello' and 'there' together, + * creating the following output: '[hello, there]'. + *

+ * + * @return the join configuration. + * @since 4.10.0 + */ + static @NotNull JoinConfiguration arrayLike() { + return JoinConfigurationImpl.STANDARD_ARRAY_LIKE; + } + /** * Creates a join configuration with a separator and no prefix or suffix. * diff --git a/api/src/main/java/net/kyori/adventure/text/JoinConfigurationImpl.java b/api/src/main/java/net/kyori/adventure/text/JoinConfigurationImpl.java index beef7d3c8b..09cf1655f2 100644 --- a/api/src/main/java/net/kyori/adventure/text/JoinConfigurationImpl.java +++ b/api/src/main/java/net/kyori/adventure/text/JoinConfigurationImpl.java @@ -39,6 +39,14 @@ final class JoinConfigurationImpl implements JoinConfiguration { static final Predicate DEFAULT_PREDICATE = componentLike -> true; static final JoinConfigurationImpl NULL = new JoinConfigurationImpl(); + static final JoinConfiguration STANDARD_NEW_LINES = JoinConfiguration.separator(Component.newline()); + static final JoinConfiguration STANDARD_COMMA_SEPARATED = JoinConfiguration.separator(Component.text(",")); + static final JoinConfiguration STANDARD_ARRAY_LIKE = JoinConfiguration.builder() + .separator(Component.text(", ")) + .prefix(Component.text("[")) + .suffix(Component.text("]")) + .build(); + private final Component prefix; private final Component suffix; private final Component separator; diff --git a/api/src/test/java/net/kyori/adventure/text/JoinTest.java b/api/src/test/java/net/kyori/adventure/text/JoinTest.java index 89f2494765..f1b92cc6af 100644 --- a/api/src/test/java/net/kyori/adventure/text/JoinTest.java +++ b/api/src/test/java/net/kyori/adventure/text/JoinTest.java @@ -257,6 +257,53 @@ final void testWithPredicate() { ); } + @Test + final void testStandardJoinConfigurationsNewLines() { + final Component result = Component.join(JoinConfiguration.newlines(), Component.text("line 1"), Component.text("line 2"), Component.text("line 3")); + assertEquals( + Component.text() + .append(Component.text("line 1")) + .append(Component.newline()) + .append(Component.text("line 2")) + .append(Component.newline()) + .append(Component.text("line 3")) + .build(), + result + ); + } + + @Test + final void testStandardJoinConfigurationsCommas() { + final Component result = Component.join(JoinConfiguration.commas(), Component.text("line 1"), Component.text("line 2"), Component.text("line 3")); + assertEquals( + Component.text() + .append(Component.text("line 1")) + .append(Component.text(",")) + .append(Component.text("line 2")) + .append(Component.text(",")) + .append(Component.text("line 3")) + .build(), + result + ); + } + + @Test + final void testStandardJoinConfigurationsArrayLike() { + final Component result = Component.join(JoinConfiguration.arrayLike(), Component.text("line 1"), Component.text("line 2"), Component.text("line 3")); + assertEquals( + Component.text() + .append(Component.text("[")) + .append(Component.text("line 1")) + .append(Component.text(", ")) + .append(Component.text("line 2")) + .append(Component.text(", ")) + .append(Component.text("line 3")) + .append(Component.text("]")) + .build(), + result + ); + } + private static final class TestComponentLike implements ComponentLike { @Override