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 methods to join components using a collector #292

Merged
merged 2 commits into from
Mar 1, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,11 @@ private void prepareChildren() {
return (B) this;
}

@Override
public @NonNull List<Component> children() {
return Collections.unmodifiableList(this.children);
}

@Override
@SuppressWarnings("unchecked")
public @NonNull B style(final @NonNull Style style) {
Expand Down
39 changes: 39 additions & 0 deletions api/src/main/java/net/kyori/adventure/text/Component.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.util.function.Function;
import java.util.function.UnaryOperator;
import java.util.regex.Pattern;
import java.util.stream.Collector;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.text.event.ClickEvent;
import net.kyori.adventure.text.event.HoverEvent;
Expand Down Expand Up @@ -159,6 +160,44 @@ public interface Component extends ComponentBuilderApplicable, ComponentLike, Ex
return builder.build();
}

/**
* Create a collector that will join components without a separator.
*
* @return a collector that can join components
* @since 4.6.0
*/
static @NonNull Collector<Component, ? extends ComponentBuilder<?, ?>, Component> toComponent() {
return toComponent(Component.empty());
}

/**
* Create a collector that will join components using the provided separator.
*
* @param separator the separator to join with
* @return a collector that can join components
* @since 4.6.0
*/
static @NonNull Collector<Component, ? extends ComponentBuilder<?, ?>, Component> toComponent(final @NonNull Component separator) {
return Collector.of(
Component::text,
(builder, add) -> {
if(separator != Component.empty() && !builder.children().isEmpty()) {
builder.append(separator);
}
builder.append(add);
}, (a, b) -> {
final List<Component> aChildren = a.children();
final TextComponent.Builder ret = Component.text().append(aChildren);
if(!aChildren.isEmpty()) {
ret.append(separator);
}
ret.append(b.children());
return ret;
},
TextComponent.Builder::build
);
}

/*
* ---------------------------
* ---- BlockNBTComponent ----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/
package net.kyori.adventure.text;

import java.util.List;
import java.util.Set;
import java.util.function.Consumer;
import java.util.function.Function;
Expand Down Expand Up @@ -155,6 +156,14 @@ public interface ComponentBuilder<C extends BuildableComponent<C, B>, B extends
@Contract("_ -> this")
@NonNull B mapChildrenDeep(final @NonNull Function<BuildableComponent<?, ?>, ? extends BuildableComponent<?, ?>> function);

/**
* Get an unmodifiable list containing all children currently in this builder.
*
* @return the list of children
* @since 4.6.0
*/
@NonNull List<Component> children();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this method necessary?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's not strictly required -- the collector could just work with the builder implementation -- but it feels weird to not expose functionality the collector needs to the api.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not really a fan of exposing getters on builders... but it looks like we do for content() as well, so I guess this is okay.


/**
* Sets the style.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;
import net.kyori.adventure.text.event.ClickEvent;
import net.kyori.adventure.text.event.HoverEvent;
import net.kyori.adventure.text.format.NamedTextColor;
Expand Down Expand Up @@ -376,4 +377,39 @@ void testBuilderApplyDeep() {
assertThat(children).hasSize(2);
forEachTransformAndAssert(children, Component::color, color -> assertEquals(NamedTextColor.GREEN, color));
}

@Test
void testCollectorNoSeparator() {
final Component joined = Stream.of(
Component.text("Hello", NamedTextColor.RED),
Component.text("World")
).collect(Component.toComponent());
final Component expected = Component.text()
.append(Component.text("Hello", NamedTextColor.RED))
.append(Component.text("World"))
.build();

assertEquals(expected, joined);
}

@Test
void testCollectorWithSeparator() {
final Component joined = Stream.of(
Component.text("Hello", NamedTextColor.RED),
Component.text("World")
).collect(Component.toComponent(Component.space()));

final Component expected = Component.text()
.append(Component.text("Hello", NamedTextColor.RED))
.append(Component.space())
.append(Component.text("World"))
.build();

assertEquals(expected, joined);
}

@Test
void testCollectOnEmptyStreamReturnsEmptyComponent() {
assertEquals(Component.empty(), Stream.<Component>of().collect(Component.toComponent()));
}
}