Merge pull request #19 from kemitix/upgrade-kemitix-parent-to-2.1.0
Upgrade kemitix-parent to 2.1.0
This commit is contained in:
commit
22518df8b9
9 changed files with 300 additions and 113 deletions
|
@ -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.
|
||||||
|
*/
|
2
pom.xml
2
pom.xml
|
@ -11,7 +11,7 @@
|
||||||
<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>
|
||||||
|
|
|
@ -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;
|
||||||
|
@ -11,23 +35,31 @@ import java.util.Set;
|
||||||
* An abstract node item, providing default implementations for most read-only
|
* An abstract node item, providing default implementations for most read-only
|
||||||
* operations.
|
* operations.
|
||||||
*
|
*
|
||||||
* @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)
|
||||||
*/
|
*/
|
||||||
abstract class AbstractNodeItem<T> implements Node<T> {
|
abstract class AbstractNodeItem<T> implements Node<T> {
|
||||||
|
|
||||||
|
private final Set<Node<T>> children;
|
||||||
|
|
||||||
private T data;
|
private T data;
|
||||||
|
|
||||||
private String name;
|
private String name;
|
||||||
|
|
||||||
private Node<T> parent;
|
private Node<T> parent;
|
||||||
|
|
||||||
private final Set<Node<T>> children;
|
/**
|
||||||
|
* Constructor.
|
||||||
|
*
|
||||||
|
* @param data the data of the node
|
||||||
|
* @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
|
||||||
|
*/
|
||||||
protected AbstractNodeItem(
|
protected AbstractNodeItem(
|
||||||
final T data, final String name, final Node<T> parent,
|
final T data, final String name, final Node<T> parent, @NonNull final Set<Node<T>> children
|
||||||
final Set<Node<T>> children) {
|
) {
|
||||||
this.data = data;
|
this.data = data;
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.parent = parent;
|
this.parent = parent;
|
||||||
|
@ -68,16 +100,18 @@ abstract class AbstractNodeItem<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 -> {
|
||||||
return d.isPresent() && d.get().equals(child);
|
final Optional<T> d = node.getData();
|
||||||
}).findAny();
|
return d.isPresent() && d.get()
|
||||||
|
.equals(child);
|
||||||
|
})
|
||||||
|
.findAny();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Node<T> getChild(final T child) {
|
public Node<T> getChild(final T child) {
|
||||||
return findChild(child).orElseThrow(
|
return findChild(child).orElseThrow(() -> new NodeException("Child not found"));
|
||||||
() -> new NodeException("Child not found"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -89,8 +123,7 @@ abstract class AbstractNodeItem<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));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -106,14 +139,9 @@ abstract class AbstractNodeItem<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);
|
||||||
}
|
}
|
||||||
|
@ -121,14 +149,14 @@ abstract class AbstractNodeItem<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(n -> n.getName().equals(named))
|
.filter(n -> n.getName()
|
||||||
|
.equals(named))
|
||||||
.findAny();
|
.findAny();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Node<T> getChildByName(final String named) {
|
public Node<T> getChildByName(final String named) {
|
||||||
return findChildByName(named).orElseThrow(
|
return findChildByName(named).orElseThrow(() -> new NodeException("Named child not found"));
|
||||||
() -> new NodeException("Named child not found"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -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,50 @@ 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 AbstractNodeItem<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, parent, children);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -73,8 +119,7 @@ final class ImmutableNodeItem<T> extends AbstractNodeItem<T> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Node<T> findOrCreateChild(final T child) {
|
public Node<T> findOrCreateChild(final T child) {
|
||||||
return findChild(child).orElseThrow(
|
return findChild(child).orElseThrow(() -> new UnsupportedOperationException(IMMUTABLE_OBJECT));
|
||||||
() -> new UnsupportedOperationException(IMMUTABLE_OBJECT));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -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();
|
||||||
|
|
||||||
|
@ -114,8 +137,7 @@ public interface Node<T> {
|
||||||
*
|
*
|
||||||
* @return the found or created child node
|
* @return the found or created child node
|
||||||
*
|
*
|
||||||
* @deprecated use {@code node.findChild(child).orElseGet(() ->
|
* @deprecated use {@code node.findChild(child).orElseGet(() -> node.createChild(child))};
|
||||||
* node.createChild(child))};
|
|
||||||
*/
|
*/
|
||||||
@Deprecated
|
@Deprecated
|
||||||
Node<T> findOrCreateChild(T child);
|
Node<T> findOrCreateChild(T child);
|
||||||
|
@ -135,8 +157,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 +203,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,16 +36,16 @@ 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;
|
||||||
|
@ -100,6 +124,24 @@ 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 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);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Set<Node<T>> getChildren() {
|
public Set<Node<T>> getChildren() {
|
||||||
return children;
|
return children;
|
||||||
|
@ -117,20 +159,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");
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -169,8 +211,7 @@ 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(
|
findOrCreateChild(descendants.get(0)).createDescendantLine(descendants.subList(1, descendants.size()));
|
||||||
descendants.subList(1, descendants.size()));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -182,8 +223,7 @@ class NodeItem<T> implements Node<T> {
|
||||||
*
|
*
|
||||||
* @return the found or created child node
|
* @return the found or created child node
|
||||||
*
|
*
|
||||||
* @deprecated use node.findChild(child).orElseGet(() -> node.createChild
|
* @deprecated use node.findChild(child).orElseGet(() -> node.createChild (child));
|
||||||
* (child));
|
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
@Deprecated
|
@Deprecated
|
||||||
|
@ -200,10 +240,13 @@ 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 -> {
|
||||||
return d.isPresent() && d.get().equals(child);
|
final Optional<T> d = node.getData();
|
||||||
}).findAny();
|
return d.isPresent() && d.get()
|
||||||
|
.equals(child);
|
||||||
|
})
|
||||||
|
.findAny();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -224,25 +267,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 +283,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);
|
||||||
}
|
}
|
||||||
|
@ -276,10 +296,8 @@ class NodeItem<T> implements Node<T> {
|
||||||
insertChild(nodeItem);
|
insertChild(nodeItem);
|
||||||
} else {
|
} else {
|
||||||
val item = path[0];
|
val item = path[0];
|
||||||
findChildByName(item)
|
findChildByName(item).orElseGet(() -> new NodeItem<>(null, item, this))
|
||||||
.orElseGet(() -> new NodeItem<>(null, item, this))
|
.insertInPath(nodeItem, Arrays.copyOfRange(path, 1, path.length));
|
||||||
.insertInPath(nodeItem,
|
|
||||||
Arrays.copyOfRange(path, 1, path.length));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -299,10 +317,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 +331,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,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.Optional;
|
import java.util.Optional;
|
||||||
|
@ -7,7 +31,7 @@ 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 {
|
||||||
|
|
||||||
|
@ -63,7 +87,8 @@ 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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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,15 +118,16 @@ 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();
|
final Optional<Node<T>> sourceParent = source.getParent();
|
||||||
if (sourceParent.isPresent()) {
|
if (sourceParent.isPresent()) {
|
||||||
return ImmutableNodeItem.newChild(source.getData().orElse(null),
|
return ImmutableNodeItem.newChild(source.getData()
|
||||||
source.getName(), sourceParent.get(),
|
.orElse(null), source.getName(), sourceParent.get(),
|
||||||
getImmutableChildren(source));
|
getImmutableChildren(source)
|
||||||
|
);
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalArgumentException(
|
throw new IllegalArgumentException("source must not be the root node");
|
||||||
"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;
|
||||||
|
|
Loading…
Reference in a new issue