From 76c4d4ceeba7c7e74af53cb692ade4759625f0cd Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Tue, 20 Sep 2016 23:10:33 +0100 Subject: [PATCH] Node: drop findOrCreateChild() --- .../net/kemitix/node/ImmutableNodeItem.java | 5 -- src/main/java/net/kemitix/node/Node.java | 13 ------ src/main/java/net/kemitix/node/NodeItem.java | 21 ++------- .../kemitix/node/ImmutableNodeItemTest.java | 33 ------------- .../java/net/kemitix/node/NodeItemTest.java | 46 ------------------- 5 files changed, 4 insertions(+), 114 deletions(-) diff --git a/src/main/java/net/kemitix/node/ImmutableNodeItem.java b/src/main/java/net/kemitix/node/ImmutableNodeItem.java index fc4d159..547dde3 100644 --- a/src/main/java/net/kemitix/node/ImmutableNodeItem.java +++ b/src/main/java/net/kemitix/node/ImmutableNodeItem.java @@ -118,11 +118,6 @@ final class ImmutableNodeItem extends NodeItem { throw new UnsupportedOperationException(IMMUTABLE_OBJECT); } - @Override - public Node findOrCreateChild(final T child) { - return findChild(child).orElseThrow(() -> new UnsupportedOperationException(IMMUTABLE_OBJECT)); - } - @Override public void insertInPath(final Node node, final String... path) { throw new UnsupportedOperationException(IMMUTABLE_OBJECT); diff --git a/src/main/java/net/kemitix/node/Node.java b/src/main/java/net/kemitix/node/Node.java index 029d536..e354227 100644 --- a/src/main/java/net/kemitix/node/Node.java +++ b/src/main/java/net/kemitix/node/Node.java @@ -129,19 +129,6 @@ public interface Node { */ void createDescendantLine(List descendants); - /** - * Looks for a child node and returns it, creating a new child node if one - * isn't found. - * - * @param child the child's data to search or create with - * - * @return the found or created child node - * - * @deprecated use {@code node.findChild(child).orElseGet(() -> node.createChild(child))}; - */ - @Deprecated - Node findOrCreateChild(T child); - /** * Fetches the node for the child if present. * diff --git a/src/main/java/net/kemitix/node/NodeItem.java b/src/main/java/net/kemitix/node/NodeItem.java index 9921dc2..e4d605a 100644 --- a/src/main/java/net/kemitix/node/NodeItem.java +++ b/src/main/java/net/kemitix/node/NodeItem.java @@ -199,26 +199,13 @@ class NodeItem implements Node { @Override public void createDescendantLine(@NonNull final List descendants) { if (!descendants.isEmpty()) { - findOrCreateChild(descendants.get(0)).createDescendantLine(descendants.subList(1, descendants.size())); + val child = descendants.get(0); + val remainingLine = descendants.subList(1, descendants.size()); + findChild(child).orElseGet(() -> createChild(child)) + .createDescendantLine(remainingLine); } } - /** - * Looks for a child node and returns it, creating a new child node if one - * isn't found. - * - * @param child the child's data to search or create with - * - * @return the found or created child node - * - * @deprecated use node.findChild(child).orElseGet(() -> node.createChild (child)); - */ - @Override - @Deprecated - public Node findOrCreateChild(@NonNull final T child) { - return findChild(child).orElseGet(() -> createChild(child)); - } - /** * Fetches the node for the child if present. * diff --git a/src/test/java/net/kemitix/node/ImmutableNodeItemTest.java b/src/test/java/net/kemitix/node/ImmutableNodeItemTest.java index c4c4bc7..411e9e5 100644 --- a/src/test/java/net/kemitix/node/ImmutableNodeItemTest.java +++ b/src/test/java/net/kemitix/node/ImmutableNodeItemTest.java @@ -222,19 +222,6 @@ public class ImmutableNodeItemTest { } } - /** - * Test that if we pass null we get an exception. - */ - @Test - public void findOrCreateChildShouldThrowNPEFWhenChildIsNull() { - //given - immutableNode = Nodes.asImmutable(Nodes.unnamedRoot("subject")); - exception.expect(NullPointerException.class); - exception.expectMessage("child"); - //when - immutableNode.findOrCreateChild(null); - } - /** * Test that we throw an exception when passed null. */ @@ -431,24 +418,4 @@ public class ImmutableNodeItemTest { //when immutableNode.insertInPath(null, ""); } - - @Test - public void findOrCreateChildShouldReturnChildWhenChildIsFound() { - //given - val root = Nodes.unnamedRoot(""); - Nodes.namedChild("child", "child", root); - immutableNode = Nodes.asImmutable(root); - //when - val found = immutableNode.findOrCreateChild("child"); - assertThat(found).extracting(Node::getName).contains("child"); - } - - @Test - public void findOrCreateChildShouldThrowExceptionWhenChildNotFound() { - //given - immutableNode = Nodes.asImmutable(Nodes.unnamedRoot("")); - expectImmutableException(); - //when - immutableNode.findOrCreateChild("child"); - } } diff --git a/src/test/java/net/kemitix/node/NodeItemTest.java b/src/test/java/net/kemitix/node/NodeItemTest.java index 653ada5..0a92e75 100644 --- a/src/test/java/net/kemitix/node/NodeItemTest.java +++ b/src/test/java/net/kemitix/node/NodeItemTest.java @@ -469,52 +469,6 @@ public class NodeItemTest { + "is created").isEmpty(); } - /** - * Test that we can find a child of a node. - */ - @Test - public void shouldFindExistingChildNode() { - //given - node = Nodes.unnamedRoot("subject"); - val childData = "child"; - val child = Nodes.unnamedChild(childData, node); - //when - val found = node.findOrCreateChild(childData); - //then - assertThat(found).as( - "when searching for a child by data, the matching child is " - + "found").isSameAs(child); - } - - /** - * Test that we create a missing child of a node. - */ - @Test - public void shouldFindCreateNewChildNode() { - //given - node = Nodes.unnamedRoot("subject"); - val childData = "child"; - //when - val found = node.findOrCreateChild(childData); - //then - assertThat(found.getData()).as( - "when searching for a non-existent child by data, a new node " - + "is created").contains(childData); - } - - /** - * Test that if we pass null we get an exception. - */ - @Test - public void findOrCreateChildShouldThrowNPEFWhenChildIsNull() { - //given - node = Nodes.unnamedRoot("subject"); - exception.expect(NullPointerException.class); - exception.expectMessage("child"); - //when - node.findOrCreateChild(null); - } - /** * Test that we can get the node for a child. */