Node: clean up method names

Deprecate:
* findOrCreateChild()

Rename:
* getChild() => findChild()
* isChildOf() => isDescendantOf()
* walkTree() => findInPath()
* placeNodeIn() => insertInPath()
* findChildNamed() => findChildByName()
* getChildNamed() => getChildByName()
This commit is contained in:
Paul Campbell 2016-05-24 14:25:55 +01:00
parent e9c6300ad4
commit 2ed0024bc0
3 changed files with 57 additions and 55 deletions

View file

@ -73,7 +73,7 @@ public interface Node<T> {
void addChild(Node<T> child); void addChild(Node<T> 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 * @param child the child node's data
* *
@ -97,6 +97,7 @@ public interface Node<T> {
* *
* @return the found or created child node * @return the found or created child node
*/ */
@Deprecated
Node<T> findOrCreateChild(T child); Node<T> findOrCreateChild(T child);
/** /**
@ -106,7 +107,8 @@ public interface Node<T> {
* *
* @return an {@link Optional} containing the child node if found * @return an {@link Optional} containing the child node if found
*/ */
Optional<Node<T>> getChild(T child); Optional<Node<T>> findChild(T child);
/** /**
* Checks if the node is an ancestor. * Checks if the node is an ancestor.
@ -115,7 +117,7 @@ public interface Node<T> {
* *
* @return true if the node is an ancestor * @return true if the node is an ancestor
*/ */
boolean isChildOf(Node<T> node); boolean isDescendantOf(Node<T> node);
/** /**
* Walks the node tree using the path to select each child. * Walks the node tree using the path to select each child.
@ -124,7 +126,7 @@ public interface Node<T> {
* *
* @return the child or null * @return the child or null
*/ */
Optional<Node<T>> walkTree(List<T> path); Optional<Node<T>> findInPath(List<T> path);
/** /**
* Places the node in the tree under by the path. Intervening empty * Places the node in the tree under by the path. Intervening empty
@ -133,7 +135,7 @@ public interface Node<T> {
* @param node the node to place * @param node the node to place
* @param path the path to contain the new node * @param path the path to contain the new node
*/ */
void placeNodeIn(Node<T> node, String... path); void insertInPath(Node<T> node, String... path);
/** /**
* Searches for a child with the name given. * Searches for a child with the name given.
@ -142,7 +144,7 @@ public interface Node<T> {
* *
* @return an Optional containing the child found or empty * @return an Optional containing the child found or empty
*/ */
Optional<Node<T>> findChildNamed(String name); Optional<Node<T>> findChildByName(String name);
/** /**
* Returns the child with the given name. If one can't be found a * Returns the child with the given name. If one can't be found a
@ -152,7 +154,7 @@ public interface Node<T> {
* *
* @return the node * @return the node
*/ */
Node<T> getChildNamed(String name); Node<T> getChildByName(String name);
/** /**
* Draw a representation of the tree. * Draw a representation of the tree.

View file

@ -152,11 +152,11 @@ public class NodeItem<T> implements Node<T> {
if (child == null) { if (child == null) {
throw new NullPointerException("child"); throw new NullPointerException("child");
} }
if (this.equals(child) || isChildOf(child)) { if (this.equals(child) || isDescendantOf(child)) {
throw new NodeException("Child is an ancestor"); throw new NodeException("Child is an ancestor");
} }
if (child.isNamed()) { if (child.isNamed()) {
final Optional<Node<T>> existingChild = findChildNamed( final Optional<Node<T>> existingChild = findChildByName(
child.getName()); child.getName());
if (existingChild.isPresent() && existingChild.get() != child) { if (existingChild.isPresent() && existingChild.get() != child) {
throw new NodeException( throw new NodeException(
@ -214,7 +214,7 @@ public class NodeItem<T> implements Node<T> {
if (child == null) { if (child == null) {
throw new NullPointerException("child"); throw new NullPointerException("child");
} }
return getChild(child).orElseGet(() -> createChild(child)); return findChild(child).orElseGet(() -> createChild(child));
} }
/** /**
@ -225,7 +225,7 @@ public class NodeItem<T> implements Node<T> {
* @return an {@link Optional} containing the child node if found * @return an {@link Optional} containing the child node if found
*/ */
@Override @Override
public Optional<Node<T>> getChild(final T child) { public Optional<Node<T>> findChild(final T child) {
if (child == null) { if (child == null) {
throw new NullPointerException("child"); throw new NullPointerException("child");
} }
@ -242,8 +242,8 @@ public class NodeItem<T> implements Node<T> {
* @return true if the node is an ancestor * @return true if the node is an ancestor
*/ */
@Override @Override
public boolean isChildOf(final Node<T> node) { public boolean isDescendantOf(final Node<T> node) {
return parent != null && (node.equals(parent) || parent.isChildOf( return parent != null && (node.equals(parent) || parent.isDescendantOf(
node)); node));
} }
@ -257,7 +257,7 @@ public class NodeItem<T> implements Node<T> {
if (parent == null) { if (parent == null) {
throw new NullPointerException("parent"); 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"); throw new NodeException("Parent is a descendant");
} }
if (this.parent != null) { if (this.parent != null) {
@ -275,15 +275,15 @@ public class NodeItem<T> implements Node<T> {
* @return the child or null * @return the child or null
*/ */
@Override @Override
public Optional<Node<T>> walkTree(final List<T> path) { public Optional<Node<T>> findInPath(final List<T> path) {
if (path == null) { if (path == null) {
throw new NullPointerException("path"); throw new NullPointerException("path");
} }
if (path.size() > 0) { if (path.size() > 0) {
Optional<Node<T>> found = getChild(path.get(0)); Optional<Node<T>> found = findChild(path.get(0));
if (found.isPresent()) { if (found.isPresent()) {
if (path.size() > 1) { if (path.size() > 1) {
return found.get().walkTree(path.subList(1, path.size())); return found.get().findInPath(path.subList(1, path.size()));
} }
return found; return found;
} }
@ -292,13 +292,13 @@ public class NodeItem<T> implements Node<T> {
} }
@Override @Override
public void placeNodeIn(final Node<T> nodeItem, final String... path) { public void insertInPath(final Node<T> nodeItem, final String... path) {
if (path.length == 0) { if (path.length == 0) {
if (!nodeItem.isNamed()) { // nothing to conflict with if (!nodeItem.isNamed()) { // nothing to conflict with
addChild(nodeItem); addChild(nodeItem);
return; return;
} }
final Optional<Node<T>> childNamed = findChildNamed( final Optional<Node<T>> childNamed = findChildByName(
nodeItem.getName()); nodeItem.getName());
if (!childNamed.isPresent()) { // nothing with the same name exists if (!childNamed.isPresent()) { // nothing with the same name exists
addChild(nodeItem); addChild(nodeItem);
@ -317,18 +317,18 @@ public class NodeItem<T> implements Node<T> {
return; return;
} }
String item = path[0]; String item = path[0];
final Optional<Node<T>> childNamed = findChildNamed(item); final Optional<Node<T>> childNamed = findChildByName(item);
Node<T> child; Node<T> child;
if (!childNamed.isPresent()) { if (!childNamed.isPresent()) {
child = new NodeItem<>(null, item, this); child = new NodeItem<>(null, item, this);
} else { } else {
child = childNamed.get(); child = childNamed.get();
} }
child.placeNodeIn(nodeItem, Arrays.copyOfRange(path, 1, path.length)); child.insertInPath(nodeItem, Arrays.copyOfRange(path, 1, path.length));
} }
@Override @Override
public Optional<Node<T>> findChildNamed(final String named) { public Optional<Node<T>> findChildByName(final String named) {
if (named == null) { if (named == null) {
throw new NullPointerException("name"); throw new NullPointerException("name");
} }
@ -338,8 +338,8 @@ public class NodeItem<T> implements Node<T> {
} }
@Override @Override
public Node<T> getChildNamed(final String named) { public Node<T> getChildByName(final String named) {
final Optional<Node<T>> optional = findChildNamed(named); final Optional<Node<T>> optional = findChildByName(named);
if (optional.isPresent()) { if (optional.isPresent()) {
return optional.get(); return optional.get();
} }

View file

@ -186,7 +186,7 @@ public class NodeItemTest {
assertThat(child.getParent()).as( assertThat(child.getParent()).as(
"when a node is assigned a new parent, the old parent is " "when a node is assigned a new parent, the old parent is "
+ "replaced").isSameAs(newParent); + "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 " "when a node is assigned a new parent, the old parent no "
+ "longer has the node among it's children").isFalse(); + "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 " "when a node with an existing parent is added as a child "
+ "to another node, then the old parent is replaced") + "to another node, then the old parent is replaced")
.isSameAs(newParent); .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 " "when a node with an existing parent is added as a child to "
+ "another node, then the old parent no longer has " + "another node, then the old parent no longer has "
+ "the node among it's children").isFalse(); + "the node among it's children").isFalse();
@ -330,7 +330,7 @@ public class NodeItemTest {
val subject = "subject"; val subject = "subject";
node = new NodeItem<>(subject, parentNode); node = new NodeItem<>(subject, parentNode);
//when //when
val result = grandParentNode.walkTree(Arrays.asList(parent, subject)); val result = grandParentNode.findInPath(Arrays.asList(parent, subject));
//then //then
assertThat(result.isPresent()).as( assertThat(result.isPresent()).as(
"when we walk the tree to a node it is found").isTrue(); "when we walk the tree to a node it is found").isTrue();
@ -353,7 +353,7 @@ public class NodeItemTest {
val subject = "subject"; val subject = "subject";
node = new NodeItem<>(subject, parentNode); node = new NodeItem<>(subject, parentNode);
//when //when
val result = parentNode.walkTree(Arrays.asList(subject, "no child")); val result = parentNode.findInPath(Arrays.asList(subject, "no child"));
//then //then
assertThat(result.isPresent()).as( assertThat(result.isPresent()).as(
"when we walk the tree to a node that doesn't exists, nothing" "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.expect(NullPointerException.class);
exception.expectMessage("path"); exception.expectMessage("path");
//when //when
node.walkTree(null); node.findInPath(null);
} }
/** /**
@ -382,7 +382,7 @@ public class NodeItemTest {
//given //given
node = new NodeItem<>("subject", Node::getData); node = new NodeItem<>("subject", Node::getData);
//when //when
val result = node.walkTree(Collections.emptyList()); val result = node.findInPath(Collections.emptyList());
//then //then
assertThat(result).isEmpty(); assertThat(result).isEmpty();
} }
@ -401,7 +401,7 @@ public class NodeItemTest {
node.createDescendantLine( node.createDescendantLine(
Arrays.asList(alphaData, betaData, gammaData)); Arrays.asList(alphaData, betaData, gammaData));
//then //then
val alphaOptional = node.getChild(alphaData); val alphaOptional = node.findChild(alphaData);
assertThat(alphaOptional.isPresent()).as( assertThat(alphaOptional.isPresent()).as(
"when creating a descendant line, the first element is found") "when creating a descendant line, the first element is found")
.isTrue(); .isTrue();
@ -410,7 +410,7 @@ public class NodeItemTest {
assertThat(alpha.getParent()).as( assertThat(alpha.getParent()).as(
"when creating a descendant line, the first element has " "when creating a descendant line, the first element has "
+ "the current node as its parent").isSameAs(node); + "the current node as its parent").isSameAs(node);
val betaOptional = alpha.getChild(betaData); val betaOptional = alpha.findChild(betaData);
assertThat(betaOptional.isPresent()).as( assertThat(betaOptional.isPresent()).as(
"when creating a descendant line, the second element is " "when creating a descendant line, the second element is "
+ "found").isTrue(); + "found").isTrue();
@ -420,7 +420,7 @@ public class NodeItemTest {
"when creating a descendant line, the second element " "when creating a descendant line, the second element "
+ "has the first as its parent") + "has the first as its parent")
.isSameAs(alpha); .isSameAs(alpha);
val gammaOptional = beta.getChild(gammaData); val gammaOptional = beta.findChild(gammaData);
assertThat(gammaOptional.isPresent()).as( assertThat(gammaOptional.isPresent()).as(
"when creating a descendant line, the third element " "when creating a descendant line, the third element "
+ "is found").isTrue(); + "is found").isTrue();
@ -521,7 +521,7 @@ public class NodeItemTest {
val child = new NodeItem<String>(childData, Node::getData); val child = new NodeItem<String>(childData, Node::getData);
node.addChild(child); node.addChild(child);
//when //when
val found = node.getChild(childData); val found = node.findChild(childData);
//then //then
assertThat(found.isPresent()).as( assertThat(found.isPresent()).as(
"when retrieving a child by its data, it is found").isTrue(); "when retrieving a child by its data, it is found").isTrue();
@ -542,7 +542,7 @@ public class NodeItemTest {
exception.expect(NullPointerException.class); exception.expect(NullPointerException.class);
exception.expectMessage("child"); exception.expectMessage("child");
//when //when
node.getChild(null); node.findChild(null);
} }
/** /**
@ -560,7 +560,7 @@ public class NodeItemTest {
assertThat(child.getParent()).as( assertThat(child.getParent()).as(
"when creating a child node, the child has the current node " "when creating a child node, the child has the current node "
+ "as its parent").isSameAs(node); + "as its parent").isSameAs(node);
val foundChild = node.getChild(childData); val foundChild = node.findChild(childData);
assertThat(foundChild.isPresent()).as( assertThat(foundChild.isPresent()).as(
"when creating a child node, the child can be found by its " "when creating a child node, the child can be found by its "
+ "data").isTrue(); + "data").isTrue();
@ -651,7 +651,7 @@ public class NodeItemTest {
node.addChild(alpha); node.addChild(alpha);
node.addChild(beta); node.addChild(beta);
//when //when
val result = node.getChildNamed("alpha"); val result = node.getChildByName("alpha");
//then //then
assertThat(result).isSameAs(alpha); assertThat(result).isSameAs(alpha);
} }
@ -667,7 +667,7 @@ public class NodeItemTest {
exception.expect(NodeException.class); exception.expect(NodeException.class);
exception.expectMessage("Named child not found"); exception.expectMessage("Named child not found");
//when //when
node.getChildNamed("gamma"); node.getChildByName("gamma");
} }
@Test @Test
@ -689,7 +689,7 @@ public class NodeItemTest {
node = new NodeItem<>(null, "root"); // create a root node = new NodeItem<>(null, "root"); // create a root
val four = new NodeItem<String>("data", "four"); val four = new NodeItem<String>("data", "four");
//when //when
node.placeNodeIn(four, "one", "two", "three"); node.insertInPath(four, "one", "two", "three");
//then //then
val three = four.getParent(); val three = four.getParent();
assertThat(four.getParent()).as("add node to a tree").isNotNull(); assertThat(four.getParent()).as("add node to a tree").isNotNull();
@ -699,10 +699,10 @@ public class NodeItemTest {
val one = two.getParent(); val one = two.getParent();
assertThat(one.getName()).isEqualTo("one"); assertThat(one.getName()).isEqualTo("one");
assertThat(one.getParent()).isSameAs(node); assertThat(one.getParent()).isSameAs(node);
assertThat(node.getChildNamed("one") assertThat(node.getChildByName("one")
.getChildNamed("two") .getChildByName("two")
.getChildNamed("three") .getChildByName("three")
.getChildNamed("four")).isSameAs(four); .getChildByName("four")).isSameAs(four);
} }
@Test @Test
@ -713,11 +713,11 @@ public class NodeItemTest {
val child = new NodeItem<String>("child data", "child"); val child = new NodeItem<String>("child data", "child");
val grandchild = new NodeItem<String>("grandchild data", "grandchild"); val grandchild = new NodeItem<String>("grandchild data", "grandchild");
//when //when
node.placeNodeIn(child); // as root/child node.insertInPath(child); // as root/child
node.placeNodeIn(grandchild, "child"); // as root/child/grandchild node.insertInPath(grandchild, "child"); // as root/child/grandchild
//then //then
assertThat(node.getChildNamed("child")).as("child").isSameAs(child); assertThat(node.getChildByName("child")).as("child").isSameAs(child);
assertThat(node.getChildNamed("child").getChildNamed("grandchild")).as( assertThat(node.getChildByName("child").getChildByName("grandchild")).as(
"grandchild").isSameAs(grandchild); "grandchild").isSameAs(grandchild);
} }
@ -729,11 +729,11 @@ public class NodeItemTest {
val child = new NodeItem<String>("child data", "child"); val child = new NodeItem<String>("child data", "child");
val grandchild = new NodeItem<String>("grandchild data", "grandchild"); val grandchild = new NodeItem<String>("grandchild data", "grandchild");
//when //when
node.placeNodeIn(grandchild, "child"); node.insertInPath(grandchild, "child");
node.placeNodeIn(child); node.insertInPath(child);
//then //then
assertThat(node.getChildNamed("child")).as("child").isSameAs(child); assertThat(node.getChildByName("child")).as("child").isSameAs(child);
assertThat(node.getChildNamed("child").getChildNamed("grandchild")).as( assertThat(node.getChildByName("child").getChildByName("grandchild")).as(
"grandchild").isSameAs(grandchild); "grandchild").isSameAs(grandchild);
} }
@ -758,7 +758,7 @@ public class NodeItemTest {
// only grandchild has data // only grandchild has data
//when //when
// attempt to add another node called 'grandchild' to 'child' // attempt to add another node called 'grandchild' to 'child'
node.placeNodeIn(new NodeItem<>("cuckoo", "grandchild"), "child"); node.insertInPath(new NodeItem<>("cuckoo", "grandchild"), "child");
} }
@Test @Test
@ -768,7 +768,7 @@ public class NodeItemTest {
node = new NodeItem<>(null); node = new NodeItem<>(null);
final Node<String> newNode = new NodeItem<>(null); final Node<String> newNode = new NodeItem<>(null);
//when //when
node.placeNodeIn(newNode); node.insertInPath(newNode);
//then //then
assertThat(node.getChildren()).containsOnly(newNode); assertThat(node.getChildren()).containsOnly(newNode);
} }
@ -786,7 +786,7 @@ public class NodeItemTest {
assertThat(addMe.getParent()).isNull(); assertThat(addMe.getParent()).isNull();
//when //when
// addMe should replace target as the sole descendant of child // addMe should replace target as the sole descendant of child
node.placeNodeIn(addMe, "child"); node.insertInPath(addMe, "child");
//then //then
assertThat(child.getChildren()).as("child only contains new node") assertThat(child.getChildren()).as("child only contains new node")
.containsOnly(addMe); .containsOnly(addMe);
@ -801,7 +801,7 @@ public class NodeItemTest {
exception.expectMessage("name"); exception.expectMessage("name");
node = new NodeItem<>(null); node = new NodeItem<>(null);
//when //when
node.findChildNamed(null); node.findChildByName(null);
} }
@Test @Test