Skip to content

Commit

Permalink
Merge pull request #482 from lynxplay/feature/standard-join-configura…
Browse files Browse the repository at this point in the history
…tions

Add standard join configurations
  • Loading branch information
zml2008 authored Nov 18, 2021
2 parents 1a1a04b + 5d4384a commit 9f56a8b
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 0 deletions.
42 changes: 42 additions & 0 deletions api/src/main/java/net/kyori/adventure/text/JoinConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,48 @@ public interface JoinConfiguration extends Buildable<JoinConfiguration, JoinConf
return JoinConfigurationImpl.NULL;
}

/**
* Provides a join configuration with no prefix or suffix that simply joins the components together using the {@link Component#newline()} component.
*
* <p>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'.</p>
*
* @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.
*
* <p>A purely text based example of this syntax, without introducing the concepts of components, would join the two strings 'hello' and 'there' together,
* creating either the output 'hello,there' or 'hello, there' depending on whether the passed boolean flag was {@code false} or {@code true} respectively.</p>
*
* @param spaces a plain boolean flag indicating whether the returned comma-based join configuration should append a single space after each comma or not
* @return the join configuration
* @since 4.10.0
*/
static @NotNull JoinConfiguration commas(final boolean spaces) {
return spaces ? JoinConfigurationImpl.STANDARD_COMMA_SPACE_SEPARATED : 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.
*
* <p>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]'.</p>
*
* @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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ final class JoinConfigurationImpl implements JoinConfiguration {
static final Predicate<ComponentLike> 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_COMMA_SPACE_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;
Expand Down
62 changes: 62 additions & 0 deletions api/src/test/java/net/kyori/adventure/text/JoinTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,68 @@ 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(false), 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 testStandardJoinConfigurationsCommasSpaced() {
final Component result = Component.join(JoinConfiguration.commas(true), 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
Expand Down

0 comments on commit 9f56a8b

Please sign in to comment.