From a39fa049c2c24416af9d9512f2d8205ea71c7626 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Tue, 13 Sep 2016 22:06:29 +0100 Subject: [PATCH 01/10] LICENSE{ => .txt}: rename and add to java sources Rename the LICENSE file and add a copy of it to each java source file as a header. --- LICENSE => LICENSE.txt | 2 ++ .../net/kemitix/node/AbstractNodeItem.java | 24 +++++++++++++++++++ .../net/kemitix/node/ImmutableNodeItem.java | 24 +++++++++++++++++++ src/main/java/net/kemitix/node/Node.java | 24 +++++++++++++++++++ .../java/net/kemitix/node/NodeException.java | 24 +++++++++++++++++++ src/main/java/net/kemitix/node/NodeItem.java | 24 +++++++++++++++++++ src/main/java/net/kemitix/node/Nodes.java | 24 +++++++++++++++++++ .../java/net/kemitix/node/package-info.java | 24 +++++++++++++++++++ 8 files changed, 170 insertions(+) rename LICENSE => LICENSE.txt (99%) diff --git a/LICENSE b/LICENSE.txt similarity index 99% rename from LICENSE rename to LICENSE.txt index c401abb..00c515a 100644 --- a/LICENSE +++ b/LICENSE.txt @@ -1,3 +1,4 @@ +/* The MIT License (MIT) 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, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +*/ diff --git a/src/main/java/net/kemitix/node/AbstractNodeItem.java b/src/main/java/net/kemitix/node/AbstractNodeItem.java index 1fe6eab..1c6077e 100644 --- a/src/main/java/net/kemitix/node/AbstractNodeItem.java +++ b/src/main/java/net/kemitix/node/AbstractNodeItem.java @@ -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; import lombok.NonNull; diff --git a/src/main/java/net/kemitix/node/ImmutableNodeItem.java b/src/main/java/net/kemitix/node/ImmutableNodeItem.java index dbc85ec..6947303 100644 --- a/src/main/java/net/kemitix/node/ImmutableNodeItem.java +++ b/src/main/java/net/kemitix/node/ImmutableNodeItem.java @@ -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; import java.util.List; diff --git a/src/main/java/net/kemitix/node/Node.java b/src/main/java/net/kemitix/node/Node.java index a5daf12..ea91047 100644 --- a/src/main/java/net/kemitix/node/Node.java +++ b/src/main/java/net/kemitix/node/Node.java @@ -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; import java.util.List; diff --git a/src/main/java/net/kemitix/node/NodeException.java b/src/main/java/net/kemitix/node/NodeException.java index 7f84643..2db0f92 100644 --- a/src/main/java/net/kemitix/node/NodeException.java +++ b/src/main/java/net/kemitix/node/NodeException.java @@ -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; /** diff --git a/src/main/java/net/kemitix/node/NodeItem.java b/src/main/java/net/kemitix/node/NodeItem.java index 12a7cef..8d0e499 100644 --- a/src/main/java/net/kemitix/node/NodeItem.java +++ b/src/main/java/net/kemitix/node/NodeItem.java @@ -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; import lombok.NonNull; diff --git a/src/main/java/net/kemitix/node/Nodes.java b/src/main/java/net/kemitix/node/Nodes.java index ecc5f39..13c8559 100644 --- a/src/main/java/net/kemitix/node/Nodes.java +++ b/src/main/java/net/kemitix/node/Nodes.java @@ -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; import java.util.Optional; diff --git a/src/main/java/net/kemitix/node/package-info.java b/src/main/java/net/kemitix/node/package-info.java index 382684c..49ea507 100644 --- a/src/main/java/net/kemitix/node/package-info.java +++ b/src/main/java/net/kemitix/node/package-info.java @@ -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. */ + package net.kemitix.node; From 8aa8abe3293bcd03e9ebb1e83dcb4497d0cdeade Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Tue, 13 Sep 2016 22:08:25 +0100 Subject: [PATCH 02/10] @author: add email address --- src/main/java/net/kemitix/node/AbstractNodeItem.java | 4 ++-- src/main/java/net/kemitix/node/ImmutableNodeItem.java | 4 ++-- src/main/java/net/kemitix/node/Node.java | 4 ++-- src/main/java/net/kemitix/node/NodeException.java | 2 +- src/main/java/net/kemitix/node/NodeItem.java | 4 ++-- src/main/java/net/kemitix/node/Nodes.java | 2 +- 6 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/main/java/net/kemitix/node/AbstractNodeItem.java b/src/main/java/net/kemitix/node/AbstractNodeItem.java index 1c6077e..a9d3aa0 100644 --- a/src/main/java/net/kemitix/node/AbstractNodeItem.java +++ b/src/main/java/net/kemitix/node/AbstractNodeItem.java @@ -35,9 +35,9 @@ import java.util.Set; * An abstract node item, providing default implementations for most read-only * operations. * - * @author Paul Campbell - * * @param the type of data stored in each node + * + * @author Paul Campbell (pcampbell@kemitix.net) */ abstract class AbstractNodeItem implements Node { diff --git a/src/main/java/net/kemitix/node/ImmutableNodeItem.java b/src/main/java/net/kemitix/node/ImmutableNodeItem.java index 6947303..b57468c 100644 --- a/src/main/java/net/kemitix/node/ImmutableNodeItem.java +++ b/src/main/java/net/kemitix/node/ImmutableNodeItem.java @@ -35,9 +35,9 @@ import java.util.Set; * getData()} they could then modify the original data within the node. This * wouldn't affect the integrity of the node tree structure, however.

