From aaab7bbe67beefe24736c909fe71768fbac7c917 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Tue, 24 May 2016 11:55:49 +0100 Subject: [PATCH] Reorganise code --- src/main/java/net/kemitix/node/Node.java | 14 +- src/main/java/net/kemitix/node/NodeItem.java | 192 +++++++++---------- 2 files changed, 103 insertions(+), 103 deletions(-) diff --git a/src/main/java/net/kemitix/node/Node.java b/src/main/java/net/kemitix/node/Node.java index 5ce9d56..a8dccdf 100644 --- a/src/main/java/net/kemitix/node/Node.java +++ b/src/main/java/net/kemitix/node/Node.java @@ -51,6 +51,13 @@ public interface Node { */ Node getParent(); + /** + * Make the current node a direct child of the parent. + * + * @param parent the new parent node + */ + void setParent(final Node parent); + /** * Fetches the child nodes. * @@ -110,13 +117,6 @@ public interface Node { */ boolean isChildOf(final Node node); - /** - * Make the current node a direct child of the parent. - * - * @param parent the new parent node - */ - void setParent(final Node parent); - /** * Walks the node tree using the path to select each child. * diff --git a/src/main/java/net/kemitix/node/NodeItem.java b/src/main/java/net/kemitix/node/NodeItem.java index bc87179..a756483 100644 --- a/src/main/java/net/kemitix/node/NodeItem.java +++ b/src/main/java/net/kemitix/node/NodeItem.java @@ -26,16 +26,6 @@ public class NodeItem implements Node { 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. * @@ -47,6 +37,16 @@ public class NodeItem implements Node { 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. * @@ -142,26 +142,6 @@ public class NodeItem implements Node { 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 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. * @@ -190,40 +170,18 @@ public class NodeItem implements Node { } /** - * 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 - public boolean isChildOf(final Node node) { - return parent != null && (node.equals(parent) || parent.isChildOf( - node)); - } - - /** - * 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> walkTree(final List path) { - if (path == null) { - throw new NullPointerException("path"); + public Node createChild(final T child) { + if (child == null) { + throw new NullPointerException("child"); } - if (path.size() > 0) { - Optional> 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(); + return new NodeItem<>(child, this); } /** @@ -277,57 +235,60 @@ public class NodeItem implements Node { } /** - * 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 - public Node createChild(final T child) { - if (child == null) { - throw new NullPointerException("child"); - } - return new NodeItem<>(child, this); + public boolean isChildOf(final Node node) { + return parent != null && (node.equals(parent) || parent.isChildOf( + node)); } + /** + * Make the current node a direct child of the parent. + * + * @param parent the new parent node + */ @Override - public Optional> findChildNamed(final String named) { - if (named == null) { - throw new NullPointerException("name"); + public final void setParent(final Node parent) { + if (parent == null) { + throw new NullPointerException("parent"); } - return children.stream() - .filter((Node t) -> t.getName().equals(named)) - .findAny(); + 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); } + /** + * 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 Node getChildNamed(final String named) { - final Optional> optional = findChildNamed(named); - if (optional.isPresent()) { - return optional.get(); + public Optional> walkTree(final List path) { + if (path == null) { + throw new NullPointerException("path"); } - throw new NodeException("Named child not found"); - } - - @Override - public void removeChild(final Node 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; + if (path.size() > 0) { + Optional> 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(); } @Override @@ -370,6 +331,25 @@ public class NodeItem implements Node { child.placeNodeIn(nodeItem, Arrays.copyOfRange(path, 1, path.length)); } + @Override + public Optional> findChildNamed(final String named) { + if (named == null) { + throw new NullPointerException("name"); + } + return children.stream() + .filter((Node t) -> t.getName().equals(named)) + .findAny(); + } + + @Override + public Node getChildNamed(final String named) { + final Optional> optional = findChildNamed(named); + if (optional.isPresent()) { + return optional.get(); + } + throw new NodeException("Named child not found"); + } + @Override public String drawTree(final int depth) { final StringBuilder sb = new StringBuilder(); @@ -390,4 +370,24 @@ public class NodeItem implements Node { return name != null && name.length() > 0; } + @Override + public void removeChild(final Node 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; + } + } + } + }