NodeItem: inline verifyChildWithSameNameDoesNotAlreadyExis

This commit is contained in:
Paul Campbell 2017-02-16 21:09:09 +00:00
parent 151a6f351c
commit eb4acbaf89

View file

@ -143,7 +143,13 @@ class NodeItem<T> implements Node<T> {
@Override
public void addChild(@NonNull final Node<T> 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);
// update the child's parent if they don't have one or it is not this
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) {
if (this.equals(child) || isDescendantOf(child)) {
throw new NodeException("Child is an ancestor");