Skip to content

Commit

Permalink
api: Add contains method that allows alternative methods of equality …
Browse files Browse the repository at this point in the history
…comparison

fixes #363
  • Loading branch information
kashike committed May 17, 2021
1 parent 8d8a765 commit e0542b5
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
37 changes: 34 additions & 3 deletions api/src/main/java/net/kyori/adventure/text/Component.java
Expand Up @@ -28,7 +28,9 @@
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.function.BiPredicate;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.UnaryOperator;
Expand Down Expand Up @@ -93,6 +95,19 @@
*/
@ApiStatus.NonExtendable
public interface Component extends ComponentBuilderApplicable, ComponentLike, Examinable, HoverEventSource<Component> {
/**
* A predicate that checks equality of two {@code Component}s using {@link Objects#equals(Object, Object)}.
*
* @since 4.8.0
*/
BiPredicate<? super Component, ? super Component> EQUALS = Objects::equals;
/**
* A predicate that checks equality of two {@code Component}s.
*
* @since 4.8.0
*/
BiPredicate<? super Component, ? super Component> EQUALS_IDENTITY = (a, b) -> a == b;

/**
* Gets an empty component.
*
Expand Down Expand Up @@ -1270,23 +1285,39 @@ static TranslatableComponent translatable(final @NonNull String key, final @Null
/**
* Checks if this component contains a component.
*
* <p>This method uses <b>identity</b> comparison when checking for contains. Use {@link #contains(Component, BiPredicate)} with {@link #EQUALS} if you
* wish to use full equality comparison.</p>
*
* @param that the other component
* @return {@code true} if this component contains the provided
* component, {@code false} otherwise
* @since 4.0.0
*/
default boolean contains(final @NonNull Component that) {
if(this == that) return true;
return this.contains(that, EQUALS_IDENTITY);
}

/**
* Checks if this component contains a component.
*
* @param that the other component
* @param equals the equality tester
* @return {@code true} if this component contains the provided
* component, {@code false} otherwise
* @since 4.8.0
*/
default boolean contains(final @NonNull Component that, final @NonNull BiPredicate<? super Component, ? super Component> equals) {
if(equals.test(this, that)) return true;
for(final Component child : this.children()) {
if(child.contains(that)) return true;
if(child.contains(that, equals)) return true;
}
final @Nullable HoverEvent<?> hoverEvent = this.hoverEvent();
if(hoverEvent != null) {
if(hoverEvent.action().type().isAssignableFrom(Component.class)) {
final Component hover = (Component) hoverEvent.value();
if(that == hover) return true;
for(final Component child : hover.children()) {
if(child.contains(that)) return true;
if(child.contains(that, equals)) return true;
}
}
}
Expand Down
12 changes: 12 additions & 0 deletions api/src/test/java/net/kyori/adventure/text/TextComponentTest.java
Expand Up @@ -142,6 +142,18 @@ void testContains() {
assertTrue(component.contains(child));
}

// https://github.com/KyoriPowered/adventure/issues/363
@Test
void testContainsEquality() {
final Component c0 = Component.text("best!");
final Component c1 = Component.text()
.append(Component.text("Nero "))
.append(Component.text(" are the ").append(c0))
.build();

assertTrue(c1.contains(Component.text("best!"), Component.EQUALS));
}

@Test
void testContent() {
final TextComponent c0 = Component.text("foo");
Expand Down

0 comments on commit e0542b5

Please sign in to comment.