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.
This commit is contained in:
Paul Campbell 2016-05-24 22:03:14 +01:00
parent 7ccef4e011
commit 7de6c5ad49
3 changed files with 35 additions and 5 deletions

View file

@ -14,7 +14,8 @@ import java.util.Set;
public interface Node<T> { public interface Node<T> {
/** /**
* 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 * @return the name of the node
*/ */
@ -193,7 +194,8 @@ public interface Node<T> {
String drawTree(int depth); 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 * @return true if the node has a name
*/ */

View file

@ -57,7 +57,6 @@ public class NodeItem<T> implements Node<T> {
final T data, final Function<Node<T>, String> nameSupplier) { final T data, final Function<Node<T>, String> nameSupplier) {
this(data); this(data);
this.nameSupplier = nameSupplier; this.nameSupplier = nameSupplier;
name = generateName();
} }
/** /**
@ -69,7 +68,6 @@ public class NodeItem<T> implements Node<T> {
public NodeItem(final T data, final Node<T> parent) { public NodeItem(final T data, final Node<T> parent) {
this.data = data; this.data = data;
setParent(parent); setParent(parent);
this.name = generateName();
} }
/** /**
@ -114,6 +112,9 @@ public class NodeItem<T> implements Node<T> {
@Override @Override
public String getName() { public String getName() {
if (name == null) {
return generateName();
}
return name; return name;
} }
@ -386,7 +387,8 @@ public class NodeItem<T> implements Node<T> {
@Override @Override
public boolean isNamed() { public boolean isNamed() {
return name != null && name.length() > 0; String currentName = getName();
return currentName != null && currentName.length() > 0;
} }
@Override @Override

View file

@ -13,6 +13,7 @@ import java.time.format.DateTimeFormatter;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.Optional; import java.util.Optional;
import java.util.concurrent.atomic.AtomicInteger;
/** /**
* Test for {@link NodeItem}. * Test for {@link NodeItem}.
@ -973,4 +974,29 @@ public class NodeItemTest {
//then //then
assertThat(child.getName()).isEqualTo("local supplier"); 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();
}
} }