Node.setData(): add ability to change node's data after instantiation
This commit is contained in:
parent
2ed0024bc0
commit
fc55bacb04
3 changed files with 23 additions and 1 deletions
|
@ -34,6 +34,13 @@ public interface Node<T> {
|
||||||
*/
|
*/
|
||||||
T getData();
|
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).
|
* Returns true if the node is empty (has no data).
|
||||||
*
|
*
|
||||||
|
|
|
@ -16,7 +16,7 @@ import java.util.function.Function;
|
||||||
*/
|
*/
|
||||||
public class NodeItem<T> implements Node<T> {
|
public class NodeItem<T> implements Node<T> {
|
||||||
|
|
||||||
private final T data;
|
private T data;
|
||||||
|
|
||||||
private final Set<Node<T>> children = new HashSet<>();
|
private final Set<Node<T>> children = new HashSet<>();
|
||||||
|
|
||||||
|
@ -127,6 +127,11 @@ public class NodeItem<T> implements Node<T> {
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setData(final T data) {
|
||||||
|
this.data = data;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isEmpty() {
|
public boolean isEmpty() {
|
||||||
return data == null;
|
return data == null;
|
||||||
|
|
|
@ -878,4 +878,14 @@ public class NodeItemTest {
|
||||||
"[ lucy]");
|
"[ lucy]");
|
||||||
assertThat(lines).containsSubsequence("[root]", "[ bob]");
|
assertThat(lines).containsSubsequence("[root]", "[ bob]");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void canChangeNodeData() {
|
||||||
|
//given
|
||||||
|
node = new NodeItem<>("initial");
|
||||||
|
//when
|
||||||
|
node.setData("updated");
|
||||||
|
//then
|
||||||
|
assertThat(node.getData()).isEqualTo("updated");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue