Reorganise code
This commit is contained in:
parent
180f325f79
commit
aaab7bbe67
2 changed files with 103 additions and 103 deletions
|
@ -51,6 +51,13 @@ public interface Node<T> {
|
||||||
*/
|
*/
|
||||||
Node<T> getParent();
|
Node<T> getParent();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Make the current node a direct child of the parent.
|
||||||
|
*
|
||||||
|
* @param parent the new parent node
|
||||||
|
*/
|
||||||
|
void setParent(final Node<T> parent);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Fetches the child nodes.
|
* Fetches the child nodes.
|
||||||
*
|
*
|
||||||
|
@ -110,13 +117,6 @@ public interface Node<T> {
|
||||||
*/
|
*/
|
||||||
boolean isChildOf(final Node<T> node);
|
boolean isChildOf(final Node<T> node);
|
||||||
|
|
||||||
/**
|
|
||||||
* Make the current node a direct child of the parent.
|
|
||||||
*
|
|
||||||
* @param parent the new parent node
|
|
||||||
*/
|
|
||||||
void setParent(final Node<T> parent);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Walks the node tree using the path to select each child.
|
* Walks the node tree using the path to select each child.
|
||||||
*
|
*
|
||||||
|
|
|
@ -26,16 +26,6 @@ public class NodeItem<T> implements Node<T> {
|
||||||
|
|
||||||
private String name;
|
private String name;
|
||||||
|
|
||||||
/**
|
|
||||||
* Create unnamed root node.
|
|
||||||
*
|
|
||||||
* @param data the data or null
|
|
||||||
*/
|
|
||||||
public NodeItem(final T data) {
|
|
||||||
this.data = data;
|
|
||||||
this.nameSupplier = (n) -> null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create named root node.
|
* Create named root node.
|
||||||
*
|
*
|
||||||
|
@ -47,6 +37,16 @@ public class NodeItem<T> implements Node<T> {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create unnamed root node.
|
||||||
|
*
|
||||||
|
* @param data the data or null
|
||||||
|
*/
|
||||||
|
public NodeItem(final T data) {
|
||||||
|
this.data = data;
|
||||||
|
this.nameSupplier = (n) -> null;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates root node with a name supplier.
|
* Creates root node with a name supplier.
|
||||||
*
|
*
|
||||||
|
@ -142,26 +142,6 @@ public class NodeItem<T> implements Node<T> {
|
||||||
return children;
|
return children;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Make the current node a direct child of the parent.
|
|
||||||
*
|
|
||||||
* @param parent the new parent node
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public final void setParent(final Node<T> parent) {
|
|
||||||
if (parent == null) {
|
|
||||||
throw new NullPointerException("parent");
|
|
||||||
}
|
|
||||||
if (this.equals(parent) || parent.isChildOf(this)) {
|
|
||||||
throw new NodeException("Parent is a descendant");
|
|
||||||
}
|
|
||||||
if (this.parent != null) {
|
|
||||||
this.parent.getChildren().remove(this);
|
|
||||||
}
|
|
||||||
this.parent = parent;
|
|
||||||
parent.addChild(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds the child to the node.
|
* Adds the child to the node.
|
||||||
*
|
*
|
||||||
|
@ -190,40 +170,18 @@ public class NodeItem<T> implements Node<T> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks if the node is an ancestor.
|
* Creates a new node and adds it as a child of the current node.
|
||||||
*
|
*
|
||||||
* @param node the potential ancestor
|
* @param child the child node's data
|
||||||
*
|
*
|
||||||
* @return true if the node is an ancestor
|
* @return the new child node
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public boolean isChildOf(final Node<T> node) {
|
public Node<T> createChild(final T child) {
|
||||||
return parent != null && (node.equals(parent) || parent.isChildOf(
|
if (child == null) {
|
||||||
node));
|
throw new NullPointerException("child");
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Walks the node tree using the path to select each child.
|
|
||||||
*
|
|
||||||
* @param path the path to the desired child
|
|
||||||
*
|
|
||||||
* @return the child or null
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public Optional<Node<T>> walkTree(final List<T> path) {
|
|
||||||
if (path == null) {
|
|
||||||
throw new NullPointerException("path");
|
|
||||||
}
|
}
|
||||||
if (path.size() > 0) {
|
return new NodeItem<>(child, this);
|
||||||
Optional<Node<T>> found = getChild(path.get(0));
|
|
||||||
if (found.isPresent()) {
|
|
||||||
if (path.size() > 1) {
|
|
||||||
return found.get().walkTree(path.subList(1, path.size()));
|
|
||||||
}
|
|
||||||
return found;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return Optional.empty();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -277,57 +235,60 @@ public class NodeItem<T> implements Node<T> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new node and adds it as a child of the current node.
|
* Checks if the node is an ancestor.
|
||||||
*
|
*
|
||||||
* @param child the child node's data
|
* @param node the potential ancestor
|
||||||
*
|
*
|
||||||
* @return the new child node
|
* @return true if the node is an ancestor
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public Node<T> createChild(final T child) {
|
public boolean isChildOf(final Node<T> node) {
|
||||||
if (child == null) {
|
return parent != null && (node.equals(parent) || parent.isChildOf(
|
||||||
throw new NullPointerException("child");
|
node));
|
||||||
}
|
|
||||||
return new NodeItem<>(child, this);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Make the current node a direct child of the parent.
|
||||||
|
*
|
||||||
|
* @param parent the new parent node
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public Optional<Node<T>> findChildNamed(final String named) {
|
public final void setParent(final Node<T> parent) {
|
||||||
if (named == null) {
|
if (parent == null) {
|
||||||
throw new NullPointerException("name");
|
throw new NullPointerException("parent");
|
||||||
}
|
}
|
||||||
return children.stream()
|
if (this.equals(parent) || parent.isChildOf(this)) {
|
||||||
.filter((Node<T> t) -> t.getName().equals(named))
|
throw new NodeException("Parent is a descendant");
|
||||||
.findAny();
|
}
|
||||||
|
if (this.parent != null) {
|
||||||
|
this.parent.getChildren().remove(this);
|
||||||
|
}
|
||||||
|
this.parent = parent;
|
||||||
|
parent.addChild(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Walks the node tree using the path to select each child.
|
||||||
|
*
|
||||||
|
* @param path the path to the desired child
|
||||||
|
*
|
||||||
|
* @return the child or null
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public Node<T> getChildNamed(final String named) {
|
public Optional<Node<T>> walkTree(final List<T> path) {
|
||||||
final Optional<Node<T>> optional = findChildNamed(named);
|
if (path == null) {
|
||||||
if (optional.isPresent()) {
|
throw new NullPointerException("path");
|
||||||
return optional.get();
|
|
||||||
}
|
}
|
||||||
throw new NodeException("Named child not found");
|
if (path.size() > 0) {
|
||||||
}
|
Optional<Node<T>> found = getChild(path.get(0));
|
||||||
|
if (found.isPresent()) {
|
||||||
@Override
|
if (path.size() > 1) {
|
||||||
public void removeChild(final Node<T> node) {
|
return found.get().walkTree(path.subList(1, path.size()));
|
||||||
if (children.remove(node)) {
|
}
|
||||||
node.removeParent();
|
return found;
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void removeParent() {
|
|
||||||
if (parent != null) {
|
|
||||||
parent.removeChild(this);
|
|
||||||
parent = null;
|
|
||||||
if (nameSupplier == null) {
|
|
||||||
// this is now a root node, so must provide a default name
|
|
||||||
// supplier
|
|
||||||
nameSupplier = n -> null;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return Optional.empty();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -370,6 +331,25 @@ public class NodeItem<T> implements Node<T> {
|
||||||
child.placeNodeIn(nodeItem, Arrays.copyOfRange(path, 1, path.length));
|
child.placeNodeIn(nodeItem, Arrays.copyOfRange(path, 1, path.length));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Optional<Node<T>> findChildNamed(final String named) {
|
||||||
|
if (named == null) {
|
||||||
|
throw new NullPointerException("name");
|
||||||
|
}
|
||||||
|
return children.stream()
|
||||||
|
.filter((Node<T> t) -> t.getName().equals(named))
|
||||||
|
.findAny();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Node<T> getChildNamed(final String named) {
|
||||||
|
final Optional<Node<T>> optional = findChildNamed(named);
|
||||||
|
if (optional.isPresent()) {
|
||||||
|
return optional.get();
|
||||||
|
}
|
||||||
|
throw new NodeException("Named child not found");
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String drawTree(final int depth) {
|
public String drawTree(final int depth) {
|
||||||
final StringBuilder sb = new StringBuilder();
|
final StringBuilder sb = new StringBuilder();
|
||||||
|
@ -390,4 +370,24 @@ public class NodeItem<T> implements Node<T> {
|
||||||
return name != null && name.length() > 0;
|
return name != null && name.length() > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void removeChild(final Node<T> node) {
|
||||||
|
if (children.remove(node)) {
|
||||||
|
node.removeParent();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void removeParent() {
|
||||||
|
if (parent != null) {
|
||||||
|
parent.removeChild(this);
|
||||||
|
parent = null;
|
||||||
|
if (nameSupplier == null) {
|
||||||
|
// this is now a root node, so must provide a default name
|
||||||
|
// supplier
|
||||||
|
nameSupplier = n -> null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue