NodeItem: inline verifyChildWithSameNameDoesNotAlreadyExis
This commit is contained in:
parent
151a6f351c
commit
eb4acbaf89
1 changed files with 7 additions and 12 deletions
|
@ -143,7 +143,13 @@ class NodeItem<T> implements Node<T> {
|
||||||
@Override
|
@Override
|
||||||
public void addChild(@NonNull final Node<T> child) {
|
public void addChild(@NonNull final Node<T> child) {
|
||||||
verifyChildIsNotAnAncestor(child);
|
verifyChildIsNotAnAncestor(child);
|
||||||
verifyChildWithSameNameDoesNotAlreadyExist(child);
|
//verifyChildWithSameNameDoesNotAlreadyExist
|
||||||
|
if (child.isNamed()) {
|
||||||
|
findChildByName(child.getName()).filter(existingChild -> existingChild != child)
|
||||||
|
.ifPresent(existingChild -> {
|
||||||
|
throw new NodeException("Node with that name already exists here");
|
||||||
|
});
|
||||||
|
}
|
||||||
children.add(child);
|
children.add(child);
|
||||||
// update the child's parent if they don't have one or it is not this
|
// update the child's parent if they don't have one or it is not this
|
||||||
val childParent = child.getParent();
|
val childParent = child.getParent();
|
||||||
|
@ -153,17 +159,6 @@ class NodeItem<T> implements Node<T> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void verifyChildWithSameNameDoesNotAlreadyExist(
|
|
||||||
final @NonNull Node<T> child
|
|
||||||
) {
|
|
||||||
if (child.isNamed()) {
|
|
||||||
findChildByName(child.getName()).filter(existingChild -> existingChild != child)
|
|
||||||
.ifPresent(existingChild -> {
|
|
||||||
throw new NodeException("Node with that name already exists here");
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void verifyChildIsNotAnAncestor(final @NonNull Node<T> child) {
|
private void verifyChildIsNotAnAncestor(final @NonNull Node<T> child) {
|
||||||
if (this.equals(child) || isDescendantOf(child)) {
|
if (this.equals(child) || isDescendantOf(child)) {
|
||||||
throw new NodeException("Child is an ancestor");
|
throw new NodeException("Child is an ancestor");
|
||||||
|
|
Loading…
Reference in a new issue