diff --git a/src/main/java/net/kemitix/node/Node.java b/src/main/java/net/kemitix/node/Node.java index 19cc60a..cba5129 100644 --- a/src/main/java/net/kemitix/node/Node.java +++ b/src/main/java/net/kemitix/node/Node.java @@ -14,7 +14,8 @@ import java.util.Set; public interface Node { /** - * Fetch the name of the node. + * Fetch the name of the node. Where a node's name is determined via a name + * supplier, the name may be regenerated each time this method is called. * * @return the name of the node */ @@ -193,7 +194,8 @@ public interface Node { String drawTree(int depth); /** - * Returns true if the Node has a name. + * Returns true if the Node has a name. Where a name supplier is used, the + * generated name is used. * * @return true if the node has a name */ diff --git a/src/main/java/net/kemitix/node/NodeItem.java b/src/main/java/net/kemitix/node/NodeItem.java index ddb402a..a6c7dea 100644 --- a/src/main/java/net/kemitix/node/NodeItem.java +++ b/src/main/java/net/kemitix/node/NodeItem.java @@ -57,7 +57,6 @@ public class NodeItem implements Node { final T data, final Function, String> nameSupplier) { this(data); this.nameSupplier = nameSupplier; - name = generateName(); } /** @@ -69,7 +68,6 @@ public class NodeItem implements Node { public NodeItem(final T data, final Node parent) { this.data = data; setParent(parent); - this.name = generateName(); } /** @@ -114,6 +112,9 @@ public class NodeItem implements Node { @Override public String getName() { + if (name == null) { + return generateName(); + } return name; } @@ -386,7 +387,8 @@ public class NodeItem implements Node { @Override public boolean isNamed() { - return name != null && name.length() > 0; + String currentName = getName(); + return currentName != null && currentName.length() > 0; } @Override diff --git a/src/test/java/net/kemitix/node/NodeItemTest.java b/src/test/java/net/kemitix/node/NodeItemTest.java index da5df99..866ac24 100644 --- a/src/test/java/net/kemitix/node/NodeItemTest.java +++ b/src/test/java/net/kemitix/node/NodeItemTest.java @@ -13,6 +13,7 @@ import java.time.format.DateTimeFormatter; import java.util.Arrays; import java.util.Collections; import java.util.Optional; +import java.util.concurrent.atomic.AtomicInteger; /** * Test for {@link NodeItem}. @@ -973,4 +974,29 @@ public class NodeItemTest { //then assertThat(child.getName()).isEqualTo("local supplier"); } + + @Test + public void getNameWithNameSupplierIsRecalculatedEachCall() { + val counter = new AtomicInteger(0); + node = new NodeItem<>(null, + n -> Integer.toString(counter.incrementAndGet())); + //then + assertThat(node.getName()).isNotEqualTo(node.getName()); + } + + @Test + public void isNamedWithNameSupplierIsRecalculatedEachCall() { + val counter = new AtomicInteger(0); + node = new NodeItem<>(null, n -> { + // alternate between even numbers and nulls: null, 2, null, 4, null + final int i = counter.incrementAndGet(); + if (i % 2 == 0) { + return Integer.toString(i); + } + return null; + }); + //then + assertThat(node.isNamed()).isFalse(); + assertThat(node.isNamed()).isTrue(); + } }