Node.createChild(data, name): can create named child nodes

# Conflicts:
#	src/test/java/net/kemitix/node/NodeItemTest.java
This commit is contained in:
Paul Campbell 2016-05-24 14:43:36 +01:00
parent fc55bacb04
commit 04599861c0
3 changed files with 29 additions and 0 deletions

View file

@ -88,6 +88,15 @@ public interface Node<T> {
*/ */
Node<T> createChild(T child); Node<T> 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<T> createChild(T child, String name);
/** /**
* Populates the tree with the path of nodes, each being a child of the * Populates the tree with the path of nodes, each being a child of the
* previous node in the path. * previous node in the path.

View file

@ -189,6 +189,13 @@ public class NodeItem<T> implements Node<T> {
return new NodeItem<>(child, this); return new NodeItem<>(child, this);
} }
@Override
public Node<T> createChild(final T child, final String name) {
Node<T> node = createChild(child);
node.setName(name);
return node;
}
/** /**
* Populates the tree with the path of nodes, each being a child of the * Populates the tree with the path of nodes, each being a child of the
* previous node in the path. * previous node in the path.

View file

@ -888,4 +888,17 @@ public class NodeItemTest {
//then //then
assertThat(node.getData()).isEqualTo("updated"); assertThat(node.getData()).isEqualTo("updated");
} }
@Test
@SuppressWarnings("unchecked")
public void canCreateNamedChild() {
//given
node = new NodeItem<>(null);
//when
Node<String> child = node.createChild("child data", "child name");
//then
assertThat(child.getName()).isEqualTo("child name");
assertThat(child.getParent()).isSameAs(node);
assertThat(node.getChildren()).containsExactly(child);
}
} }