Merge branch release/0.5.0 into master
Make ImmutableNodeItem extend NodeItem Upgrade kemitix-parent to 2.1.0 Upgrade lombok to 1.16.10 Upgrade assertj to 3.5.2
This commit is contained in:
commit
c81d20ea5d
13 changed files with 312 additions and 410 deletions
|
@ -1,6 +1,15 @@
|
||||||
CHANGELOG
|
CHANGELOG
|
||||||
=========
|
=========
|
||||||
|
|
||||||
|
0.5.0
|
||||||
|
------
|
||||||
|
|
||||||
|
* Make ImmutableNodeItem extend NodeItem
|
||||||
|
* Upgrade kemitix-parent to 2.1.0
|
||||||
|
* Upgrade lombok to 1.16.10
|
||||||
|
* Upgrade assertj to 3.5.2
|
||||||
|
*
|
||||||
|
|
||||||
0.4.0
|
0.4.0
|
||||||
------
|
------
|
||||||
|
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
/*
|
||||||
The MIT License (MIT)
|
The MIT License (MIT)
|
||||||
|
|
||||||
Copyright (c) 2016 Paul Campbell
|
Copyright (c) 2016 Paul Campbell
|
||||||
|
@ -19,3 +20,4 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
SOFTWARE.
|
SOFTWARE.
|
||||||
|
*/
|
9
pom.xml
9
pom.xml
|
@ -2,7 +2,7 @@
|
||||||
<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">
|
<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>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<artifactId>node</artifactId>
|
<artifactId>node</artifactId>
|
||||||
<version>0.4.2</version>
|
<version>0.5.0</version>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
<name>Node</name>
|
<name>Node</name>
|
||||||
|
@ -11,11 +11,12 @@
|
||||||
<parent>
|
<parent>
|
||||||
<groupId>net.kemitix</groupId>
|
<groupId>net.kemitix</groupId>
|
||||||
<artifactId>kemitix-parent</artifactId>
|
<artifactId>kemitix-parent</artifactId>
|
||||||
<version>2.0.0</version>
|
<version>2.1.0</version>
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<assertj.version>3.4.1</assertj.version>
|
<lombok.version>1.16.10</lombok.version>
|
||||||
|
<assertj.version>3.5.2</assertj.version>
|
||||||
<coveralls-maven-plugin.version>4.2.0</coveralls-maven-plugin.version>
|
<coveralls-maven-plugin.version>4.2.0</coveralls-maven-plugin.version>
|
||||||
<trajano-commons-testing.version>2.1.0</trajano-commons-testing.version>
|
<trajano-commons-testing.version>2.1.0</trajano-commons-testing.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
@ -49,7 +50,7 @@
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.projectlombok</groupId>
|
<groupId>org.projectlombok</groupId>
|
||||||
<artifactId>lombok</artifactId>
|
<artifactId>lombok</artifactId>
|
||||||
<version>1.16.8</version>
|
<version>${lombok.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>junit</groupId>
|
<groupId>junit</groupId>
|
||||||
|
|
|
@ -1,155 +0,0 @@
|
||||||
package net.kemitix.node;
|
|
||||||
|
|
||||||
import lombok.NonNull;
|
|
||||||
|
|
||||||
import java.util.HashSet;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Optional;
|
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* An abstract node item, providing default implementations for most read-only
|
|
||||||
* operations.
|
|
||||||
*
|
|
||||||
* @author Paul Campbell
|
|
||||||
*
|
|
||||||
* @param <T> the type of data stored in each node
|
|
||||||
*/
|
|
||||||
abstract class AbstractNodeItem<T> implements Node<T> {
|
|
||||||
|
|
||||||
private T data;
|
|
||||||
|
|
||||||
private String name;
|
|
||||||
|
|
||||||
private Node<T> parent;
|
|
||||||
|
|
||||||
private final Set<Node<T>> children;
|
|
||||||
|
|
||||||
protected AbstractNodeItem(
|
|
||||||
final T data, final String name, final Node<T> parent,
|
|
||||||
final Set<Node<T>> children) {
|
|
||||||
this.data = data;
|
|
||||||
this.name = name;
|
|
||||||
this.parent = parent;
|
|
||||||
this.children = children;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getName() {
|
|
||||||
return name;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Optional<T> getData() {
|
|
||||||
return Optional.ofNullable(data);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean isEmpty() {
|
|
||||||
return data == null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Optional<Node<T>> getParent() {
|
|
||||||
return Optional.ofNullable(parent);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Set<Node<T>> getChildren() {
|
|
||||||
return new HashSet<>(children);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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>> findChild(@NonNull final T child) {
|
|
||||||
return children.stream().filter(node -> {
|
|
||||||
final Optional<T> d = node.getData();
|
|
||||||
return d.isPresent() && d.get().equals(child);
|
|
||||||
}).findAny();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Node<T> getChild(final T child) {
|
|
||||||
return findChild(child).orElseThrow(
|
|
||||||
() -> new NodeException("Child not found"));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Checks if the node is an ancestor.
|
|
||||||
*
|
|
||||||
* @param node the potential ancestor
|
|
||||||
*
|
|
||||||
* @return true if the node is an ancestor
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public boolean isDescendantOf(final Node<T> node) {
|
|
||||||
return parent != null && (node.equals(parent) || parent.isDescendantOf(
|
|
||||||
node));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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>> findInPath(@NonNull final List<T> path) {
|
|
||||||
if (path.isEmpty()) {
|
|
||||||
return Optional.empty();
|
|
||||||
}
|
|
||||||
Node<T> current = this;
|
|
||||||
for (T item : path) {
|
|
||||||
final Optional<Node<T>> child = current.findChild(item);
|
|
||||||
if (child.isPresent()) {
|
|
||||||
current = child.get();
|
|
||||||
} else {
|
|
||||||
current = null;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return Optional.ofNullable(current);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Optional<Node<T>> findChildByName(@NonNull final String named) {
|
|
||||||
return children.stream()
|
|
||||||
.filter(n -> n.getName().equals(named))
|
|
||||||
.findAny();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Node<T> getChildByName(final String named) {
|
|
||||||
return findChildByName(named).orElseThrow(
|
|
||||||
() -> new NodeException("Named child not found"));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String drawTree(final int depth) {
|
|
||||||
final StringBuilder sb = new StringBuilder();
|
|
||||||
final String unnamed = "(unnamed)";
|
|
||||||
if (isNamed()) {
|
|
||||||
sb.append(formatByDepth(name, depth));
|
|
||||||
} else if (!children.isEmpty()) {
|
|
||||||
sb.append(formatByDepth(unnamed, depth));
|
|
||||||
}
|
|
||||||
getChildren().forEach(c -> sb.append(c.drawTree(depth + 1)));
|
|
||||||
return sb.toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
private String formatByDepth(final String value, final int depth) {
|
|
||||||
return String.format("[%1$" + (depth + value.length()) + "s]\n", value);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean isNamed() {
|
|
||||||
return name != null && name.length() > 0;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,3 +1,27 @@
|
||||||
|
/*
|
||||||
|
The MIT License (MIT)
|
||||||
|
|
||||||
|
Copyright (c) 2016 Paul Campbell
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
package net.kemitix.node;
|
package net.kemitix.node;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
@ -11,28 +35,51 @@ import java.util.Set;
|
||||||
* getData()} they could then modify the original data within the node. This
|
* getData()} they could then modify the original data within the node. This
|
||||||
* wouldn't affect the integrity of the node tree structure, however.</p>
|
* wouldn't affect the integrity of the node tree structure, however.</p>
|
||||||
*
|
*
|
||||||
* @author Paul Campbell
|
|
||||||
*
|
|
||||||
* @param <T> the type of data stored in each node
|
* @param <T> the type of data stored in each node
|
||||||
|
*
|
||||||
|
* @author Paul Campbell (pcampbell@kemitix.net)
|
||||||
*/
|
*/
|
||||||
final class ImmutableNodeItem<T> extends AbstractNodeItem<T> {
|
final class ImmutableNodeItem<T> extends NodeItem<T> {
|
||||||
|
|
||||||
private static final String IMMUTABLE_OBJECT = "Immutable object";
|
private static final String IMMUTABLE_OBJECT = "Immutable object";
|
||||||
|
|
||||||
private ImmutableNodeItem(
|
private ImmutableNodeItem(
|
||||||
final T data, final String name, final Node<T> parent,
|
final T data, final String name, final Node<T> parent, final Set<Node<T>> children
|
||||||
final Set<Node<T>> children) {
|
) {
|
||||||
super(data, name, parent, children);
|
super(data, name, null, children);
|
||||||
|
forceParent(parent);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new immutable root node.
|
||||||
|
*
|
||||||
|
* @param data the data of the node
|
||||||
|
* @param name the name of the node
|
||||||
|
* @param children the children of the node
|
||||||
|
* @param <T> the type of the data in the node
|
||||||
|
*
|
||||||
|
* @return the new node tree's root node
|
||||||
|
*/
|
||||||
static <T> ImmutableNodeItem<T> newRoot(
|
static <T> ImmutableNodeItem<T> newRoot(
|
||||||
final T data, final String name, final Set<Node<T>> children) {
|
final T data, final String name, final Set<Node<T>> children
|
||||||
|
) {
|
||||||
return new ImmutableNodeItem<>(data, name, null, children);
|
return new ImmutableNodeItem<>(data, name, null, children);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new immutable subtree from this child.
|
||||||
|
*
|
||||||
|
* @param data the data of the node
|
||||||
|
* @param name the name of the node
|
||||||
|
* @param parent the mutable parent of the node
|
||||||
|
* @param children the children of the node
|
||||||
|
* @param <T> the type of the data in the node
|
||||||
|
*
|
||||||
|
* @return the new immutable node
|
||||||
|
*/
|
||||||
static <T> ImmutableNodeItem<T> newChild(
|
static <T> ImmutableNodeItem<T> newChild(
|
||||||
final T data, final String name, final Node<T> parent,
|
final T data, final String name, final Node<T> parent, final Set<Node<T>> children
|
||||||
final Set<Node<T>> children) {
|
) {
|
||||||
return new ImmutableNodeItem<>(data, name, parent, children);
|
return new ImmutableNodeItem<>(data, name, parent, children);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -71,12 +118,6 @@ final class ImmutableNodeItem<T> extends AbstractNodeItem<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);
|
||||||
|
@ -91,5 +132,4 @@ final class ImmutableNodeItem<T> extends AbstractNodeItem<T> {
|
||||||
public void removeParent() {
|
public void removeParent() {
|
||||||
throw new UnsupportedOperationException(IMMUTABLE_OBJECT);
|
throw new UnsupportedOperationException(IMMUTABLE_OBJECT);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,3 +1,27 @@
|
||||||
|
/*
|
||||||
|
The MIT License (MIT)
|
||||||
|
|
||||||
|
Copyright (c) 2016 Paul Campbell
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
package net.kemitix.node;
|
package net.kemitix.node;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
@ -7,9 +31,9 @@ import java.util.Set;
|
||||||
/**
|
/**
|
||||||
* An interface for tree node items.
|
* An interface for tree node items.
|
||||||
*
|
*
|
||||||
* @author Paul Campbell
|
|
||||||
*
|
|
||||||
* @param <T> the type of data held in each node
|
* @param <T> the type of data held in each node
|
||||||
|
*
|
||||||
|
* @author Paul Campbell (pcampbell@kemitix.net)
|
||||||
*/
|
*/
|
||||||
public interface Node<T> {
|
public interface Node<T> {
|
||||||
|
|
||||||
|
@ -32,8 +56,7 @@ public interface Node<T> {
|
||||||
/**
|
/**
|
||||||
* Fetch the data held within the node.
|
* Fetch the data held within the node.
|
||||||
*
|
*
|
||||||
* @return an Optional containing the node's data, or empty if the node has
|
* @return an Optional containing the node's data, or empty if the node has none
|
||||||
* none
|
|
||||||
*/
|
*/
|
||||||
Optional<T> getData();
|
Optional<T> getData();
|
||||||
|
|
||||||
|
@ -106,20 +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.
|
||||||
*
|
*
|
||||||
|
@ -135,8 +144,6 @@ public interface Node<T> {
|
||||||
* @param child the child's data to search for
|
* @param child the child's data to search for
|
||||||
*
|
*
|
||||||
* @return the child node if found
|
* @return the child node if found
|
||||||
*
|
|
||||||
* @throws NodeException if the node is not found
|
|
||||||
*/
|
*/
|
||||||
Node<T> getChild(T child);
|
Node<T> getChild(T child);
|
||||||
|
|
||||||
|
@ -183,8 +190,6 @@ public interface Node<T> {
|
||||||
* @param name the name of the child
|
* @param name the name of the child
|
||||||
*
|
*
|
||||||
* @return the node
|
* @return the node
|
||||||
*
|
|
||||||
* @throws NodeException if the node is not found
|
|
||||||
*/
|
*/
|
||||||
Node<T> getChildByName(String name);
|
Node<T> getChildByName(String name);
|
||||||
|
|
||||||
|
|
|
@ -1,9 +1,33 @@
|
||||||
|
/*
|
||||||
|
The MIT License (MIT)
|
||||||
|
|
||||||
|
Copyright (c) 2016 Paul Campbell
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
package net.kemitix.node;
|
package net.kemitix.node;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents an error within the tree node.
|
* Represents an error within the tree node.
|
||||||
*
|
*
|
||||||
* @author pcampbell
|
* @author Paul Campbell (pcampbell@kemitix.net)
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("serial")
|
@SuppressWarnings("serial")
|
||||||
public class NodeException extends RuntimeException {
|
public class NodeException extends RuntimeException {
|
||||||
|
|
|
@ -1,3 +1,27 @@
|
||||||
|
/*
|
||||||
|
The MIT License (MIT)
|
||||||
|
|
||||||
|
Copyright (c) 2016 Paul Campbell
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
package net.kemitix.node;
|
package net.kemitix.node;
|
||||||
|
|
||||||
import lombok.NonNull;
|
import lombok.NonNull;
|
||||||
|
@ -12,62 +36,46 @@ import java.util.Set;
|
||||||
/**
|
/**
|
||||||
* Represents a tree of nodes.
|
* Represents a tree of nodes.
|
||||||
*
|
*
|
||||||
* @author Paul Campbell
|
|
||||||
*
|
|
||||||
* @param <T> the type of data stored in each node
|
* @param <T> the type of data stored in each node
|
||||||
|
*
|
||||||
|
* @author Paul Campbell (pcampbell@kemitix.net)
|
||||||
*/
|
*/
|
||||||
class NodeItem<T> implements Node<T> {
|
class NodeItem<T> implements Node<T> {
|
||||||
|
|
||||||
private T data;
|
|
||||||
|
|
||||||
private final Set<Node<T>> children = new HashSet<>();
|
private final Set<Node<T>> children = new HashSet<>();
|
||||||
|
|
||||||
|
private T data;
|
||||||
|
|
||||||
private Node<T> parent;
|
private Node<T> parent;
|
||||||
|
|
||||||
private String name;
|
private String name;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create named root node.
|
* Constructor.
|
||||||
*
|
*
|
||||||
* @param data the data or null
|
* @param data the data of the node
|
||||||
* @param name the name
|
* @param name the name of the node
|
||||||
|
* @param parent the parent of the node, or null for a root node
|
||||||
|
* @param children the children of the node - must not be null
|
||||||
*/
|
*/
|
||||||
NodeItem(final T data, final String name) {
|
NodeItem(
|
||||||
this(data);
|
final T data, final String name, final Node<T> parent, @NonNull final Set<Node<T>> children
|
||||||
this.name = name;
|
) {
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create unnamed root node.
|
|
||||||
*
|
|
||||||
* @param data the data or null
|
|
||||||
*/
|
|
||||||
NodeItem(final T data) {
|
|
||||||
this.data = data;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates a node with a parent.
|
|
||||||
*
|
|
||||||
* @param data the data or null
|
|
||||||
* @param parent the parent node
|
|
||||||
*/
|
|
||||||
NodeItem(final T data, final Node<T> parent) {
|
|
||||||
this.data = data;
|
|
||||||
setParent(parent);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates a named node with a parent.
|
|
||||||
*
|
|
||||||
* @param data the data or null
|
|
||||||
* @param name the name
|
|
||||||
* @param parent the parent node
|
|
||||||
*/
|
|
||||||
NodeItem(final T data, final String name, final Node<T> parent) {
|
|
||||||
this.data = data;
|
this.data = data;
|
||||||
this.name = name;
|
this.name = name;
|
||||||
setParent(parent);
|
if (parent != null) {
|
||||||
|
doSetParent(parent);
|
||||||
|
}
|
||||||
|
this.children.addAll(children);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the parent of a node without updating the parent in the process as {@link #setParent(Node)} does.
|
||||||
|
*
|
||||||
|
* @param newParent The new parent node
|
||||||
|
*/
|
||||||
|
protected void forceParent(final Node<T> newParent) {
|
||||||
|
this.parent = newParent;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -100,6 +108,28 @@ class NodeItem<T> implements Node<T> {
|
||||||
return Optional.ofNullable(parent);
|
return Optional.ofNullable(parent);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Make the current node a direct child of the parent.
|
||||||
|
*
|
||||||
|
* @param parent the new parent node
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void setParent(@NonNull final Node<T> parent) {
|
||||||
|
doSetParent(parent);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void doSetParent(@NonNull final Node<T> newParent) {
|
||||||
|
if (this.equals(newParent) || newParent.isDescendantOf(this)) {
|
||||||
|
throw new NodeException("Parent is a descendant");
|
||||||
|
}
|
||||||
|
if (this.parent != null) {
|
||||||
|
this.parent.getChildren()
|
||||||
|
.remove(this);
|
||||||
|
}
|
||||||
|
this.parent = newParent;
|
||||||
|
newParent.addChild(this);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Set<Node<T>> getChildren() {
|
public Set<Node<T>> getChildren() {
|
||||||
return children;
|
return children;
|
||||||
|
@ -117,20 +147,20 @@ class NodeItem<T> implements Node<T> {
|
||||||
children.add(child);
|
children.add(child);
|
||||||
// update the child's parent if they don't have one or it is not this
|
// update the child's parent if they don't have one or it is not this
|
||||||
val childParent = child.getParent();
|
val childParent = child.getParent();
|
||||||
if (!childParent.isPresent() || !childParent.get().equals(this)) {
|
if (!childParent.isPresent() || !childParent.get()
|
||||||
|
.equals(this)) {
|
||||||
child.setParent(this);
|
child.setParent(this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void verifyChildWithSameNameDoesNotAlreadyExist(
|
private void verifyChildWithSameNameDoesNotAlreadyExist(
|
||||||
final @NonNull Node<T> child) {
|
final @NonNull Node<T> child
|
||||||
|
) {
|
||||||
if (child.isNamed()) {
|
if (child.isNamed()) {
|
||||||
findChildByName(child.getName())
|
findChildByName(child.getName()).filter(existingChild -> existingChild != child)
|
||||||
.filter(existingChild -> existingChild != child)
|
.ifPresent(existingChild -> {
|
||||||
.ifPresent(existingChild -> {
|
throw new NodeException("Node with that name already exists here");
|
||||||
throw new NodeException(
|
});
|
||||||
"Node with that name already exists here");
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -149,7 +179,7 @@ class NodeItem<T> implements Node<T> {
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public Node<T> createChild(@NonNull final T child) {
|
public Node<T> createChild(@NonNull final T child) {
|
||||||
return new NodeItem<>(child, this);
|
return new NodeItem<>(child, "", this, new HashSet<>());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -169,28 +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(
|
val child = descendants.get(0);
|
||||||
descendants.subList(1, descendants.size()));
|
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.
|
||||||
*
|
*
|
||||||
|
@ -200,19 +215,15 @@ class NodeItem<T> implements Node<T> {
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public Optional<Node<T>> findChild(@NonNull final T child) {
|
public Optional<Node<T>> findChild(@NonNull final T child) {
|
||||||
return children.stream().filter(node -> {
|
return children.stream()
|
||||||
final Optional<T> d = node.getData();
|
.filter(node -> child.equals(node.getData()
|
||||||
return d.isPresent() && d.get().equals(child);
|
.orElseGet(() -> null)))
|
||||||
}).findAny();
|
.findFirst();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Node<T> getChild(final T child) {
|
public Node<T> getChild(final T child) {
|
||||||
Optional<Node<T>> optional = findChild(child);
|
return findChild(child).orElseThrow(() -> new NodeException("Child not found"));
|
||||||
if (optional.isPresent()) {
|
|
||||||
return optional.get();
|
|
||||||
}
|
|
||||||
throw new NodeException("Child not found");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -224,25 +235,7 @@ class NodeItem<T> implements Node<T> {
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public boolean isDescendantOf(final Node<T> node) {
|
public boolean isDescendantOf(final Node<T> node) {
|
||||||
return parent != null && (node.equals(parent) || parent.isDescendantOf(
|
return parent != null && (node.equals(parent) || parent.isDescendantOf(node));
|
||||||
node));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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.isDescendantOf(this)) {
|
|
||||||
throw new NodeException("Parent is a descendant");
|
|
||||||
}
|
|
||||||
if (this.parent != null) {
|
|
||||||
this.parent.getChildren().remove(this);
|
|
||||||
}
|
|
||||||
this.parent = parent;
|
|
||||||
parent.addChild(this);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -258,14 +251,9 @@ class NodeItem<T> implements Node<T> {
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
}
|
}
|
||||||
Node<T> current = this;
|
Node<T> current = this;
|
||||||
for (T item : path) {
|
for (int i = 0, pathSize = path.size(); i < pathSize && current != null; i++) {
|
||||||
final Optional<Node<T>> child = current.findChild(item);
|
current = current.findChild(path.get(i))
|
||||||
if (child.isPresent()) {
|
.orElse(null);
|
||||||
current = child.get();
|
|
||||||
} else {
|
|
||||||
current = null;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return Optional.ofNullable(current);
|
return Optional.ofNullable(current);
|
||||||
}
|
}
|
||||||
|
@ -275,11 +263,9 @@ class NodeItem<T> implements Node<T> {
|
||||||
if (path.length == 0) {
|
if (path.length == 0) {
|
||||||
insertChild(nodeItem);
|
insertChild(nodeItem);
|
||||||
} else {
|
} else {
|
||||||
val item = path[0];
|
val nextInPath = path[0];
|
||||||
findChildByName(item)
|
findChildByName(nextInPath).orElseGet(() -> new NodeItem<>(null, nextInPath, this, new HashSet<>()))
|
||||||
.orElseGet(() -> new NodeItem<>(null, item, this))
|
.insertInPath(nodeItem, Arrays.copyOfRange(path, 1, path.length));
|
||||||
.insertInPath(nodeItem,
|
|
||||||
Arrays.copyOfRange(path, 1, path.length));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -299,10 +285,10 @@ class NodeItem<T> implements Node<T> {
|
||||||
val existing = childByName.get();
|
val existing = childByName.get();
|
||||||
if (existing.isEmpty()) {
|
if (existing.isEmpty()) {
|
||||||
// place any data in the new node into the existing empty node
|
// place any data in the new node into the existing empty node
|
||||||
nodeItem.getData().ifPresent(existing::setData);
|
nodeItem.getData()
|
||||||
|
.ifPresent(existing::setData);
|
||||||
} else {
|
} else {
|
||||||
throw new NodeException("A non-empty node named '"
|
throw new NodeException("A non-empty node named '" + nodeItem.getName() + "' already exists here");
|
||||||
+ nodeItem.getName() + "' already exists here");
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// nothing with the same name exists
|
// nothing with the same name exists
|
||||||
|
@ -313,8 +299,9 @@ class NodeItem<T> implements Node<T> {
|
||||||
@Override
|
@Override
|
||||||
public Optional<Node<T>> findChildByName(@NonNull final String named) {
|
public Optional<Node<T>> findChildByName(@NonNull final String named) {
|
||||||
return children.stream()
|
return children.stream()
|
||||||
.filter((Node<T> t) -> t.getName().equals(named))
|
.filter((Node<T> t) -> t.getName()
|
||||||
.findAny();
|
.equals(named))
|
||||||
|
.findAny();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -1,13 +1,37 @@
|
||||||
|
/*
|
||||||
|
The MIT License (MIT)
|
||||||
|
|
||||||
|
Copyright (c) 2016 Paul Campbell
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
package net.kemitix.node;
|
package net.kemitix.node;
|
||||||
|
|
||||||
import java.util.Optional;
|
import java.util.HashSet;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Utility class for {@link Node} items.
|
* Utility class for {@link Node} items.
|
||||||
*
|
*
|
||||||
* @author pcampbell
|
* @author Paul Campbell (pcampbell@kemitix.net)
|
||||||
*/
|
*/
|
||||||
public final class Nodes {
|
public final class Nodes {
|
||||||
|
|
||||||
|
@ -23,7 +47,7 @@ public final class Nodes {
|
||||||
* @return the new node
|
* @return the new node
|
||||||
*/
|
*/
|
||||||
public static <T> Node<T> unnamedRoot(final T data) {
|
public static <T> Node<T> unnamedRoot(final T data) {
|
||||||
return new NodeItem<>(data);
|
return new NodeItem<>(data, "", null, new HashSet<>());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -36,7 +60,7 @@ public final class Nodes {
|
||||||
* @return the new node
|
* @return the new node
|
||||||
*/
|
*/
|
||||||
public static <T> Node<T> namedRoot(final T data, final String name) {
|
public static <T> Node<T> namedRoot(final T data, final String name) {
|
||||||
return new NodeItem<>(data, name);
|
return new NodeItem<>(data, name, null, new HashSet<>());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -49,7 +73,7 @@ public final class Nodes {
|
||||||
* @return the new node
|
* @return the new node
|
||||||
*/
|
*/
|
||||||
public static <T> Node<T> unnamedChild(final T data, final Node<T> parent) {
|
public static <T> Node<T> unnamedChild(final T data, final Node<T> parent) {
|
||||||
return new NodeItem<>(data, parent);
|
return new NodeItem<>(data, "", parent, new HashSet<>());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -63,8 +87,9 @@ public final class Nodes {
|
||||||
* @return the new node
|
* @return the new node
|
||||||
*/
|
*/
|
||||||
public static <T> Node<T> namedChild(
|
public static <T> Node<T> namedChild(
|
||||||
final T data, final String name, final Node<T> parent) {
|
final T data, final String name, final Node<T> parent
|
||||||
return new NodeItem<>(data, name, parent);
|
) {
|
||||||
|
return new NodeItem<>(data, name, parent, new HashSet<>());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -76,12 +101,13 @@ public final class Nodes {
|
||||||
* @return the immutable copy of the tree
|
* @return the immutable copy of the tree
|
||||||
*/
|
*/
|
||||||
public static <T> Node<T> asImmutable(final Node<T> root) {
|
public static <T> Node<T> asImmutable(final Node<T> root) {
|
||||||
if (root.getParent().isPresent()) {
|
if (root.getParent()
|
||||||
|
.isPresent()) {
|
||||||
throw new IllegalArgumentException("source must be the root node");
|
throw new IllegalArgumentException("source must be the root node");
|
||||||
}
|
}
|
||||||
final Set<Node<T>> children = getImmutableChildren(root);
|
final Set<Node<T>> children = getImmutableChildren(root);
|
||||||
return ImmutableNodeItem.newRoot(root.getData().orElse(null),
|
return ImmutableNodeItem.newRoot(root.getData()
|
||||||
root.getName(), children);
|
.orElse(null), root.getName(), children);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static <T> Set<Node<T>> getImmutableChildren(final Node<T> source) {
|
private static <T> Set<Node<T>> getImmutableChildren(final Node<T> source) {
|
||||||
|
@ -92,16 +118,13 @@ public final class Nodes {
|
||||||
}
|
}
|
||||||
|
|
||||||
private static <T> Node<T> asImmutableChild(
|
private static <T> Node<T> asImmutableChild(
|
||||||
final Node<T> source) {
|
final Node<T> source
|
||||||
final Optional<Node<T>> sourceParent = source.getParent();
|
) {
|
||||||
if (sourceParent.isPresent()) {
|
return ImmutableNodeItem.newChild(source.getData()
|
||||||
return ImmutableNodeItem.newChild(source.getData().orElse(null),
|
.orElse(null), source.getName(), source.getParent()
|
||||||
source.getName(), sourceParent.get(),
|
.orElse(null),
|
||||||
getImmutableChildren(source));
|
getImmutableChildren(source)
|
||||||
} else {
|
);
|
||||||
throw new IllegalArgumentException(
|
|
||||||
"source must not be the root node");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,28 @@
|
||||||
|
/*
|
||||||
|
The MIT License (MIT)
|
||||||
|
|
||||||
|
Copyright (c) 2016 Paul Campbell
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
||||||
|
*/
|
||||||
/**
|
/**
|
||||||
* Tree Node implementation.
|
* Tree Node implementation.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package net.kemitix.node;
|
package net.kemitix.node;
|
||||||
|
|
|
@ -126,7 +126,7 @@ public class ImmutableNodeItemTest {
|
||||||
immutableNode = Nodes.asImmutable(Nodes.unnamedRoot("subject"));
|
immutableNode = Nodes.asImmutable(Nodes.unnamedRoot("subject"));
|
||||||
expectImmutableException();
|
expectImmutableException();
|
||||||
//when
|
//when
|
||||||
immutableNode.setParent(null);
|
immutableNode.setParent(Nodes.unnamedRoot("child"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -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.
|
||||||
*/
|
*/
|
||||||
|
@ -433,22 +420,11 @@ public class ImmutableNodeItemTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void findOrCreateChildShouldReturnChildWhenChildIsFound() {
|
public void AsImmutableShouldThrowIAEWhenNotRoot() {
|
||||||
//given
|
//given
|
||||||
val root = Nodes.unnamedRoot("");
|
exception.expect(IllegalArgumentException.class);
|
||||||
Nodes.namedChild("child", "child", root);
|
exception.expectMessage("source must be the root node");
|
||||||
immutableNode = Nodes.asImmutable(root);
|
|
||||||
//when
|
//when
|
||||||
val found = immutableNode.findOrCreateChild("child");
|
Nodes.asImmutable(Nodes.unnamedChild("child", Nodes.unnamedRoot("root")));
|
||||||
assertThat(found).extracting(Node::getName).contains("child");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void findOrCreateChildShouldThrowExceptionWhenChildNotFound() {
|
|
||||||
//given
|
|
||||||
immutableNode = Nodes.asImmutable(Nodes.unnamedRoot(""));
|
|
||||||
expectImmutableException();
|
|
||||||
//when
|
|
||||||
immutableNode.findOrCreateChild("child");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,6 +10,7 @@ import org.junit.rules.ExpectedException;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
@ -84,6 +85,17 @@ public class NodeItemTest {
|
||||||
.contains(parent);
|
.contains(parent);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldAddAsChildWhenCreatedWithParent() {
|
||||||
|
//given
|
||||||
|
final Node<String> root = Nodes.namedRoot("root data", "root name");
|
||||||
|
//when
|
||||||
|
final Node<String> child = Nodes.namedChild("child data", "child name", root);
|
||||||
|
//then
|
||||||
|
final Set<Node<String>> children = root.getChildren();
|
||||||
|
assertThat(children).containsExactly(child);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test that setting the parent on a node where the proposed parent is a
|
* Test that setting the parent on a node where the proposed parent is a
|
||||||
* child of the node throws an exception.
|
* child of the node throws an exception.
|
||||||
|
@ -457,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.
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -24,7 +24,7 @@ public class NodesTest {
|
||||||
val node = Nodes.unnamedRoot("data");
|
val node = Nodes.unnamedRoot("data");
|
||||||
SoftAssertions softly = new SoftAssertions();
|
SoftAssertions softly = new SoftAssertions();
|
||||||
softly.assertThat(node.getData()).contains("data");
|
softly.assertThat(node.getData()).contains("data");
|
||||||
softly.assertThat(node.getName()).isNull();
|
softly.assertThat(node.getName()).isEmpty();
|
||||||
softly.assertAll();
|
softly.assertAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -43,7 +43,7 @@ public class NodesTest {
|
||||||
val node = Nodes.unnamedChild("data", parent);
|
val node = Nodes.unnamedChild("data", parent);
|
||||||
SoftAssertions softly = new SoftAssertions();
|
SoftAssertions softly = new SoftAssertions();
|
||||||
softly.assertThat(node.getData()).contains("data");
|
softly.assertThat(node.getData()).contains("data");
|
||||||
softly.assertThat(node.getName()).isNull();
|
softly.assertThat(node.getName()).isEmpty();
|
||||||
softly.assertThat(node.getParent()).contains(parent);
|
softly.assertThat(node.getParent()).contains(parent);
|
||||||
softly.assertAll();
|
softly.assertAll();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue