From c77f9c29d399ce71ee813d3c21c0c21d6b8bf9f8 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Tue, 20 Sep 2016 23:09:45 +0100 Subject: [PATCH] NodeItem: don't call overridable method from constructor --- src/main/java/net/kemitix/node/NodeItem.java | 21 ++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/main/java/net/kemitix/node/NodeItem.java b/src/main/java/net/kemitix/node/NodeItem.java index d933770..9921dc2 100644 --- a/src/main/java/net/kemitix/node/NodeItem.java +++ b/src/main/java/net/kemitix/node/NodeItem.java @@ -64,13 +64,18 @@ class NodeItem implements Node { this.data = data; this.name = name; if (parent != null) { - setParent(parent); + doSetParent(parent); } this.children.addAll(children); } - protected void forceParent(final Node parent) { - this.parent = parent; + /** + * Sets the parent of a node without updating the parent in the process as {@link #setParent(Node)} does. + * + * @param newParent The new parent node + */ + protected void forceParent(final Node newParent) { + this.parent = newParent; } @Override @@ -110,15 +115,19 @@ class NodeItem implements Node { */ @Override public void setParent(@NonNull final Node parent) { - if (this.equals(parent) || parent.isDescendantOf(this)) { + doSetParent(parent); + } + + private void doSetParent(@NonNull final Node newParent) { + if (this.equals(newParent) || newParent.isDescendantOf(this)) { throw new NodeException("Parent is a descendant"); } if (this.parent != null) { this.parent.getChildren() .remove(this); } - this.parent = parent; - parent.addChild(this); + this.parent = newParent; + newParent.addChild(this); } @Override