diff --git a/src/main/java/net/kemitix/node/Node.java b/src/main/java/net/kemitix/node/Node.java index 0a96c66..4d95501 100644 --- a/src/main/java/net/kemitix/node/Node.java +++ b/src/main/java/net/kemitix/node/Node.java @@ -162,4 +162,15 @@ public interface Node { */ boolean isNamed(); + /** + * Remove the node from the children. + * + * @param node the node to be removed + */ + void removeChild(Node node); + + /** + * Removes the parent from the node. Makes the node into a new root node. + */ + void removeParent(); } diff --git a/src/main/java/net/kemitix/node/NodeItem.java b/src/main/java/net/kemitix/node/NodeItem.java index 88764e4..c02cf86 100644 --- a/src/main/java/net/kemitix/node/NodeItem.java +++ b/src/main/java/net/kemitix/node/NodeItem.java @@ -305,6 +305,26 @@ public class NodeItem implements Node { throw new NodeException("Named child not found"); } + @Override + public void removeChild(final Node 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 public String drawTree(final int depth) { final StringBuilder sb = new StringBuilder();