* - * @author Paul Campbell - * * @param the type of data stored in each node + * + * @author Paul Campbell (pcampbell@kemitix.net) */ final class ImmutableNodeItem extends AbstractNodeItem { diff --git a/src/main/java/net/kemitix/node/Node.java b/src/main/java/net/kemitix/node/Node.java index ea91047..50f75a0 100644 --- a/src/main/java/net/kemitix/node/Node.java +++ b/src/main/java/net/kemitix/node/Node.java @@ -31,9 +31,9 @@ import java.util.Set; /** * An interface for tree node items. * - * @author Paul Campbell - * * @param the type of data held in each node + * + * @author Paul Campbell (pcampbell@kemitix.net) */ public interface Node { diff --git a/src/main/java/net/kemitix/node/NodeException.java b/src/main/java/net/kemitix/node/NodeException.java index 2db0f92..f401b6e 100644 --- a/src/main/java/net/kemitix/node/NodeException.java +++ b/src/main/java/net/kemitix/node/NodeException.java @@ -27,7 +27,7 @@ package net.kemitix.node; /** * Represents an error within the tree node. * - * @author pcampbell + * @author Paul Campbell (pcampbell@kemitix.net) */ @SuppressWarnings("serial") public class NodeException extends RuntimeException { diff --git a/src/main/java/net/kemitix/node/NodeItem.java b/src/main/java/net/kemitix/node/NodeItem.java index 8d0e499..92eb923 100644 --- a/src/main/java/net/kemitix/node/NodeItem.java +++ b/src/main/java/net/kemitix/node/NodeItem.java @@ -36,9 +36,9 @@ import java.util.Set; /** * Represents a tree of nodes. * - * @author Paul Campbell - * * @param the type of data stored in each node + * + * @author Paul Campbell (pcampbell@kemitix.net) */ class NodeItem implements Node { diff --git a/src/main/java/net/kemitix/node/Nodes.java b/src/main/java/net/kemitix/node/Nodes.java index 13c8559..41b90c7 100644 --- a/src/main/java/net/kemitix/node/Nodes.java +++ b/src/main/java/net/kemitix/node/Nodes.java @@ -31,7 +31,7 @@ import java.util.stream.Collectors; /** * Utility class for {@link Node} items. * - * @author pcampbell + * @author Paul Campbell (pcampbell@kemitix.net) */ public final class Nodes { From 915a63f1b0c8155670273297cf5ca05979423168 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Tue, 13 Sep 2016 22:12:38 +0100 Subject: [PATCH 03/10] Node{Item}: undo line wraps --- src/main/java/net/kemitix/node/Node.java | 6 ++---- src/main/java/net/kemitix/node/NodeItem.java | 3 +-- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/src/main/java/net/kemitix/node/Node.java b/src/main/java/net/kemitix/node/Node.java index 50f75a0..b00bf67 100644 --- a/src/main/java/net/kemitix/node/Node.java +++ b/src/main/java/net/kemitix/node/Node.java @@ -56,8 +56,7 @@ public interface Node { /** * Fetch the data held within the node. * - * @return an Optional containing the node's data, or empty if the node has - * none + * @return an Optional containing the node's data, or empty if the node has none */ Optional getData(); @@ -138,8 +137,7 @@ public interface Node { * * @return the found or created child node * - * @deprecated use {@code node.findChild(child).orElseGet(() -> - * node.createChild(child))}; + * @deprecated use {@code node.findChild(child).orElseGet(() -> node.createChild(child))}; */ @Deprecated Node findOrCreateChild(T child); diff --git a/src/main/java/net/kemitix/node/NodeItem.java b/src/main/java/net/kemitix/node/NodeItem.java index 92eb923..5d83553 100644 --- a/src/main/java/net/kemitix/node/NodeItem.java +++ b/src/main/java/net/kemitix/node/NodeItem.java @@ -206,8 +206,7 @@ class NodeItem implements Node { * * @return the found or created child node * - * @deprecated use node.findChild(child).orElseGet(() -> node.createChild - * (child)); + * @deprecated use node.findChild(child).orElseGet(() -> node.createChild (child)); */ @Override @Deprecated From 29648c9f701d6e6359c012c0311a26360cb7f7b0 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Tue, 13 Sep 2016 22:32:56 +0100 Subject: [PATCH 04/10] Code Style update - use wider limits --- .../net/kemitix/node/AbstractNodeItem.java | 31 +++---- .../net/kemitix/node/ImmutableNodeItem.java | 14 +-- src/main/java/net/kemitix/node/NodeItem.java | 89 ++++++++++--------- src/main/java/net/kemitix/node/Nodes.java | 23 ++--- 4 files changed, 81 insertions(+), 76 deletions(-) diff --git a/src/main/java/net/kemitix/node/AbstractNodeItem.java b/src/main/java/net/kemitix/node/AbstractNodeItem.java index a9d3aa0..2843652 100644 --- a/src/main/java/net/kemitix/node/AbstractNodeItem.java +++ b/src/main/java/net/kemitix/node/AbstractNodeItem.java @@ -41,17 +41,17 @@ import java.util.Set; */ abstract class AbstractNodeItem implements Node { + private final Set> children; + private T data; private String name; private Node parent; - private final Set> children; - protected AbstractNodeItem( - final T data, final String name, final Node parent, - final Set> children) { + final T data, final String name, final Node parent, final Set> children + ) { this.data = data; this.name = name; this.parent = parent; @@ -92,16 +92,18 @@ abstract class AbstractNodeItem implements Node { */ @Override public Optional> findChild(@NonNull final T child) { - return children.stream().filter(node -> { - final Optional d = node.getData(); - return d.isPresent() && d.get().equals(child); - }).findAny(); + return children.stream() + .filter(node -> { + final Optional d = node.getData(); + return d.isPresent() && d.get() + .equals(child); + }) + .findAny(); } @Override public Node getChild(final T child) { - return findChild(child).orElseThrow( - () -> new NodeException("Child not found")); + return findChild(child).orElseThrow(() -> new NodeException("Child not found")); } /** @@ -113,8 +115,7 @@ abstract class AbstractNodeItem implements Node { */ @Override public boolean isDescendantOf(final Node node) { - return parent != null && (node.equals(parent) || parent.isDescendantOf( - node)); + return parent != null && (node.equals(parent) || parent.isDescendantOf(node)); } /** @@ -145,14 +146,14 @@ abstract class AbstractNodeItem implements Node { @Override public Optional> findChildByName(@NonNull final String named) { return children.stream() - .filter(n -> n.getName().equals(named)) + .filter(n -> n.getName() + .equals(named)) .findAny(); } @Override public Node getChildByName(final String named) { - return findChildByName(named).orElseThrow( - () -> new NodeException("Named child not found")); + return findChildByName(named).orElseThrow(() -> new NodeException("Named child not found")); } @Override diff --git a/src/main/java/net/kemitix/node/ImmutableNodeItem.java b/src/main/java/net/kemitix/node/ImmutableNodeItem.java index b57468c..020cdb9 100644 --- a/src/main/java/net/kemitix/node/ImmutableNodeItem.java +++ b/src/main/java/net/kemitix/node/ImmutableNodeItem.java @@ -44,19 +44,20 @@ final class ImmutableNodeItem extends AbstractNodeItem { private static final String IMMUTABLE_OBJECT = "Immutable object"; private ImmutableNodeItem( - final T data, final String name, final Node parent, - final Set> children) { + final T data, final String name, final Node parent, final Set> children + ) { super(data, name, parent, children); } static ImmutableNodeItem newRoot( - final T data, final String name, final Set> children) { + final T data, final String name, final Set> children + ) { return new ImmutableNodeItem<>(data, name, null, children); } static ImmutableNodeItem newChild( - final T data, final String name, final Node parent, - final Set> children) { + final T data, final String name, final Node parent, final Set> children + ) { return new ImmutableNodeItem<>(data, name, parent, children); } @@ -97,8 +98,7 @@ final class ImmutableNodeItem extends AbstractNodeItem { @Override public Node findOrCreateChild(final T child) { - return findChild(child).orElseThrow( - () -> new UnsupportedOperationException(IMMUTABLE_OBJECT)); + return findChild(child).orElseThrow(() -> new UnsupportedOperationException(IMMUTABLE_OBJECT)); } @Override diff --git a/src/main/java/net/kemitix/node/NodeItem.java b/src/main/java/net/kemitix/node/NodeItem.java index 5d83553..5afea63 100644 --- a/src/main/java/net/kemitix/node/NodeItem.java +++ b/src/main/java/net/kemitix/node/NodeItem.java @@ -42,10 +42,10 @@ import java.util.Set; */ class NodeItem implements Node { - private T data; - private final Set> children = new HashSet<>(); + private T data; + private Node parent; private String name; @@ -124,6 +124,24 @@ class NodeItem implements Node { 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 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 public Set> getChildren() { return children; @@ -141,20 +159,20 @@ class NodeItem implements Node { children.add(child); // update the child's parent if they don't have one or it is not this val childParent = child.getParent(); - if (!childParent.isPresent() || !childParent.get().equals(this)) { + if (!childParent.isPresent() || !childParent.get() + .equals(this)) { child.setParent(this); } } private void verifyChildWithSameNameDoesNotAlreadyExist( - final @NonNull Node child) { + final @NonNull Node child + ) { if (child.isNamed()) { - findChildByName(child.getName()) - .filter(existingChild -> existingChild != child) - .ifPresent(existingChild -> { - throw new NodeException( - "Node with that name already exists here"); - }); + findChildByName(child.getName()).filter(existingChild -> existingChild != child) + .ifPresent(existingChild -> { + throw new NodeException("Node with that name already exists here"); + }); } } @@ -193,8 +211,7 @@ class NodeItem implements Node { @Override public void createDescendantLine(@NonNull final List descendants) { if (!descendants.isEmpty()) { - findOrCreateChild(descendants.get(0)).createDescendantLine( - descendants.subList(1, descendants.size())); + findOrCreateChild(descendants.get(0)).createDescendantLine(descendants.subList(1, descendants.size())); } } @@ -223,10 +240,13 @@ class NodeItem implements Node { */ @Override public Optional> findChild(@NonNull final T child) { - return children.stream().filter(node -> { - final Optional d = node.getData(); - return d.isPresent() && d.get().equals(child); - }).findAny(); + return children.stream() + .filter(node -> { + final Optional d = node.getData(); + return d.isPresent() && d.get() + .equals(child); + }) + .findAny(); } @Override @@ -247,25 +267,7 @@ class NodeItem implements Node { */ @Override public boolean isDescendantOf(final Node node) { - return parent != null && (node.equals(parent) || parent.isDescendantOf( - 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 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); + return parent != null && (node.equals(parent) || parent.isDescendantOf(node)); } /** @@ -299,10 +301,8 @@ class NodeItem implements Node { insertChild(nodeItem); } else { val item = path[0]; - findChildByName(item) - .orElseGet(() -> new NodeItem<>(null, item, this)) - .insertInPath(nodeItem, - Arrays.copyOfRange(path, 1, path.length)); + findChildByName(item).orElseGet(() -> new NodeItem<>(null, item, this)) + .insertInPath(nodeItem, Arrays.copyOfRange(path, 1, path.length)); } } @@ -322,10 +322,10 @@ class NodeItem implements Node { val existing = childByName.get(); if (existing.isEmpty()) { // place any data in the new node into the existing empty node - nodeItem.getData().ifPresent(existing::setData); + nodeItem.getData() + .ifPresent(existing::setData); } else { - throw new NodeException("A non-empty node named '" - + nodeItem.getName() + "' already exists here"); + throw new NodeException("A non-empty node named '" + nodeItem.getName() + "' already exists here"); } } else { // nothing with the same name exists @@ -336,8 +336,9 @@ class NodeItem implements Node { @Override public Optional> findChildByName(@NonNull final String named) { return children.stream() - .filter((Node t) -> t.getName().equals(named)) - .findAny(); + .filter((Node t) -> t.getName() + .equals(named)) + .findAny(); } @Override diff --git a/src/main/java/net/kemitix/node/Nodes.java b/src/main/java/net/kemitix/node/Nodes.java index 41b90c7..7093c07 100644 --- a/src/main/java/net/kemitix/node/Nodes.java +++ b/src/main/java/net/kemitix/node/Nodes.java @@ -87,7 +87,8 @@ public final class Nodes { * @return the new node */ public static Node namedChild( - final T data, final String name, final Node parent) { + final T data, final String name, final Node parent + ) { return new NodeItem<>(data, name, parent); } @@ -100,12 +101,13 @@ public final class Nodes { * @return the immutable copy of the tree */ public static Node asImmutable(final Node root) { - if (root.getParent().isPresent()) { + if (root.getParent() + .isPresent()) { throw new IllegalArgumentException("source must be the root node"); } final Set> children = getImmutableChildren(root); - return ImmutableNodeItem.newRoot(root.getData().orElse(null), - root.getName(), children); + return ImmutableNodeItem.newRoot(root.getData() + .orElse(null), root.getName(), children); } private static Set> getImmutableChildren(final Node source) { @@ -116,15 +118,16 @@ public final class Nodes { } private static Node asImmutableChild( - final Node source) { + final Node source + ) { final Optional> sourceParent = source.getParent(); if (sourceParent.isPresent()) { - return ImmutableNodeItem.newChild(source.getData().orElse(null), - source.getName(), sourceParent.get(), - getImmutableChildren(source)); + return ImmutableNodeItem.newChild(source.getData() + .orElse(null), source.getName(), sourceParent.get(), + getImmutableChildren(source) + ); } else { - throw new IllegalArgumentException( - "source must not be the root node"); + throw new IllegalArgumentException("source must not be the root node"); } } From d32221c6495c4d7937df30b057541606a8ce485e Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Tue, 13 Sep 2016 22:37:24 +0100 Subject: [PATCH 05/10] ImmutableNodeItem: add javadoc --- .../net/kemitix/node/ImmutableNodeItem.java | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/main/java/net/kemitix/node/ImmutableNodeItem.java b/src/main/java/net/kemitix/node/ImmutableNodeItem.java index 020cdb9..009d2af 100644 --- a/src/main/java/net/kemitix/node/ImmutableNodeItem.java +++ b/src/main/java/net/kemitix/node/ImmutableNodeItem.java @@ -49,12 +49,33 @@ final class ImmutableNodeItem extends AbstractNodeItem { 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 the type of the data in the node + * + * @return the new node tree's root node + */ static ImmutableNodeItem newRoot( final T data, final String name, final Set> 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 the type of the data in the node + * + * @return the new immutable node + */ static ImmutableNodeItem newChild( final T data, final String name, final Node parent, final Set> children ) { From 99db9e2a1a78690f5915de8a5425054e89089ed2 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Tue, 13 Sep 2016 22:38:49 +0100 Subject: [PATCH 06/10] Node: remove undeclared runtime exceptions from javadoc --- src/main/java/net/kemitix/node/Node.java | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/main/java/net/kemitix/node/Node.java b/src/main/java/net/kemitix/node/Node.java index b00bf67..029d536 100644 --- a/src/main/java/net/kemitix/node/Node.java +++ b/src/main/java/net/kemitix/node/Node.java @@ -157,8 +157,6 @@ public interface Node { * @param child the child's data to search for * * @return the child node if found - * - * @throws NodeException if the node is not found */ Node getChild(T child); @@ -205,8 +203,6 @@ public interface Node { * @param name the name of the child * * @return the node - * - * @throws NodeException if the node is not found */ Node getChildByName(String name); From 4770a9998333b63d5cfaf19b7831a4c1ec1705ea Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Tue, 13 Sep 2016 22:41:01 +0100 Subject: [PATCH 07/10] AbstractNodeItem: add javadoc for constructor --- src/main/java/net/kemitix/node/AbstractNodeItem.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/net/kemitix/node/AbstractNodeItem.java b/src/main/java/net/kemitix/node/AbstractNodeItem.java index 2843652..b11ba08 100644 --- a/src/main/java/net/kemitix/node/AbstractNodeItem.java +++ b/src/main/java/net/kemitix/node/AbstractNodeItem.java @@ -50,7 +50,7 @@ abstract class AbstractNodeItem implements Node { private Node parent; protected AbstractNodeItem( - final T data, final String name, final Node parent, final Set> children + final T data, final String name, final Node parent, @NonNull final Set> children ) { this.data = data; this.name = name; From afdaa082cc6c6e36b628af76b7e3ea06b3147d48 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Tue, 13 Sep 2016 22:41:24 +0100 Subject: [PATCH 08/10] AbsractNodeItem: constructor children must not be null --- src/main/java/net/kemitix/node/AbstractNodeItem.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/main/java/net/kemitix/node/AbstractNodeItem.java b/src/main/java/net/kemitix/node/AbstractNodeItem.java index b11ba08..7954f1d 100644 --- a/src/main/java/net/kemitix/node/AbstractNodeItem.java +++ b/src/main/java/net/kemitix/node/AbstractNodeItem.java @@ -49,6 +49,14 @@ abstract class AbstractNodeItem implements Node { private Node parent; + /** + * 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( final T data, final String name, final Node parent, @NonNull final Set> children ) { From 62d9d28fe14f1cd37422156948d6dd02cdf4317b Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Tue, 13 Sep 2016 22:47:41 +0100 Subject: [PATCH 09/10] {Abstract}NodeItem: reduce complexity of findInPath() --- src/main/java/net/kemitix/node/AbstractNodeItem.java | 11 +++-------- src/main/java/net/kemitix/node/NodeItem.java | 11 +++-------- 2 files changed, 6 insertions(+), 16 deletions(-) diff --git a/src/main/java/net/kemitix/node/AbstractNodeItem.java b/src/main/java/net/kemitix/node/AbstractNodeItem.java index 7954f1d..5f6e5d5 100644 --- a/src/main/java/net/kemitix/node/AbstractNodeItem.java +++ b/src/main/java/net/kemitix/node/AbstractNodeItem.java @@ -139,14 +139,9 @@ abstract class AbstractNodeItem implements Node { return Optional.empty(); } Node current = this; - for (T item : path) { - final Optional> child = current.findChild(item); - if (child.isPresent()) { - current = child.get(); - } else { - current = null; - break; - } + for (int i = 0, pathSize = path.size(); i < pathSize && current != null; i++) { + current = current.findChild(path.get(i)) + .orElse(null); } return Optional.ofNullable(current); } diff --git a/src/main/java/net/kemitix/node/NodeItem.java b/src/main/java/net/kemitix/node/NodeItem.java index 5afea63..0e92dfb 100644 --- a/src/main/java/net/kemitix/node/NodeItem.java +++ b/src/main/java/net/kemitix/node/NodeItem.java @@ -283,14 +283,9 @@ class NodeItem implements Node { return Optional.empty(); } Node current = this; - for (T item : path) { - final Optional> child = current.findChild(item); - if (child.isPresent()) { - current = child.get(); - } else { - current = null; - break; - } + for (int i = 0, pathSize = path.size(); i < pathSize && current != null; i++) { + current = current.findChild(path.get(i)) + .orElse(null); } return Optional.ofNullable(current); } From df5ee5224b4964863d0a0c5166236be3a8239b2a Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Tue, 13 Sep 2016 22:47:59 +0100 Subject: [PATCH 10/10] pom.xml: upgrade kemitix-parent to 2.1.0 --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 791ea8f..27b0244 100644 --- a/pom.xml +++ b/pom.xml @@ -11,7 +11,7 @@ net.kemitix kemitix-parent - 2.0.0 + 2.1.0