commit
94f7b5fb95
3 changed files with 49 additions and 6 deletions
|
@ -14,14 +14,16 @@ import java.util.Set;
|
|||
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
|
||||
*/
|
||||
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
|
||||
*/
|
||||
|
@ -193,7 +195,8 @@ public interface Node<T> {
|
|||
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
|
||||
*/
|
||||
|
|
|
@ -57,7 +57,6 @@ public class NodeItem<T> implements Node<T> {
|
|||
final T data, final Function<Node<T>, String> nameSupplier) {
|
||||
this(data);
|
||||
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) {
|
||||
this.data = data;
|
||||
setParent(parent);
|
||||
this.name = generateName();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -114,6 +112,9 @@ public class NodeItem<T> implements Node<T> {
|
|||
|
||||
@Override
|
||||
public String getName() {
|
||||
if (name == null) {
|
||||
return generateName();
|
||||
}
|
||||
return name;
|
||||
}
|
||||
|
||||
|
@ -386,7 +387,8 @@ public class NodeItem<T> implements Node<T> {
|
|||
|
||||
@Override
|
||||
public boolean isNamed() {
|
||||
return name != null && name.length() > 0;
|
||||
String currentName = getName();
|
||||
return currentName != null && currentName.length() > 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -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,41 @@ public class NodeItemTest {
|
|||
//then
|
||||
assertThat(child.getName()).isEqualTo("local supplier");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setNameToNullRevertsToParentNameSupplier() {
|
||||
//given
|
||||
node = new NodeItem<>(null, n -> "root supplier");
|
||||
val child = new NodeItem<String>(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);
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue