NodeItem: remove support for dynamic names

This commit is contained in:
Paul Campbell 2016-09-04 22:19:22 +01:00
parent 69be86ba07
commit 29a5ceca82

View file

@ -8,7 +8,6 @@ import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.Set; import java.util.Set;
import java.util.function.Function;
/** /**
* Represents a tree of nodes. * Represents a tree of nodes.
@ -23,8 +22,6 @@ class NodeItem<T> implements Node<T> {
private final Set<Node<T>> children = new HashSet<>(); private final Set<Node<T>> children = new HashSet<>();
private Function<Node<T>, String> nameSupplier;
private Node<T> parent; private Node<T> parent;
private String name; private String name;
@ -47,7 +44,6 @@ class NodeItem<T> implements Node<T> {
*/ */
NodeItem(final T data) { NodeItem(final T data) {
this.data = data; this.data = data;
this.nameSupplier = (n) -> null;
} }
/** /**
@ -74,24 +70,8 @@ class NodeItem<T> implements Node<T> {
setParent(parent); setParent(parent);
} }
private String generateName() {
return getNameSupplier().apply(this);
}
private Function<Node<T>, String> getNameSupplier() {
if (nameSupplier != null) {
return nameSupplier;
}
// no test for parent as root nodes will always have a default name
// supplier
return ((NodeItem<T>) parent).getNameSupplier();
}
@Override @Override
public String getName() { public String getName() {
if (name == null) {
return generateName();
}
return name; return name;
} }
@ -380,14 +360,8 @@ class NodeItem<T> implements Node<T> {
public void removeParent() { public void removeParent() {
if (parent != null) { if (parent != null) {
Node<T> oldParent = parent; Node<T> oldParent = parent;
Function<Node<T>, String> supplier = getNameSupplier();
parent = null; parent = null;
oldParent.removeChild(this); oldParent.removeChild(this);
if (this.nameSupplier == null) {
// this is now a root node, so must provide a default name
// supplier
this.nameSupplier = supplier;
}
} }
} }