Skip to content

Commit

Permalink
Merge pull request #439 from KyoriPowered/fix/append-nullchecks
Browse files Browse the repository at this point in the history
  • Loading branch information
zml2008 authored Sep 6, 2021
2 parents eb867ff + 02449c2 commit 071d128
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import static java.util.Objects.requireNonNull;

/**
* An abstract implementation of a component builder.
*
Expand Down Expand Up @@ -74,13 +76,14 @@ protected AbstractComponentBuilder(final @NotNull C component) {
public @NotNull B append(final @NotNull Component component) {
if (component == Component.empty()) return (B) this;
this.prepareChildren();
this.children.add(component);
this.children.add(requireNonNull(component, "component"));
return (B) this;
}

@Override
@SuppressWarnings("unchecked")
public @NotNull B append(final @NotNull Component@NotNull... components) {
requireNonNull(components, "components");
boolean prepared = false;
for (int i = 0, length = components.length; i < length; i++) {
final Component component = components[i];
Expand All @@ -89,7 +92,7 @@ protected AbstractComponentBuilder(final @NotNull C component) {
this.prepareChildren();
prepared = true;
}
this.children.add(component);
this.children.add(requireNonNull(component, "components[?]"));
}
}
return (B) this;
Expand All @@ -98,6 +101,7 @@ protected AbstractComponentBuilder(final @NotNull C component) {
@Override
@SuppressWarnings("unchecked")
public @NotNull B append(final @NotNull ComponentLike@NotNull... components) {
requireNonNull(components, "components");
boolean prepared = false;
for (int i = 0, length = components.length; i < length; i++) {
final Component component = components[i].asComponent();
Expand All @@ -106,7 +110,7 @@ protected AbstractComponentBuilder(final @NotNull C component) {
this.prepareChildren();
prepared = true;
}
this.children.add(component);
this.children.add(requireNonNull(component, "components[?]"));
}
}
return (B) this;
Expand All @@ -115,6 +119,7 @@ protected AbstractComponentBuilder(final @NotNull C component) {
@Override
@SuppressWarnings("unchecked")
public @NotNull B append(final @NotNull Iterable<? extends ComponentLike> components) {
requireNonNull(components, "components");
boolean prepared = false;
for (final ComponentLike like : components) {
final Component component = like.asComponent();
Expand All @@ -123,7 +128,7 @@ protected AbstractComponentBuilder(final @NotNull C component) {
this.prepareChildren();
prepared = true;
}
this.children.add(component);
this.children.add(requireNonNull(component, "components[?]"));
}
}
return (B) this;
Expand Down Expand Up @@ -167,7 +172,7 @@ private void prepareChildren() {
if (!(child instanceof BuildableComponent<?, ?>)) {
continue;
}
final BuildableComponent<?, ?> mappedChild = function.apply((BuildableComponent<?, ?>) child);
final BuildableComponent<?, ?> mappedChild = requireNonNull(function.apply((BuildableComponent<?, ?>) child), "mappedChild");
if (child == mappedChild) {
continue;
}
Expand All @@ -188,7 +193,7 @@ private void prepareChildren() {
if (!(child instanceof BuildableComponent<?, ?>)) {
continue;
}
final BuildableComponent<?, ?> mappedChild = function.apply((BuildableComponent<?, ?>) child);
final BuildableComponent<?, ?> mappedChild = requireNonNull(function.apply((BuildableComponent<?, ?>) child), "mappedChild");
if (mappedChild.children().isEmpty()) {
if (child == mappedChild) {
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import static java.util.Objects.requireNonNull;

/**
* Some magic to change return types.
*
Expand Down Expand Up @@ -77,7 +79,7 @@ public interface ScopedComponent<C extends Component> extends Component {
default @NotNull C append(final @NotNull Component component) {
if (component == Component.empty()) return (C) this;
final List<Component> oldChildren = this.children();
return this.children(MonkeyBars.addOne(oldChildren, component));
return this.children(MonkeyBars.addOne(oldChildren, requireNonNull(component, "component")));
}

@Override
Expand Down

0 comments on commit 071d128

Please sign in to comment.