Skip to content

Commit

Permalink
Merge pull request #292 from KyoriPowered/feature/collector
Browse files Browse the repository at this point in the history
  • Loading branch information
zml2008 authored Mar 1, 2021
2 parents bbcfe9b + 91b5636 commit 931b841
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 0 deletions.
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();

/**
* 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()));
}
}

0 comments on commit 931b841

Please sign in to comment.