From fc55bacb049bb298ff765bd15835d62b22afd62f Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Tue, 24 May 2016 14:30:00 +0100 Subject: [PATCH] Node.setData(): add ability to change node's data after instantiation --- src/main/java/net/kemitix/node/Node.java | 7 +++++++ src/main/java/net/kemitix/node/NodeItem.java | 7 ++++++- src/test/java/net/kemitix/node/NodeItemTest.java | 10 ++++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/main/java/net/kemitix/node/Node.java b/src/main/java/net/kemitix/node/Node.java index 8094888..a01efa7 100644 --- a/src/main/java/net/kemitix/node/Node.java +++ b/src/main/java/net/kemitix/node/Node.java @@ -34,6 +34,13 @@ public interface Node { */ T getData(); + /** + * Set the data held within the node. + * + * @param data the node's data + */ + void setData(T data); + /** * Returns true if the node is empty (has no data). * diff --git a/src/main/java/net/kemitix/node/NodeItem.java b/src/main/java/net/kemitix/node/NodeItem.java index 9175667..5938e71 100644 --- a/src/main/java/net/kemitix/node/NodeItem.java +++ b/src/main/java/net/kemitix/node/NodeItem.java @@ -16,7 +16,7 @@ import java.util.function.Function; */ public class NodeItem implements Node { - private final T data; + private T data; private final Set> children = new HashSet<>(); @@ -127,6 +127,11 @@ public class NodeItem implements Node { return data; } + @Override + public void setData(final T data) { + this.data = data; + } + @Override public boolean isEmpty() { return data == null; diff --git a/src/test/java/net/kemitix/node/NodeItemTest.java b/src/test/java/net/kemitix/node/NodeItemTest.java index 771c207..8cd5ca4 100644 --- a/src/test/java/net/kemitix/node/NodeItemTest.java +++ b/src/test/java/net/kemitix/node/NodeItemTest.java @@ -878,4 +878,14 @@ public class NodeItemTest { "[ lucy]"); assertThat(lines).containsSubsequence("[root]", "[ bob]"); } + + @Test + public void canChangeNodeData() { + //given + node = new NodeItem<>("initial"); + //when + node.setData("updated"); + //then + assertThat(node.getData()).isEqualTo("updated"); + } }