From 7de6c5ad4980001b0e39bb2f465b80563bf8461e Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Tue, 24 May 2016 22:03:14 +0100 Subject: [PATCH 1/2] Node.getName(): generated each time called when using a name supplier Previously the name was set when the node was created and only a call to setName() would have changed it. isNamed() uses the generated name, so this may change if the generated name is even null or the empty string. --- src/main/java/net/kemitix/node/Node.java | 6 +++-- src/main/java/net/kemitix/node/NodeItem.java | 8 +++--- .../java/net/kemitix/node/NodeItemTest.java | 26 +++++++++++++++++++ 3 files changed, 35 insertions(+), 5 deletions(-) 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(); + } } From 96ac0f720f62f60007681b801bffbacd338ccd27 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Tue, 24 May 2016 22:04:00 +0100 Subject: [PATCH 2/2] Node.setName(): ensure that setting name to null enables the name supplier --- src/main/java/net/kemitix/node/Node.java | 3 ++- src/test/java/net/kemitix/node/NodeItemTest.java | 12 ++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/main/java/net/kemitix/node/Node.java b/src/main/java/net/kemitix/node/Node.java index cba5129..9d82efd 100644 --- a/src/main/java/net/kemitix/node/Node.java +++ b/src/main/java/net/kemitix/node/Node.java @@ -22,7 +22,8 @@ public interface Node { String getName(); /** - * Sets the explicit name for a node. + * Sets the explicit name for a node. Setting the name to null will clear + * the name and revert to the parent's name supplier. * * @param name the new name */ diff --git a/src/test/java/net/kemitix/node/NodeItemTest.java b/src/test/java/net/kemitix/node/NodeItemTest.java index 866ac24..dde300a 100644 --- a/src/test/java/net/kemitix/node/NodeItemTest.java +++ b/src/test/java/net/kemitix/node/NodeItemTest.java @@ -975,6 +975,18 @@ public class NodeItemTest { assertThat(child.getName()).isEqualTo("local supplier"); } + @Test + public void setNameToNullRevertsToParentNameSupplier() { + //given + node = new NodeItem<>(null, n -> "root supplier"); + val child = new NodeItem(null, "child name", node); + assertThat(child.getName()).isEqualTo("child name"); + //when + child.setName(null); + //then + assertThat(child.getName()).isEqualTo("root supplier"); + } + @Test public void getNameWithNameSupplierIsRecalculatedEachCall() { val counter = new AtomicInteger(0);