From 04599861c0dc5da89b6981794857ca30d0c73ab6 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Tue, 24 May 2016 14:43:36 +0100 Subject: [PATCH] Node.createChild(data, name): can create named child nodes # Conflicts: # src/test/java/net/kemitix/node/NodeItemTest.java --- src/main/java/net/kemitix/node/Node.java | 9 +++++++++ src/main/java/net/kemitix/node/NodeItem.java | 7 +++++++ src/test/java/net/kemitix/node/NodeItemTest.java | 13 +++++++++++++ 3 files changed, 29 insertions(+) diff --git a/src/main/java/net/kemitix/node/Node.java b/src/main/java/net/kemitix/node/Node.java index a01efa7..04bec0e 100644 --- a/src/main/java/net/kemitix/node/Node.java +++ b/src/main/java/net/kemitix/node/Node.java @@ -88,6 +88,15 @@ public interface Node { */ Node createChild(T child); + /** + * Creates a new named node and adds it as a child of the current node. + * + * @param child the child node's data + * + * @return the new child node + */ + Node createChild(T child, String name); + /** * Populates the tree with the path of nodes, each being a child of the * previous node in the path. diff --git a/src/main/java/net/kemitix/node/NodeItem.java b/src/main/java/net/kemitix/node/NodeItem.java index 5938e71..c9d70f8 100644 --- a/src/main/java/net/kemitix/node/NodeItem.java +++ b/src/main/java/net/kemitix/node/NodeItem.java @@ -189,6 +189,13 @@ public class NodeItem implements Node { return new NodeItem<>(child, this); } + @Override + public Node createChild(final T child, final String name) { + Node node = createChild(child); + node.setName(name); + return node; + } + /** * Populates the tree with the path of nodes, each being a child of the * previous node in the path. diff --git a/src/test/java/net/kemitix/node/NodeItemTest.java b/src/test/java/net/kemitix/node/NodeItemTest.java index 8cd5ca4..53b0c07 100644 --- a/src/test/java/net/kemitix/node/NodeItemTest.java +++ b/src/test/java/net/kemitix/node/NodeItemTest.java @@ -888,4 +888,17 @@ public class NodeItemTest { //then assertThat(node.getData()).isEqualTo("updated"); } + + @Test + @SuppressWarnings("unchecked") + public void canCreateNamedChild() { + //given + node = new NodeItem<>(null); + //when + Node child = node.createChild("child data", "child name"); + //then + assertThat(child.getName()).isEqualTo("child name"); + assertThat(child.getParent()).isSameAs(node); + assertThat(node.getChildren()).containsExactly(child); + } }