From 2ed0024bc0e7ab7d01f3ed11f165f9decb709d73 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Tue, 24 May 2016 14:25:55 +0100 Subject: [PATCH] Node: clean up method names Deprecate: * findOrCreateChild() Rename: * getChild() => findChild() * isChildOf() => isDescendantOf() * walkTree() => findInPath() * placeNodeIn() => insertInPath() * findChildNamed() => findChildByName() * getChildNamed() => getChildByName() --- src/main/java/net/kemitix/node/Node.java | 16 ++--- src/main/java/net/kemitix/node/NodeItem.java | 34 +++++----- .../java/net/kemitix/node/NodeItemTest.java | 62 +++++++++---------- 3 files changed, 57 insertions(+), 55 deletions(-) diff --git a/src/main/java/net/kemitix/node/Node.java b/src/main/java/net/kemitix/node/Node.java index 9c432dc..8094888 100644 --- a/src/main/java/net/kemitix/node/Node.java +++ b/src/main/java/net/kemitix/node/Node.java @@ -73,7 +73,7 @@ public interface Node { void addChild(Node child); /** - * Creates a new node and adds it as a child of the current node. + * Creates a new unnamed node and adds it as a child of the current node. * * @param child the child node's data * @@ -97,6 +97,7 @@ public interface Node { * * @return the found or created child node */ + @Deprecated Node findOrCreateChild(T child); /** @@ -106,7 +107,8 @@ public interface Node { * * @return an {@link Optional} containing the child node if found */ - Optional> getChild(T child); + Optional> findChild(T child); + /** * Checks if the node is an ancestor. @@ -115,7 +117,7 @@ public interface Node { * * @return true if the node is an ancestor */ - boolean isChildOf(Node node); + boolean isDescendantOf(Node node); /** * Walks the node tree using the path to select each child. @@ -124,7 +126,7 @@ public interface Node { * * @return the child or null */ - Optional> walkTree(List path); + Optional> findInPath(List path); /** * Places the node in the tree under by the path. Intervening empty @@ -133,7 +135,7 @@ public interface Node { * @param node the node to place * @param path the path to contain the new node */ - void placeNodeIn(Node node, String... path); + void insertInPath(Node node, String... path); /** * Searches for a child with the name given. @@ -142,7 +144,7 @@ public interface Node { * * @return an Optional containing the child found or empty */ - Optional> findChildNamed(String name); + Optional> findChildByName(String name); /** * Returns the child with the given name. If one can't be found a @@ -152,7 +154,7 @@ public interface Node { * * @return the node */ - Node getChildNamed(String name); + Node getChildByName(String name); /** * Draw a representation of the tree. diff --git a/src/main/java/net/kemitix/node/NodeItem.java b/src/main/java/net/kemitix/node/NodeItem.java index 1ef3bf6..9175667 100644 --- a/src/main/java/net/kemitix/node/NodeItem.java +++ b/src/main/java/net/kemitix/node/NodeItem.java @@ -152,11 +152,11 @@ public class NodeItem implements Node { if (child == null) { throw new NullPointerException("child"); } - if (this.equals(child) || isChildOf(child)) { + if (this.equals(child) || isDescendantOf(child)) { throw new NodeException("Child is an ancestor"); } if (child.isNamed()) { - final Optional> existingChild = findChildNamed( + final Optional> existingChild = findChildByName( child.getName()); if (existingChild.isPresent() && existingChild.get() != child) { throw new NodeException( @@ -214,7 +214,7 @@ public class NodeItem implements Node { if (child == null) { throw new NullPointerException("child"); } - return getChild(child).orElseGet(() -> createChild(child)); + return findChild(child).orElseGet(() -> createChild(child)); } /** @@ -225,7 +225,7 @@ public class NodeItem implements Node { * @return an {@link Optional} containing the child node if found */ @Override - public Optional> getChild(final T child) { + public Optional> findChild(final T child) { if (child == null) { throw new NullPointerException("child"); } @@ -242,8 +242,8 @@ public class NodeItem implements Node { * @return true if the node is an ancestor */ @Override - public boolean isChildOf(final Node node) { - return parent != null && (node.equals(parent) || parent.isChildOf( + public boolean isDescendantOf(final Node node) { + return parent != null && (node.equals(parent) || parent.isDescendantOf( node)); } @@ -257,7 +257,7 @@ public class NodeItem implements Node { if (parent == null) { throw new NullPointerException("parent"); } - if (this.equals(parent) || parent.isChildOf(this)) { + if (this.equals(parent) || parent.isDescendantOf(this)) { throw new NodeException("Parent is a descendant"); } if (this.parent != null) { @@ -275,15 +275,15 @@ public class NodeItem implements Node { * @return the child or null */ @Override - public Optional> walkTree(final List path) { + public Optional> findInPath(final List path) { if (path == null) { throw new NullPointerException("path"); } if (path.size() > 0) { - Optional> found = getChild(path.get(0)); + Optional> found = findChild(path.get(0)); if (found.isPresent()) { if (path.size() > 1) { - return found.get().walkTree(path.subList(1, path.size())); + return found.get().findInPath(path.subList(1, path.size())); } return found; } @@ -292,13 +292,13 @@ public class NodeItem implements Node { } @Override - public void placeNodeIn(final Node nodeItem, final String... path) { + public void insertInPath(final Node nodeItem, final String... path) { if (path.length == 0) { if (!nodeItem.isNamed()) { // nothing to conflict with addChild(nodeItem); return; } - final Optional> childNamed = findChildNamed( + final Optional> childNamed = findChildByName( nodeItem.getName()); if (!childNamed.isPresent()) { // nothing with the same name exists addChild(nodeItem); @@ -317,18 +317,18 @@ public class NodeItem implements Node { return; } String item = path[0]; - final Optional> childNamed = findChildNamed(item); + final Optional> childNamed = findChildByName(item); Node child; if (!childNamed.isPresent()) { child = new NodeItem<>(null, item, this); } else { child = childNamed.get(); } - child.placeNodeIn(nodeItem, Arrays.copyOfRange(path, 1, path.length)); + child.insertInPath(nodeItem, Arrays.copyOfRange(path, 1, path.length)); } @Override - public Optional> findChildNamed(final String named) { + public Optional> findChildByName(final String named) { if (named == null) { throw new NullPointerException("name"); } @@ -338,8 +338,8 @@ public class NodeItem implements Node { } @Override - public Node getChildNamed(final String named) { - final Optional> optional = findChildNamed(named); + public Node getChildByName(final String named) { + final Optional> optional = findChildByName(named); if (optional.isPresent()) { return optional.get(); } diff --git a/src/test/java/net/kemitix/node/NodeItemTest.java b/src/test/java/net/kemitix/node/NodeItemTest.java index 200321f..771c207 100644 --- a/src/test/java/net/kemitix/node/NodeItemTest.java +++ b/src/test/java/net/kemitix/node/NodeItemTest.java @@ -186,7 +186,7 @@ public class NodeItemTest { assertThat(child.getParent()).as( "when a node is assigned a new parent, the old parent is " + "replaced").isSameAs(newParent); - assertThat(node.getChild("child").isPresent()).as( + assertThat(node.findChild("child").isPresent()).as( "when a node is assigned a new parent, the old parent no " + "longer has the node among it's children").isFalse(); } @@ -208,7 +208,7 @@ public class NodeItemTest { "when a node with an existing parent is added as a child " + "to another node, then the old parent is replaced") .isSameAs(newParent); - assertThat(node.getChild("child").isPresent()).as( + assertThat(node.findChild("child").isPresent()).as( "when a node with an existing parent is added as a child to " + "another node, then the old parent no longer has " + "the node among it's children").isFalse(); @@ -330,7 +330,7 @@ public class NodeItemTest { val subject = "subject"; node = new NodeItem<>(subject, parentNode); //when - val result = grandParentNode.walkTree(Arrays.asList(parent, subject)); + val result = grandParentNode.findInPath(Arrays.asList(parent, subject)); //then assertThat(result.isPresent()).as( "when we walk the tree to a node it is found").isTrue(); @@ -353,7 +353,7 @@ public class NodeItemTest { val subject = "subject"; node = new NodeItem<>(subject, parentNode); //when - val result = parentNode.walkTree(Arrays.asList(subject, "no child")); + val result = parentNode.findInPath(Arrays.asList(subject, "no child")); //then assertThat(result.isPresent()).as( "when we walk the tree to a node that doesn't exists, nothing" @@ -370,7 +370,7 @@ public class NodeItemTest { exception.expect(NullPointerException.class); exception.expectMessage("path"); //when - node.walkTree(null); + node.findInPath(null); } /** @@ -382,7 +382,7 @@ public class NodeItemTest { //given node = new NodeItem<>("subject", Node::getData); //when - val result = node.walkTree(Collections.emptyList()); + val result = node.findInPath(Collections.emptyList()); //then assertThat(result).isEmpty(); } @@ -401,7 +401,7 @@ public class NodeItemTest { node.createDescendantLine( Arrays.asList(alphaData, betaData, gammaData)); //then - val alphaOptional = node.getChild(alphaData); + val alphaOptional = node.findChild(alphaData); assertThat(alphaOptional.isPresent()).as( "when creating a descendant line, the first element is found") .isTrue(); @@ -410,7 +410,7 @@ public class NodeItemTest { assertThat(alpha.getParent()).as( "when creating a descendant line, the first element has " + "the current node as its parent").isSameAs(node); - val betaOptional = alpha.getChild(betaData); + val betaOptional = alpha.findChild(betaData); assertThat(betaOptional.isPresent()).as( "when creating a descendant line, the second element is " + "found").isTrue(); @@ -420,7 +420,7 @@ public class NodeItemTest { "when creating a descendant line, the second element " + "has the first as its parent") .isSameAs(alpha); - val gammaOptional = beta.getChild(gammaData); + val gammaOptional = beta.findChild(gammaData); assertThat(gammaOptional.isPresent()).as( "when creating a descendant line, the third element " + "is found").isTrue(); @@ -521,7 +521,7 @@ public class NodeItemTest { val child = new NodeItem(childData, Node::getData); node.addChild(child); //when - val found = node.getChild(childData); + val found = node.findChild(childData); //then assertThat(found.isPresent()).as( "when retrieving a child by its data, it is found").isTrue(); @@ -542,7 +542,7 @@ public class NodeItemTest { exception.expect(NullPointerException.class); exception.expectMessage("child"); //when - node.getChild(null); + node.findChild(null); } /** @@ -560,7 +560,7 @@ public class NodeItemTest { assertThat(child.getParent()).as( "when creating a child node, the child has the current node " + "as its parent").isSameAs(node); - val foundChild = node.getChild(childData); + val foundChild = node.findChild(childData); assertThat(foundChild.isPresent()).as( "when creating a child node, the child can be found by its " + "data").isTrue(); @@ -651,7 +651,7 @@ public class NodeItemTest { node.addChild(alpha); node.addChild(beta); //when - val result = node.getChildNamed("alpha"); + val result = node.getChildByName("alpha"); //then assertThat(result).isSameAs(alpha); } @@ -667,7 +667,7 @@ public class NodeItemTest { exception.expect(NodeException.class); exception.expectMessage("Named child not found"); //when - node.getChildNamed("gamma"); + node.getChildByName("gamma"); } @Test @@ -689,7 +689,7 @@ public class NodeItemTest { node = new NodeItem<>(null, "root"); // create a root val four = new NodeItem("data", "four"); //when - node.placeNodeIn(four, "one", "two", "three"); + node.insertInPath(four, "one", "two", "three"); //then val three = four.getParent(); assertThat(four.getParent()).as("add node to a tree").isNotNull(); @@ -699,10 +699,10 @@ public class NodeItemTest { val one = two.getParent(); assertThat(one.getName()).isEqualTo("one"); assertThat(one.getParent()).isSameAs(node); - assertThat(node.getChildNamed("one") - .getChildNamed("two") - .getChildNamed("three") - .getChildNamed("four")).isSameAs(four); + assertThat(node.getChildByName("one") + .getChildByName("two") + .getChildByName("three") + .getChildByName("four")).isSameAs(four); } @Test @@ -713,11 +713,11 @@ public class NodeItemTest { val child = new NodeItem("child data", "child"); val grandchild = new NodeItem("grandchild data", "grandchild"); //when - node.placeNodeIn(child); // as root/child - node.placeNodeIn(grandchild, "child"); // as root/child/grandchild + node.insertInPath(child); // as root/child + node.insertInPath(grandchild, "child"); // as root/child/grandchild //then - assertThat(node.getChildNamed("child")).as("child").isSameAs(child); - assertThat(node.getChildNamed("child").getChildNamed("grandchild")).as( + assertThat(node.getChildByName("child")).as("child").isSameAs(child); + assertThat(node.getChildByName("child").getChildByName("grandchild")).as( "grandchild").isSameAs(grandchild); } @@ -729,11 +729,11 @@ public class NodeItemTest { val child = new NodeItem("child data", "child"); val grandchild = new NodeItem("grandchild data", "grandchild"); //when - node.placeNodeIn(grandchild, "child"); - node.placeNodeIn(child); + node.insertInPath(grandchild, "child"); + node.insertInPath(child); //then - assertThat(node.getChildNamed("child")).as("child").isSameAs(child); - assertThat(node.getChildNamed("child").getChildNamed("grandchild")).as( + assertThat(node.getChildByName("child")).as("child").isSameAs(child); + assertThat(node.getChildByName("child").getChildByName("grandchild")).as( "grandchild").isSameAs(grandchild); } @@ -758,7 +758,7 @@ public class NodeItemTest { // only grandchild has data //when // attempt to add another node called 'grandchild' to 'child' - node.placeNodeIn(new NodeItem<>("cuckoo", "grandchild"), "child"); + node.insertInPath(new NodeItem<>("cuckoo", "grandchild"), "child"); } @Test @@ -768,7 +768,7 @@ public class NodeItemTest { node = new NodeItem<>(null); final Node newNode = new NodeItem<>(null); //when - node.placeNodeIn(newNode); + node.insertInPath(newNode); //then assertThat(node.getChildren()).containsOnly(newNode); } @@ -786,7 +786,7 @@ public class NodeItemTest { assertThat(addMe.getParent()).isNull(); //when // addMe should replace target as the sole descendant of child - node.placeNodeIn(addMe, "child"); + node.insertInPath(addMe, "child"); //then assertThat(child.getChildren()).as("child only contains new node") .containsOnly(addMe); @@ -801,7 +801,7 @@ public class NodeItemTest { exception.expectMessage("name"); node = new NodeItem<>(null); //when - node.findChildNamed(null); + node.findChildByName(null); } @Test