From 9fca56b4c6781fd1b2cd2041c241d2630ce3bba3 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Tue, 24 May 2016 11:30:55 +0100 Subject: [PATCH] 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. --- src/main/java/net/kemitix/node/Node.java | 11 +++++++++++ src/main/java/net/kemitix/node/NodeItem.java | 20 ++++++++++++++++++++ 2 files changed, 31 insertions(+) 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();