Node: remove{Child,Parent}(): split trees branches apart

removeChild(node): removes the node from children of the current node,
making the child node into a new root node.

removeParent(): removes the current from from it's parent's children,
making itself into a new root node.
This commit is contained in:
Paul Campbell 2016-05-24 11:30:55 +01:00
parent b2c3032ec0
commit 9fca56b4c6
2 changed files with 31 additions and 0 deletions

View file

@ -162,4 +162,15 @@ public interface Node<T> {
*/ */
boolean isNamed(); boolean isNamed();
/**
* Remove the node from the children.
*
* @param node the node to be removed
*/
void removeChild(Node<T> node);
/**
* Removes the parent from the node. Makes the node into a new root node.
*/
void removeParent();
} }

View file

@ -305,6 +305,26 @@ public class NodeItem<T> implements Node<T> {
throw new NodeException("Named child not found"); throw new NodeException("Named child not found");
} }
@Override
public void removeChild(final Node<T> node) {
if (children.remove(node)) {
node.removeParent();
}
}
@Override
public void removeParent() {
if (parent != null) {
parent.removeChild(this);
parent = null;
if (nameSupplier == null) {
// this is now a root node, so must provide a default name
// supplier
nameSupplier = n -> null;
}
}
}
@Override @Override
public String drawTree(final int depth) { public String drawTree(final int depth) {
final StringBuilder sb = new StringBuilder(); final StringBuilder sb = new StringBuilder();