Node: drop findOrCreateChild()
This commit is contained in:
parent
6a93ed9199
commit
76c4d4ceeb
5 changed files with 4 additions and 114 deletions
|
@ -118,11 +118,6 @@ final class ImmutableNodeItem<T> extends NodeItem<T> {
|
||||||
throw new UnsupportedOperationException(IMMUTABLE_OBJECT);
|
throw new UnsupportedOperationException(IMMUTABLE_OBJECT);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public Node<T> findOrCreateChild(final T child) {
|
|
||||||
return findChild(child).orElseThrow(() -> new UnsupportedOperationException(IMMUTABLE_OBJECT));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void insertInPath(final Node<T> node, final String... path) {
|
public void insertInPath(final Node<T> node, final String... path) {
|
||||||
throw new UnsupportedOperationException(IMMUTABLE_OBJECT);
|
throw new UnsupportedOperationException(IMMUTABLE_OBJECT);
|
||||||
|
|
|
@ -129,19 +129,6 @@ public interface Node<T> {
|
||||||
*/
|
*/
|
||||||
void createDescendantLine(List<T> descendants);
|
void createDescendantLine(List<T> 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<T> findOrCreateChild(T child);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Fetches the node for the child if present.
|
* Fetches the node for the child if present.
|
||||||
*
|
*
|
||||||
|
|
|
@ -199,26 +199,13 @@ class NodeItem<T> implements Node<T> {
|
||||||
@Override
|
@Override
|
||||||
public void createDescendantLine(@NonNull final List<T> descendants) {
|
public void createDescendantLine(@NonNull final List<T> descendants) {
|
||||||
if (!descendants.isEmpty()) {
|
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<T> findOrCreateChild(@NonNull final T child) {
|
|
||||||
return findChild(child).orElseGet(() -> createChild(child));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Fetches the node for the child if present.
|
* Fetches the node for the child if present.
|
||||||
*
|
*
|
||||||
|
|
|
@ -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.
|
* Test that we throw an exception when passed null.
|
||||||
*/
|
*/
|
||||||
|
@ -431,24 +418,4 @@ public class ImmutableNodeItemTest {
|
||||||
//when
|
//when
|
||||||
immutableNode.insertInPath(null, "");
|
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");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -469,52 +469,6 @@ public class NodeItemTest {
|
||||||
+ "is created").isEmpty();
|
+ "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.
|
* Test that we can get the node for a child.
|
||||||
*/
|
*/
|
||||||
|
|
Loading…
Reference in a new issue