Merge branch release/0.1.0 into master
Initial Release Signed-off-by: Paul Campbell <pcampbell@kemitix.net>
This commit is contained in:
commit
c8acd884e1
12 changed files with 1169 additions and 0 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -1,3 +1,5 @@
|
||||||
|
/target
|
||||||
|
/nbproject
|
||||||
*.class
|
*.class
|
||||||
|
|
||||||
# Mobile Tools for Java (J2ME)
|
# Mobile Tools for Java (J2ME)
|
||||||
|
|
3
.travis.yml
Normal file
3
.travis.yml
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
language: java
|
||||||
|
jdk:
|
||||||
|
- oraclejdk8
|
8
CHANGELOG
Normal file
8
CHANGELOG
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
CHANGELOG
|
||||||
|
=========
|
||||||
|
|
||||||
|
0.1.0
|
||||||
|
------
|
||||||
|
|
||||||
|
* Initial Release
|
||||||
|
|
62
README.md
62
README.md
|
@ -1,2 +1,64 @@
|
||||||
# node
|
# node
|
||||||
A parent/children data structure
|
A parent/children data structure
|
||||||
|
|
||||||
|
# Usage
|
||||||
|
|
||||||
|
Add as a dependency in your `pom.xml`:
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>net.kemitix</groupId>
|
||||||
|
<artifactId>node</artifactId>
|
||||||
|
<version>${node.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
The library consits of an interface `Node` and an implementation `NodeItem`.
|
||||||
|
|
||||||
|
## Create a root node
|
||||||
|
|
||||||
|
Node<String> root = new NodeItem<>("[root]");
|
||||||
|
|
||||||
|
## Get the contents of the node
|
||||||
|
|
||||||
|
String rootData = root.getData(); // returns "[root]"
|
||||||
|
|
||||||
|
## Add a child node
|
||||||
|
|
||||||
|
Node<String> child = root.createChild("child");
|
||||||
|
|
||||||
|
Which is shorthand for:
|
||||||
|
|
||||||
|
Node<String> child = new NodeItem<>("child");
|
||||||
|
root.addChild(child);
|
||||||
|
|
||||||
|
The tree now looks like:
|
||||||
|
|
||||||
|
"[root]"
|
||||||
|
\-> "child"
|
||||||
|
|
||||||
|
## Get the child node
|
||||||
|
|
||||||
|
Node<String> childNode = root.getChild("child");
|
||||||
|
|
||||||
|
## Create a chain of nodes
|
||||||
|
|
||||||
|
root.createDescendantLine(Arrays.asList("alpha", "beta", "gamma"));
|
||||||
|
|
||||||
|
"[root]"
|
||||||
|
|-> "child"
|
||||||
|
\-> "alpha"
|
||||||
|
\-> "beta"
|
||||||
|
\-> "gamma"
|
||||||
|
|
||||||
|
## Walk the tree to find a node
|
||||||
|
|
||||||
|
Optional<Node<String>> foundNode = root.walkTree(Arrays.asList(
|
||||||
|
"alpha", "beta", "gamma"));
|
||||||
|
if (foundNode.isPresent()) {
|
||||||
|
String betaData = foundNode.get().getParent().getData();
|
||||||
|
// returns "beta"
|
||||||
|
}
|
||||||
|
|
||||||
|
## Get all children of a node
|
||||||
|
|
||||||
|
Set<Node<String>> children = root.getChildren();
|
||||||
|
children.size(); // returns 2 ("child" and "alpha")
|
||||||
|
|
192
checkstyle.xml
Normal file
192
checkstyle.xml
Normal file
|
@ -0,0 +1,192 @@
|
||||||
|
<?xml version="1.0"?>
|
||||||
|
<!DOCTYPE module PUBLIC
|
||||||
|
"-//Puppy Crawl//DTD Check Configuration 1.3//EN"
|
||||||
|
"http://www.puppycrawl.com/dtds/configuration_1_3.dtd">
|
||||||
|
|
||||||
|
<!--
|
||||||
|
|
||||||
|
Checkstyle configuration that checks the sun coding conventions from:
|
||||||
|
|
||||||
|
- the Java Language Specification at
|
||||||
|
http://java.sun.com/docs/books/jls/second_edition/html/index.html
|
||||||
|
|
||||||
|
- the Sun Code Conventions at http://java.sun.com/docs/codeconv/
|
||||||
|
|
||||||
|
- the Javadoc guidelines at
|
||||||
|
http://java.sun.com/j2se/javadoc/writingdoccomments/index.html
|
||||||
|
|
||||||
|
- the JDK Api documentation http://java.sun.com/j2se/docs/api/index.html
|
||||||
|
|
||||||
|
- some best practices
|
||||||
|
|
||||||
|
Checkstyle is very configurable. Be sure to read the documentation at
|
||||||
|
http://checkstyle.sf.net (or in your downloaded distribution).
|
||||||
|
|
||||||
|
Most Checks are configurable, be sure to consult the documentation.
|
||||||
|
|
||||||
|
To completely disable a check, just comment it out or delete it from the file.
|
||||||
|
|
||||||
|
Finally, it is worth reading the documentation.
|
||||||
|
|
||||||
|
-->
|
||||||
|
|
||||||
|
<module name="Checker">
|
||||||
|
<!--
|
||||||
|
If you set the basedir property below, then all reported file
|
||||||
|
names will be relative to the specified directory. See
|
||||||
|
http://checkstyle.sourceforge.net/5.x/config.html#Checker
|
||||||
|
|
||||||
|
<property name="basedir" value="${basedir}"/>
|
||||||
|
-->
|
||||||
|
|
||||||
|
<!-- Checks that a package-info.java file exists for each package. -->
|
||||||
|
<!-- See http://checkstyle.sf.net/config_javadoc.html#JavadocPackage -->
|
||||||
|
<module name="JavadocPackage"/>
|
||||||
|
|
||||||
|
<!-- Checks whether files end with a new line. -->
|
||||||
|
<!-- See http://checkstyle.sf.net/config_misc.html#NewlineAtEndOfFile -->
|
||||||
|
<module name="NewlineAtEndOfFile"/>
|
||||||
|
|
||||||
|
<!-- Checks that property files contain the same keys. -->
|
||||||
|
<!-- See http://checkstyle.sf.net/config_misc.html#Translation -->
|
||||||
|
<module name="Translation"/>
|
||||||
|
|
||||||
|
<!-- Checks for Size Violations. -->
|
||||||
|
<!-- See http://checkstyle.sf.net/config_sizes.html -->
|
||||||
|
<module name="FileLength"/>
|
||||||
|
|
||||||
|
<!-- Checks for whitespace -->
|
||||||
|
<!-- See http://checkstyle.sf.net/config_whitespace.html -->
|
||||||
|
<module name="FileTabCharacter"/>
|
||||||
|
|
||||||
|
<!-- Miscellaneous other checks. -->
|
||||||
|
<!-- See http://checkstyle.sf.net/config_misc.html -->
|
||||||
|
<module name="RegexpSingleline">
|
||||||
|
<property name="format" value="\s+$"/>
|
||||||
|
<property name="minimum" value="0"/>
|
||||||
|
<property name="maximum" value="0"/>
|
||||||
|
<property name="message" value="Line has trailing spaces."/>
|
||||||
|
</module>
|
||||||
|
|
||||||
|
|
||||||
|
<!-- Checks for Headers -->
|
||||||
|
<!-- See http://checkstyle.sf.net/config_header.html -->
|
||||||
|
<!-- <module name="Header"> -->
|
||||||
|
<!-- <property name="headerFile" value="${checkstyle.header.file}"/> -->
|
||||||
|
<!-- <property name="fileExtensions" value="java"/> -->
|
||||||
|
<!-- </module> -->
|
||||||
|
|
||||||
|
<module name="TreeWalker">
|
||||||
|
|
||||||
|
<!-- Support @SuppressWarnings annotation -->
|
||||||
|
<!-- See http://checkstyle.sourceforge.net/config.html -->
|
||||||
|
<module name="SuppressWarningsHolder"/>
|
||||||
|
|
||||||
|
<module name="FileContentsHolder"/>
|
||||||
|
|
||||||
|
<!-- Checks for Javadoc comments. -->
|
||||||
|
<!-- See http://checkstyle.sf.net/config_javadoc.html -->
|
||||||
|
<module name="JavadocMethod">
|
||||||
|
<property name="scope" value="public"/>
|
||||||
|
</module>
|
||||||
|
<module name="JavadocType"/>
|
||||||
|
<!--<module name="JavadocVariable"/>-->
|
||||||
|
<module name="JavadocStyle"/>
|
||||||
|
|
||||||
|
|
||||||
|
<!-- Checks for Naming Conventions. -->
|
||||||
|
<!-- See http://checkstyle.sf.net/config_naming.html -->
|
||||||
|
<module name="ConstantName"/>
|
||||||
|
<module name="LocalFinalVariableName"/>
|
||||||
|
<module name="LocalVariableName"/>
|
||||||
|
<module name="MemberName"/>
|
||||||
|
<module name="MethodName"/>
|
||||||
|
<module name="PackageName"/>
|
||||||
|
<module name="ParameterName"/>
|
||||||
|
<module name="StaticVariableName"/>
|
||||||
|
<module name="TypeName"/>
|
||||||
|
|
||||||
|
|
||||||
|
<!-- Checks for imports -->
|
||||||
|
<!-- See http://checkstyle.sf.net/config_import.html -->
|
||||||
|
<module name="AvoidStarImport"/>
|
||||||
|
<module name="IllegalImport"/> <!-- defaults to sun.* packages -->
|
||||||
|
<module name="RedundantImport"/>
|
||||||
|
<module name="UnusedImports"/>
|
||||||
|
|
||||||
|
|
||||||
|
<!-- Checks for Size Violations. -->
|
||||||
|
<!-- See http://checkstyle.sf.net/config_sizes.html -->
|
||||||
|
<module name="LineLength"/>
|
||||||
|
<module name="MethodLength"/>
|
||||||
|
<module name="ParameterNumber"/>
|
||||||
|
|
||||||
|
|
||||||
|
<!-- Checks for whitespace -->
|
||||||
|
<!-- See http://checkstyle.sf.net/config_whitespace.html -->
|
||||||
|
<module name="EmptyForIteratorPad"/>
|
||||||
|
<module name="GenericWhitespace"/>
|
||||||
|
<module name="MethodParamPad"/>
|
||||||
|
<module name="NoWhitespaceAfter"/>
|
||||||
|
<module name="NoWhitespaceBefore"/>
|
||||||
|
<module name="OperatorWrap"/>
|
||||||
|
<module name="ParenPad"/>
|
||||||
|
<module name="TypecastParenPad"/>
|
||||||
|
<module name="WhitespaceAfter"/>
|
||||||
|
<module name="WhitespaceAround"/>
|
||||||
|
|
||||||
|
|
||||||
|
<!-- Modifier Checks -->
|
||||||
|
<!-- See http://checkstyle.sf.net/config_modifiers.html -->
|
||||||
|
<module name="ModifierOrder"/>
|
||||||
|
<module name="RedundantModifier"/>
|
||||||
|
|
||||||
|
|
||||||
|
<!-- Checks for blocks. You know, those {}'s -->
|
||||||
|
<!-- See http://checkstyle.sf.net/config_blocks.html -->
|
||||||
|
<module name="AvoidNestedBlocks"/>
|
||||||
|
<module name="EmptyBlock"/>
|
||||||
|
<module name="LeftCurly"/>
|
||||||
|
<module name="NeedBraces"/>
|
||||||
|
<module name="RightCurly"/>
|
||||||
|
|
||||||
|
|
||||||
|
<!-- Checks for common coding problems -->
|
||||||
|
<!-- See http://checkstyle.sf.net/config_coding.html -->
|
||||||
|
<module name="AvoidInlineConditionals"/>
|
||||||
|
<module name="EmptyStatement"/>
|
||||||
|
<module name="EqualsHashCode"/>
|
||||||
|
<module name="HiddenField">
|
||||||
|
<property name="ignoreConstructorParameter" value="true"/>
|
||||||
|
<property name="ignoreSetter" value="true"/>
|
||||||
|
<property name="setterCanReturnItsClass" value="true"/>
|
||||||
|
</module>
|
||||||
|
<module name="IllegalInstantiation"/>
|
||||||
|
<module name="InnerAssignment"/>
|
||||||
|
<module name="MagicNumber"/>
|
||||||
|
<module name="MissingSwitchDefault"/>
|
||||||
|
<module name="SimplifyBooleanExpression"/>
|
||||||
|
<module name="SimplifyBooleanReturn"/>
|
||||||
|
|
||||||
|
<!-- Checks for class design -->
|
||||||
|
<!-- See http://checkstyle.sf.net/config_design.html -->
|
||||||
|
<!--<module name="DesignForExtension"/>-->
|
||||||
|
<module name="FinalClass"/>
|
||||||
|
<module name="HideUtilityClassConstructor"/>
|
||||||
|
<module name="InterfaceIsType"/>
|
||||||
|
<module name="VisibilityModifier"/>
|
||||||
|
|
||||||
|
|
||||||
|
<!-- Miscellaneous other checks. -->
|
||||||
|
<!-- See http://checkstyle.sf.net/config_misc.html -->
|
||||||
|
<module name="ArrayTypeStyle"/>
|
||||||
|
<module name="FinalParameters"/>
|
||||||
|
<module name="TodoComment"/>
|
||||||
|
<module name="UpperEll"/>
|
||||||
|
|
||||||
|
</module>
|
||||||
|
|
||||||
|
<module name="SuppressWarningsFilter"/>
|
||||||
|
<module name="SuppressionCommentFilter"/>
|
||||||
|
|
||||||
|
</module>
|
53
pom.xml
Normal file
53
pom.xml
Normal file
|
@ -0,0 +1,53 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<groupId>net.kemitix</groupId>
|
||||||
|
<artifactId>node</artifactId>
|
||||||
|
<version>0.1.0</version>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
|
<name>Node</name>
|
||||||
|
<description>A parent/children data structure</description>
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<groupId>net.kemitix</groupId>
|
||||||
|
<artifactId>kemitix-parent</artifactId>
|
||||||
|
<version>0.6.0</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<issueManagement>
|
||||||
|
<url>https://github.com/kemitix/node/issues</url>
|
||||||
|
<system>GitHub Issues</system>
|
||||||
|
</issueManagement>
|
||||||
|
|
||||||
|
<scm>
|
||||||
|
<connection>scm:git:git@github.com:kemitix/node.git</connection>
|
||||||
|
<developerConnection>scm:git:git@github.com:kemitix/node.git</developerConnection>
|
||||||
|
<url>git@github.com:kemitix/node.git</url>
|
||||||
|
</scm>
|
||||||
|
|
||||||
|
<url>https://github.com/kemitix/node</url>
|
||||||
|
|
||||||
|
<inceptionYear>2016</inceptionYear>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
<version>1.16.6</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>junit</groupId>
|
||||||
|
<artifactId>junit</artifactId>
|
||||||
|
<version>4.12</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.hamcrest</groupId>
|
||||||
|
<artifactId>hamcrest-core</artifactId>
|
||||||
|
<version>1.3</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
</project>
|
108
src/main/java/net/kemitix/node/Node.java
Normal file
108
src/main/java/net/kemitix/node/Node.java
Normal file
|
@ -0,0 +1,108 @@
|
||||||
|
package net.kemitix.node;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An interface for tree node items.
|
||||||
|
*
|
||||||
|
* @author pcampbell
|
||||||
|
* @param <T> the type of data held in each node
|
||||||
|
*/
|
||||||
|
public interface Node<T> {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fetch the data held within the node.
|
||||||
|
*
|
||||||
|
* @return the node's data
|
||||||
|
*/
|
||||||
|
T getData();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fetch the parent node.
|
||||||
|
*
|
||||||
|
* <p>
|
||||||
|
* If the node is a root node, i.e. has no parent, then this will return
|
||||||
|
* null.
|
||||||
|
*
|
||||||
|
* @return the parent node
|
||||||
|
*/
|
||||||
|
Node<T> getParent();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fetches the child nodes.
|
||||||
|
*
|
||||||
|
* @return the set of child nodes
|
||||||
|
*/
|
||||||
|
Set<Node<T>> getChildren();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds the child to the node.
|
||||||
|
*
|
||||||
|
* @param child the node to add
|
||||||
|
*/
|
||||||
|
void addChild(final Node<T> child);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new node and adds it as a child of the current node.
|
||||||
|
*
|
||||||
|
* @param child the child node's data
|
||||||
|
*
|
||||||
|
* @return the new child node
|
||||||
|
*/
|
||||||
|
Node<T> createChild(final T child);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Populates the tree with the path of nodes, each being a child of the
|
||||||
|
* previous node in the path.
|
||||||
|
*
|
||||||
|
* @param descendants the line of descendants from the current node
|
||||||
|
*/
|
||||||
|
void createDescendantLine(final 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
|
||||||
|
*/
|
||||||
|
Node<T> findOrCreateChild(final T child);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fetches the node for the child if present.
|
||||||
|
*
|
||||||
|
* @param child the child's data to search for
|
||||||
|
*
|
||||||
|
* @return an {@link Optional} containing the child node if found
|
||||||
|
*/
|
||||||
|
Optional<Node<T>> getChild(final T child);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if the node is an ancestor.
|
||||||
|
*
|
||||||
|
* @param node the potential ancestor
|
||||||
|
*
|
||||||
|
* @return true if the node is an ancestor
|
||||||
|
*/
|
||||||
|
boolean isChildOf(final Node<T> node);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Make the current node a direct child of the parent.
|
||||||
|
*
|
||||||
|
* @param parent the new parent node
|
||||||
|
*/
|
||||||
|
void setParent(final Node<T> parent);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Walks the node tree using the path to select each child.
|
||||||
|
*
|
||||||
|
* @param path the path to the desired child
|
||||||
|
*
|
||||||
|
* @return the child or null
|
||||||
|
*/
|
||||||
|
Optional<Node<T>> walkTree(final List<T> path);
|
||||||
|
|
||||||
|
}
|
20
src/main/java/net/kemitix/node/NodeException.java
Normal file
20
src/main/java/net/kemitix/node/NodeException.java
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
package net.kemitix.node;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents an error within the tree node.
|
||||||
|
*
|
||||||
|
* @author pcampbell
|
||||||
|
*/
|
||||||
|
@SuppressWarnings("serial")
|
||||||
|
public class NodeException extends RuntimeException {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor with message.
|
||||||
|
*
|
||||||
|
* @param message the message
|
||||||
|
*/
|
||||||
|
public NodeException(final String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
186
src/main/java/net/kemitix/node/NodeItem.java
Normal file
186
src/main/java/net/kemitix/node/NodeItem.java
Normal file
|
@ -0,0 +1,186 @@
|
||||||
|
package net.kemitix.node;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.NonNull;
|
||||||
|
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents a tree of nodes.
|
||||||
|
*
|
||||||
|
* @param <T> the type of data stored in each node
|
||||||
|
*
|
||||||
|
* @author pcampbell
|
||||||
|
*/
|
||||||
|
public class NodeItem<T> implements Node<T> {
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
private final T data;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
private Node<T> parent;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
private Set<Node<T>> children;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a root node.
|
||||||
|
*
|
||||||
|
* @param data the value of the node
|
||||||
|
*/
|
||||||
|
public NodeItem(@NonNull final T data) {
|
||||||
|
this(data, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a node with a parent.
|
||||||
|
*
|
||||||
|
* @param data the value of the node
|
||||||
|
* @param parent the parent node
|
||||||
|
*/
|
||||||
|
public NodeItem(final T data, final Node<T> parent) {
|
||||||
|
this.data = data;
|
||||||
|
if (parent != null) {
|
||||||
|
setParent(parent);
|
||||||
|
}
|
||||||
|
this.children = new HashSet<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Make the current node a direct child of the parent.
|
||||||
|
*
|
||||||
|
* @param parent the new parent node
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public final void setParent(@NonNull final Node<T> parent) {
|
||||||
|
if (this.equals(parent) || parent.isChildOf(this)) {
|
||||||
|
throw new NodeException("Parent is a descendant");
|
||||||
|
}
|
||||||
|
if (this.parent != null) {
|
||||||
|
this.parent.getChildren().remove(this);
|
||||||
|
}
|
||||||
|
this.parent = parent;
|
||||||
|
parent.addChild(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds the child to the node.
|
||||||
|
*
|
||||||
|
* @param child the node to add
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void addChild(@NonNull final Node<T> child) {
|
||||||
|
if (this.equals(child) || isChildOf(child)) {
|
||||||
|
throw new NodeException("Child is an ancestor");
|
||||||
|
}
|
||||||
|
children.add(child);
|
||||||
|
if (child.getParent() == null || !child.getParent().equals(this)) {
|
||||||
|
child.setParent(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if the node is an ancestor.
|
||||||
|
*
|
||||||
|
* @param node the potential ancestor
|
||||||
|
*
|
||||||
|
* @return true if the node is an ancestor
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public boolean isChildOf(final Node<T> node) {
|
||||||
|
if (node.equals(parent)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (parent != null) {
|
||||||
|
return parent.isChildOf(node);
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Walks the node tree using the path to select each child.
|
||||||
|
*
|
||||||
|
* @param path the path to the desired child
|
||||||
|
*
|
||||||
|
* @return the child or null
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public Optional<Node<T>> walkTree(@NonNull final List<T> path) {
|
||||||
|
if (path.size() > 0) {
|
||||||
|
Optional<Node<T>> found = children.stream()
|
||||||
|
.filter((Node<T> child) -> path.get(0)
|
||||||
|
.equals(child.getData()))
|
||||||
|
.findFirst();
|
||||||
|
if (found.isPresent()) {
|
||||||
|
if (path.size() > 1) {
|
||||||
|
return found.get().walkTree(path.subList(1, path.size()));
|
||||||
|
}
|
||||||
|
return found;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return Optional.empty();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Populates the tree with the path of nodes, each being a child of the
|
||||||
|
* previous node in the path.
|
||||||
|
*
|
||||||
|
* @param descendants the line of descendants from the current node
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void createDescendantLine(@NonNull final List<T> descendants) {
|
||||||
|
if (!descendants.isEmpty()) {
|
||||||
|
findOrCreateChild(descendants.get(0))
|
||||||
|
.createDescendantLine(
|
||||||
|
descendants.subList(1, descendants.size()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public Node<T> findOrCreateChild(@NonNull final T child) {
|
||||||
|
Optional<Node<T>> found = getChild(child);
|
||||||
|
if (found.isPresent()) {
|
||||||
|
return found.get();
|
||||||
|
} else {
|
||||||
|
return createChild(child);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fetches the node for the child if present.
|
||||||
|
*
|
||||||
|
* @param child the child's data to search for
|
||||||
|
*
|
||||||
|
* @return an {@link Optional} containing the child node if found
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public Optional<Node<T>> getChild(@NonNull final T child) {
|
||||||
|
return children.stream()
|
||||||
|
.filter((Node<T> t) -> t.getData().equals(child))
|
||||||
|
.findAny();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new node and adds it as a child of the current node.
|
||||||
|
*
|
||||||
|
* @param child the child node's data
|
||||||
|
*
|
||||||
|
* @return the new child node
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public Node<T> createChild(@NonNull final T child) {
|
||||||
|
return new NodeItem<>(child, this);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
4
src/main/java/net/kemitix/node/package-info.java
Normal file
4
src/main/java/net/kemitix/node/package-info.java
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
/**
|
||||||
|
* Tree Node implementation.
|
||||||
|
*/
|
||||||
|
package net.kemitix.node;
|
35
src/test/java/net/kemitix/node/NodeExceptionTest.java
Normal file
35
src/test/java/net/kemitix/node/NodeExceptionTest.java
Normal file
|
@ -0,0 +1,35 @@
|
||||||
|
package net.kemitix.node;
|
||||||
|
|
||||||
|
import net.kemitix.node.NodeException;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static org.hamcrest.CoreMatchers.is;
|
||||||
|
import static org.junit.Assert.assertThat;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests for {@link NodeException}.
|
||||||
|
*
|
||||||
|
* @author pcampbell
|
||||||
|
*/
|
||||||
|
public class NodeExceptionTest {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class under test.
|
||||||
|
*/
|
||||||
|
private NodeException nodeException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test that message provided to constructor is returned.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void shouldReturnConstructorMessage() {
|
||||||
|
//given
|
||||||
|
final String message = "this is the message";
|
||||||
|
//when
|
||||||
|
nodeException = new NodeException(message);
|
||||||
|
//then
|
||||||
|
assertThat(nodeException.getMessage(), is(message));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
496
src/test/java/net/kemitix/node/NodeItemTest.java
Normal file
496
src/test/java/net/kemitix/node/NodeItemTest.java
Normal file
|
@ -0,0 +1,496 @@
|
||||||
|
package net.kemitix.node;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
import static org.hamcrest.CoreMatchers.hasItem;
|
||||||
|
import static org.hamcrest.CoreMatchers.is;
|
||||||
|
import static org.junit.Assert.assertFalse;
|
||||||
|
import static org.junit.Assert.assertNull;
|
||||||
|
import static org.junit.Assert.assertThat;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test for {@link NodeItem}.
|
||||||
|
*
|
||||||
|
* @author pcampbell
|
||||||
|
*/
|
||||||
|
public class NodeItemTest {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class under test.
|
||||||
|
*/
|
||||||
|
private Node<String> node;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test {@link NodeItem#Node(java.lang.Object) } that node data is
|
||||||
|
* recoverable.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void shouldReturnNodeData() {
|
||||||
|
//given
|
||||||
|
final String data = "this node data";
|
||||||
|
//when
|
||||||
|
node = new NodeItem<>(data);
|
||||||
|
//then
|
||||||
|
assertThat(node.getData(), is(data));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test {@link NodeItem#Node(java.lang.Object) } that passing null as node
|
||||||
|
* data throws exception.
|
||||||
|
*/
|
||||||
|
@Test(expected = NullPointerException.class)
|
||||||
|
public void shouldThrowNPEWhenDataIsNull() {
|
||||||
|
//when
|
||||||
|
node = new NodeItem<>(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test {@link NodeItem#Node(java.lang.Object) } that default node parent is
|
||||||
|
* null.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void shouldHaveNullForDefaulParent() {
|
||||||
|
//given
|
||||||
|
node = new NodeItem<>("data");
|
||||||
|
//then
|
||||||
|
assertNull(node.getParent());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test {@link NodeItem#Node(java.lang.Object, net.kemitix.node.Node) } that
|
||||||
|
* provided node parent is returned.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void shouldReturnNodeParent() {
|
||||||
|
//given
|
||||||
|
Node<String> parent = new NodeItem<>("parent");
|
||||||
|
//when
|
||||||
|
node = new NodeItem<>("subject", parent);
|
||||||
|
//then
|
||||||
|
assertThat(node.getParent(), is(parent));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test {@link NodeItem#Node(java.lang.Object, net.kemitix.node.Node) } that
|
||||||
|
* setting the parent on a node where the proposed parent is a child of the
|
||||||
|
* node throws an exception.
|
||||||
|
*/
|
||||||
|
@Test(expected = NodeException.class)
|
||||||
|
public void shouldThrowNEWhenSettingParentToAChild() {
|
||||||
|
//given
|
||||||
|
node = new NodeItem<>("subject");
|
||||||
|
Node<String> child = new NodeItem<>("child", node);
|
||||||
|
//when
|
||||||
|
node.setParent(child);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test {@link NodeItem#Node(java.lang.Object, net.kemitix.node.Node) } that
|
||||||
|
* when parent is added to created node, the created node is now a child of
|
||||||
|
* the parent.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void shouldAddNewNodeAsChildToParent() {
|
||||||
|
//given
|
||||||
|
Node<String> parent = new NodeItem<>("parent");
|
||||||
|
//when
|
||||||
|
node = new NodeItem<>("subject", parent);
|
||||||
|
//then
|
||||||
|
assertThat(parent.getChildren(), hasItem(node));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test {@link NodeItem#setParent(net.kemitix.node.Node) } that we return
|
||||||
|
* the same parent when set.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void shouldReturnSetParent() {
|
||||||
|
//given
|
||||||
|
node = new NodeItem<>("subject");
|
||||||
|
Node<String> parent = new NodeItem<>("parent");
|
||||||
|
//when
|
||||||
|
node.setParent(parent);
|
||||||
|
//then
|
||||||
|
assertThat(node.getParent(), is(parent));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test {@link NodeItem#setParent(net.kemitix.node.Node) } that we throw an
|
||||||
|
* exception when passed null.
|
||||||
|
*/
|
||||||
|
@Test(expected = NullPointerException.class)
|
||||||
|
public void shouldThrowNPEWhenSetParentNull() {
|
||||||
|
//given
|
||||||
|
node = new NodeItem<>("subject");
|
||||||
|
//when
|
||||||
|
node.setParent(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test {@link NodeItem#setParent(net.kemitix.node.Node) } that we throw an
|
||||||
|
* exceptions when attempting to node as its own parent.
|
||||||
|
*/
|
||||||
|
@Test(expected = NodeException.class)
|
||||||
|
public void shouldThrowNEWhenSetParentSelf() {
|
||||||
|
//given
|
||||||
|
node = new NodeItem<>("subject");
|
||||||
|
//when
|
||||||
|
node.setParent(node);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test {@link NodeItem#setParent(net.kemitix.node.Node) } that when a node
|
||||||
|
* with an existing parent is assigned a new parent, that the old parent no
|
||||||
|
* longer sees it as one of its children.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void shouldUpdateOldParentWhenNodeSetToNewParent() {
|
||||||
|
//given
|
||||||
|
node = new NodeItem<>("subject");
|
||||||
|
Node<String> child = node.createChild("child");
|
||||||
|
Node<String> newParent = new NodeItem<>("newParent");
|
||||||
|
//when
|
||||||
|
child.setParent(newParent);
|
||||||
|
//then
|
||||||
|
assertThat(child.getParent(), is(newParent));
|
||||||
|
assertFalse(node.getChild("child").isPresent());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test {@link NodeItem#addChild(net.kemitix.node.Node) } that when a node
|
||||||
|
* is added as a child to another node, that it's previous parent no longer
|
||||||
|
* has it as a child.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void shouldRemoveNodeFromOldParentWhenAddedAsChildToNewParent() {
|
||||||
|
//given
|
||||||
|
node = new NodeItem<>("subject");
|
||||||
|
Node<String> child = node.createChild("child");
|
||||||
|
Node<String> newParent = new NodeItem<>("newParent");
|
||||||
|
//when
|
||||||
|
newParent.addChild(child);
|
||||||
|
//then
|
||||||
|
assertThat(child.getParent(), is(newParent));
|
||||||
|
assertFalse(node.getChild("child").isPresent());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test {@link NodeItem#addChild(net.kemitix.node.Node) } that adding null
|
||||||
|
* as a child throws an exception.
|
||||||
|
*/
|
||||||
|
@Test(expected = NullPointerException.class)
|
||||||
|
public void shouldThrowNPEWhenAddingNullAsChild() {
|
||||||
|
//given
|
||||||
|
node = new NodeItem<>("subject");
|
||||||
|
//when
|
||||||
|
node.addChild(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test {@link NodeItem#addChild(net.kemitix.node.Node) } that adding a
|
||||||
|
* child is returned.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void shouldReturnAddedChild() {
|
||||||
|
//given
|
||||||
|
Node<String> child = new NodeItem<>("child");
|
||||||
|
node = new NodeItem<>("subject");
|
||||||
|
//when
|
||||||
|
node.addChild(child);
|
||||||
|
//then
|
||||||
|
assertThat(node.getChildren(), hasItem(child));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test {@link NodeItem#addChild(net.kemitix.node.Node) } that adding a node
|
||||||
|
* as it's own child throws an exception.
|
||||||
|
*/
|
||||||
|
@Test(expected = NodeException.class)
|
||||||
|
public void shouldThrowNEWhenAddingANodeAsOwnChild() {
|
||||||
|
//given
|
||||||
|
node = new NodeItem<>("subject");
|
||||||
|
//then
|
||||||
|
node.addChild(node);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test {@link NodeItem#addChild(net.kemitix.node.Node) } that adding a node
|
||||||
|
* to itself as a child causes an exception.
|
||||||
|
*/
|
||||||
|
@Test(expected = NodeException.class)
|
||||||
|
public void shouldThrowWhenAddingSelfAsChild() {
|
||||||
|
//given
|
||||||
|
node = new NodeItem<>("subject");
|
||||||
|
//when
|
||||||
|
node.addChild(node);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test {@link NodeItem#addChild(net.kemitix.node.Node) } that adding the
|
||||||
|
* parent to node causes an exception.
|
||||||
|
*/
|
||||||
|
@Test(expected = NodeException.class)
|
||||||
|
public void shouldThrowWhenAddingParentAsChild() {
|
||||||
|
//given
|
||||||
|
Node<String> parent = new NodeItem<>("parent");
|
||||||
|
node = new NodeItem<>("subject", parent);
|
||||||
|
//when
|
||||||
|
node.addChild(parent);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test {@link NodeItem#addChild(net.kemitix.node.Node) } that adding the
|
||||||
|
* grandparent to node causes an exception.
|
||||||
|
*/
|
||||||
|
@Test(expected = NodeException.class)
|
||||||
|
public void shouldThrowWhenAddingGrandParentAsChild() {
|
||||||
|
//given
|
||||||
|
Node<String> grandParent = new NodeItem<>("grandparent");
|
||||||
|
Node<String> parent = new NodeItem<>("parent", grandParent);
|
||||||
|
node = new NodeItem<>("subject", parent);
|
||||||
|
//when
|
||||||
|
node.addChild(grandParent);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test {@link NodeItem#addChild(net.kemitix.node.Node) } that adding a
|
||||||
|
* child to a node, sets the child's parent node.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void shouldSetParentOnChildWhenAddedAsChild() {
|
||||||
|
//given
|
||||||
|
Node<String> child = new NodeItem<>("child");
|
||||||
|
node = new NodeItem<>("subject");
|
||||||
|
//when
|
||||||
|
node.addChild(child);
|
||||||
|
//then
|
||||||
|
assertThat(child.getParent(), is(node));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test {@link NodeItem#walkTree(java.util.List) } that we can walk a tree
|
||||||
|
* to the target node.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void shouldWalkTreeToNode() {
|
||||||
|
//given
|
||||||
|
final String grandparent = "grandparent";
|
||||||
|
Node<String> grandParentNode = new NodeItem<>(grandparent);
|
||||||
|
final String parent = "parent";
|
||||||
|
Node<String> parentNode = new NodeItem<>(parent, grandParentNode);
|
||||||
|
final String subject = "subject";
|
||||||
|
node = new NodeItem<>(subject, parentNode);
|
||||||
|
//when
|
||||||
|
Optional<Node<String>> result = grandParentNode.walkTree(Arrays.asList(
|
||||||
|
parent, subject));
|
||||||
|
//then
|
||||||
|
assertTrue(result.isPresent());
|
||||||
|
assertThat(result.get(), is(node));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test {@link NodeItem#walkTree(java.util.List) } that we get an empty
|
||||||
|
* {@link Optional} when walking a path that doesn't exist.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void shouldNotFindNonExistantChildNode() {
|
||||||
|
//given
|
||||||
|
final String parent = "parent";
|
||||||
|
Node<String> parentNode = new NodeItem<>(parent);
|
||||||
|
final String subject = "subject";
|
||||||
|
node = new NodeItem<>(subject, parentNode);
|
||||||
|
//when
|
||||||
|
Optional<Node<String>> result = parentNode.walkTree(Arrays.asList(
|
||||||
|
subject, "no child"));
|
||||||
|
//then
|
||||||
|
assertFalse(result.isPresent());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test {@link NodeItem#walkTree(java.util.List) } that when we pass null we
|
||||||
|
* get an exception.
|
||||||
|
*/
|
||||||
|
@Test(expected = NullPointerException.class)
|
||||||
|
public void shouldThrowNEWhenWalkTreeNull() {
|
||||||
|
//given
|
||||||
|
node = new NodeItem<>("subject");
|
||||||
|
//when
|
||||||
|
node.walkTree(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test {@link NodeItem#walkTree(java.util.List) } that when we pass an
|
||||||
|
* empty path we get and empty {@link Optional} as a result.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void shouldReturnEmptyForEmptyWalkTreePath() {
|
||||||
|
//given
|
||||||
|
node = new NodeItem<>("subject");
|
||||||
|
//when
|
||||||
|
node.walkTree(Collections.emptyList());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test {@link NodeItem#createDescendantLine(java.util.List) } that we can
|
||||||
|
* create a chain of descendant nodes.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void shouldCreateDescendantNodes() {
|
||||||
|
//given
|
||||||
|
node = new NodeItem<>("subject");
|
||||||
|
final String alphaData = "alpha";
|
||||||
|
final String betaData = "beta";
|
||||||
|
final String gammaData = "gamma";
|
||||||
|
//when
|
||||||
|
node.createDescendantLine(
|
||||||
|
Arrays.asList(alphaData, betaData, gammaData));
|
||||||
|
//then
|
||||||
|
final Optional<Node<String>> alphaOptional = node.getChild(alphaData);
|
||||||
|
assertTrue(alphaOptional.isPresent());
|
||||||
|
Node<String> alpha = alphaOptional.get();
|
||||||
|
assertThat(alpha.getParent(), is(node));
|
||||||
|
final Optional<Node<String>> betaOptional = alpha.getChild(betaData);
|
||||||
|
assertTrue(betaOptional.isPresent());
|
||||||
|
Node<String> beta = betaOptional.get();
|
||||||
|
assertThat(beta.getParent(), is(alpha));
|
||||||
|
final Optional<Node<String>> gammaOptional = beta.getChild(gammaData);
|
||||||
|
assertTrue(gammaOptional.isPresent());
|
||||||
|
Node<String> gamma = gammaOptional.get();
|
||||||
|
assertThat(gamma.getParent(), is(beta));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test {@link NodeItem#createDescendantLine(java.util.List) } that if we
|
||||||
|
* pass null to create a chain of descendant nodes we get an exception.
|
||||||
|
*/
|
||||||
|
@Test(expected = NullPointerException.class)
|
||||||
|
public void shouldThrowNPEWhenCreateDescendantNull() {
|
||||||
|
//given
|
||||||
|
node = new NodeItem<>("subject");
|
||||||
|
//when
|
||||||
|
node.createDescendantLine(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test {@link NodeItem#createDescendantLine(java.util.List) } that if we
|
||||||
|
* pass an empty list nothing is changed.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void shouldChangeNothingWhenCreateDescendantEmpty() {
|
||||||
|
//given
|
||||||
|
node = new NodeItem<>("subject");
|
||||||
|
//when
|
||||||
|
node.createDescendantLine(Collections.emptyList());
|
||||||
|
//then
|
||||||
|
assertThat(node.getChildren().size(), is(0));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test {@link NodeItem#findOrCreateChild(java.lang.Object) } that we can
|
||||||
|
* find a child of a node.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void shouldFindExistingChildNode() {
|
||||||
|
//given
|
||||||
|
node = new NodeItem<>("subject");
|
||||||
|
final String childData = "child";
|
||||||
|
Node<String> child = new NodeItem<>(childData, node);
|
||||||
|
//when
|
||||||
|
Node<String> found = node.findOrCreateChild(childData);
|
||||||
|
//then
|
||||||
|
assertThat(found, is(child));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test {@link NodeItem#findOrCreateChild(java.lang.Object) } that we create
|
||||||
|
* a missing child of a node.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void shouldFindCreateNewChildNode() {
|
||||||
|
//given
|
||||||
|
node = new NodeItem<>("subject");
|
||||||
|
final String childData = "child";
|
||||||
|
//when
|
||||||
|
Node<String> found = node.findOrCreateChild(childData);
|
||||||
|
//then
|
||||||
|
assertThat(found.getData(), is(childData));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test {@link NodeItem#findOrCreateChild(java.lang.Object) } that if we
|
||||||
|
* pass null we get an exception.
|
||||||
|
*/
|
||||||
|
@Test(expected = NullPointerException.class)
|
||||||
|
public void shouldThrowNPEFWhenFindOrCreateChildNull() {
|
||||||
|
//given
|
||||||
|
node = new NodeItem<>("subject");
|
||||||
|
//when
|
||||||
|
node.findOrCreateChild(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test {@link NodeItem#getChild(java.lang.Object) } that we can get the
|
||||||
|
* node for a child.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void shouldGetChild() {
|
||||||
|
//given
|
||||||
|
node = new NodeItem<>("subject");
|
||||||
|
final String childData = "child";
|
||||||
|
Node<String> child = new NodeItem<>(childData);
|
||||||
|
node.addChild(child);
|
||||||
|
//when
|
||||||
|
Optional<Node<String>> found = node.getChild(childData);
|
||||||
|
//then
|
||||||
|
assertTrue(found.isPresent());
|
||||||
|
assertThat(found.get(), is(child));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test {@link NodeItem#getChild(java.lang.Object) } that we throw an
|
||||||
|
* exception when passed null.
|
||||||
|
*/
|
||||||
|
@Test(expected = NullPointerException.class)
|
||||||
|
public void shouldThrowNPEWhenGetChildNull() {
|
||||||
|
//given
|
||||||
|
node = new NodeItem<>("subject");
|
||||||
|
//when
|
||||||
|
node.getChild(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test {@link NodeItem#createChild(java.lang.Object) } that we create a
|
||||||
|
* child as a child of the current node and with the current node as its
|
||||||
|
* parent.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void shoudCreateChild() {
|
||||||
|
//given
|
||||||
|
node = new NodeItem<>("subject");
|
||||||
|
final String childData = "child";
|
||||||
|
//when
|
||||||
|
Node<String> child = node.createChild(childData);
|
||||||
|
//then
|
||||||
|
assertThat(child.getParent(), is(node));
|
||||||
|
final Optional<Node<String>> foundChild = node.getChild(childData);
|
||||||
|
assertTrue(foundChild.isPresent());
|
||||||
|
assertThat(foundChild.get(), is(child));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test that we throw an exception when passed null.
|
||||||
|
*/
|
||||||
|
@Test(expected = NullPointerException.class)
|
||||||
|
public void shouldThrowNPEWhenCreateChildNull() {
|
||||||
|
//given
|
||||||
|
node = new NodeItem<>("subject");
|
||||||
|
//when
|
||||||
|
node.createChild(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in a new issue