Nodes: add utility class to create Node instances
This commit is contained in:
parent
39721e77c1
commit
51e8194db7
2 changed files with 128 additions and 0 deletions
66
src/main/java/net/kemitix/node/Nodes.java
Normal file
66
src/main/java/net/kemitix/node/Nodes.java
Normal file
|
@ -0,0 +1,66 @@
|
|||
package net.kemitix.node;
|
||||
|
||||
/**
|
||||
* Utility class for {@link Node} items.
|
||||
*
|
||||
* @author pcampbell
|
||||
*/
|
||||
public final class Nodes {
|
||||
|
||||
private Nodes() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new unnamed root node.
|
||||
*
|
||||
* @param data the data the node will contain
|
||||
* @param <T> the type of the data
|
||||
*
|
||||
* @return the new node
|
||||
*/
|
||||
public static <T> Node<T> unnamedRoot(final T data) {
|
||||
return new NodeItem<>(data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new named root node.
|
||||
*
|
||||
* @param data the data the node will contain
|
||||
* @param name the name of the node
|
||||
* @param <T> the type of the data
|
||||
*
|
||||
* @return the new node
|
||||
*/
|
||||
public static <T> Node<T> namedRoot(final T data, final String name) {
|
||||
return new NodeItem<>(data, name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new unnamed child node.
|
||||
*
|
||||
* @param data the data the node will contain
|
||||
* @param parent the parent of the node
|
||||
* @param <T> the type of the data
|
||||
*
|
||||
* @return the new node
|
||||
*/
|
||||
public static <T> Node<T> unnamedChild(final T data, final Node<T> parent) {
|
||||
return new NodeItem<>(data, parent);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new named child node.
|
||||
*
|
||||
* @param data the data the node will contain
|
||||
* @param name the name of the node
|
||||
* @param parent the parent of the node
|
||||
* @param <T> the type of the data
|
||||
*
|
||||
* @return the new node
|
||||
*/
|
||||
public static <T> Node<T> namedChild(
|
||||
final T data, final String name, final Node<T> parent) {
|
||||
return new NodeItem<>(data, name, parent);
|
||||
}
|
||||
|
||||
}
|
62
src/test/java/net/kemitix/node/NodesTest.java
Normal file
62
src/test/java/net/kemitix/node/NodesTest.java
Normal file
|
@ -0,0 +1,62 @@
|
|||
package net.kemitix.node;
|
||||
|
||||
import lombok.val;
|
||||
import org.assertj.core.api.SoftAssertions;
|
||||
import org.junit.Test;
|
||||
|
||||
import static net.trajano.commons.testing.UtilityClassTestUtil
|
||||
.assertUtilityClassWellDefined;
|
||||
|
||||
/**
|
||||
* Tests for {@link Nodes}.
|
||||
*
|
||||
* @author pcampbell
|
||||
*/
|
||||
public class NodesTest {
|
||||
|
||||
@Test
|
||||
public void shouldBeValidUtilityClass() throws Exception {
|
||||
assertUtilityClassWellDefined(Nodes.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldCreateUnnamedRoot() throws Exception {
|
||||
val node = Nodes.unnamedRoot("data");
|
||||
SoftAssertions softly = new SoftAssertions();
|
||||
softly.assertThat(node.getData()).contains("data");
|
||||
softly.assertThat(node.getName()).isNull();
|
||||
softly.assertAll();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldCreateNamedRoot() throws Exception {
|
||||
val node = Nodes.namedRoot("data", "name");
|
||||
SoftAssertions softly = new SoftAssertions();
|
||||
softly.assertThat(node.getData()).contains("data");
|
||||
softly.assertThat(node.getName()).isEqualTo("name");
|
||||
softly.assertAll();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldCreateUnnamedChild() throws Exception {
|
||||
val parent = Nodes.unnamedRoot("root");
|
||||
val node = Nodes.unnamedChild("data", parent);
|
||||
SoftAssertions softly = new SoftAssertions();
|
||||
softly.assertThat(node.getData()).contains("data");
|
||||
softly.assertThat(node.getName()).isNull();
|
||||
softly.assertThat(node.getParent()).contains(parent);
|
||||
softly.assertAll();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldCreateNamedChild() throws Exception {
|
||||
val parent = Nodes.unnamedRoot("root");
|
||||
val node = Nodes.namedChild("data", "child", parent);
|
||||
SoftAssertions softly = new SoftAssertions();
|
||||
softly.assertThat(node.getData()).contains("data");
|
||||
softly.assertThat(node.getName()).isEqualTo("child");
|
||||
softly.assertThat(node.getParent()).contains(parent);
|
||||
softly.assertAll();
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in a new issue