From 3db3c0287e9467f6e68d162bb0adb6f5d5891404 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Tue, 13 Sep 2016 07:39:06 +0100 Subject: [PATCH 01/30] pom.xml: version set to 0.5.0-SNAPSHOT --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 4835b9d..ac5062a 100644 --- a/pom.xml +++ b/pom.xml @@ -2,7 +2,7 @@ 4.0.0 node - 0.4.0 + 0.5.0-SNAPSHOT jar Node From cf347c92ed2adce6fbe4d7abdbb33a27d8b25673 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Tue, 13 Sep 2016 08:09:04 +0100 Subject: [PATCH 02/30] pom.xml: upgrade assertj to 3.5.2 --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index ac5062a..baa58ba 100644 --- a/pom.xml +++ b/pom.xml @@ -15,7 +15,7 @@ - 3.4.1 + 3.5.2 4.2.0 2.1.0 From 83d9bc50fd75097e1515ea6247145b06e1d90e5b Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Tue, 13 Sep 2016 20:06:13 +0100 Subject: [PATCH 03/30] pom.xml: upgrade lombok to 1.16.10 --- pom.xml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index baa58ba..791ea8f 100644 --- a/pom.xml +++ b/pom.xml @@ -15,6 +15,7 @@ + 1.16.10 3.5.2 4.2.0 2.1.0 @@ -49,7 +50,7 @@ org.projectlombok lombok - 1.16.8 + ${lombok.version} junit From a39fa049c2c24416af9d9512f2d8205ea71c7626 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Tue, 13 Sep 2016 22:06:29 +0100 Subject: [PATCH 04/30] 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 05/30] @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 06/30] 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 07/30] 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 08/30] 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 09/30] 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 10/30] 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 11/30] 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 12/30] {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 13/30] 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 From 7f0f0d1bf9c33576ea5edf9c3a772ddc724a098c Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Tue, 20 Sep 2016 22:12:29 +0100 Subject: [PATCH 14/30] NodeItem: add all args constructor --- src/main/java/net/kemitix/node/NodeItem.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/main/java/net/kemitix/node/NodeItem.java b/src/main/java/net/kemitix/node/NodeItem.java index 0e92dfb..c3d87f7 100644 --- a/src/main/java/net/kemitix/node/NodeItem.java +++ b/src/main/java/net/kemitix/node/NodeItem.java @@ -50,6 +50,23 @@ class NodeItem implements Node { private String name; + /** + * 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 + */ + NodeItem( + final T data, final String name, final Node parent, @NonNull final Set> children + ) { + this.data = data; + this.name = name; + this.parent = parent; + this.children.addAll(children); + } + /** * Create named root node. * From dda6acc047f945b2fcaeaa9973f227212bf777da Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Tue, 20 Sep 2016 22:17:00 +0100 Subject: [PATCH 15/30] Nodes: use new NodeItem constructor --- src/main/java/net/kemitix/node/NodeItem.java | 2 +- src/main/java/net/kemitix/node/Nodes.java | 9 +++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/main/java/net/kemitix/node/NodeItem.java b/src/main/java/net/kemitix/node/NodeItem.java index c3d87f7..86c66ed 100644 --- a/src/main/java/net/kemitix/node/NodeItem.java +++ b/src/main/java/net/kemitix/node/NodeItem.java @@ -208,7 +208,7 @@ class NodeItem implements Node { */ @Override public Node createChild(@NonNull final T child) { - return new NodeItem<>(child, this); + return new NodeItem<>(child, "", this, new HashSet<>()); } @Override diff --git a/src/main/java/net/kemitix/node/Nodes.java b/src/main/java/net/kemitix/node/Nodes.java index 7093c07..0a6fa96 100644 --- a/src/main/java/net/kemitix/node/Nodes.java +++ b/src/main/java/net/kemitix/node/Nodes.java @@ -24,6 +24,7 @@ SOFTWARE. package net.kemitix.node; +import java.util.HashSet; import java.util.Optional; import java.util.Set; import java.util.stream.Collectors; @@ -47,7 +48,7 @@ public final class Nodes { * @return the new node */ public static Node unnamedRoot(final T data) { - return new NodeItem<>(data); + return new NodeItem<>(data, "", null, new HashSet<>()); } /** @@ -60,7 +61,7 @@ public final class Nodes { * @return the new node */ public static Node namedRoot(final T data, final String name) { - return new NodeItem<>(data, name); + return new NodeItem<>(data, name, null, new HashSet<>()); } /** @@ -73,7 +74,7 @@ public final class Nodes { * @return the new node */ public static Node unnamedChild(final T data, final Node parent) { - return new NodeItem<>(data, parent); + return new NodeItem<>(data, "", parent, new HashSet<>()); } /** @@ -89,7 +90,7 @@ public final class Nodes { public static Node namedChild( final T data, final String name, final Node parent ) { - return new NodeItem<>(data, name, parent); + return new NodeItem<>(data, name, parent, new HashSet<>()); } /** From f34f2a72009144f9689a2b1d24ea017198f5b9b9 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Tue, 20 Sep 2016 22:17:15 +0100 Subject: [PATCH 16/30] NodeItem: remove old constructors --- src/main/java/net/kemitix/node/NodeItem.java | 44 -------------------- 1 file changed, 44 deletions(-) diff --git a/src/main/java/net/kemitix/node/NodeItem.java b/src/main/java/net/kemitix/node/NodeItem.java index 86c66ed..b4a6eeb 100644 --- a/src/main/java/net/kemitix/node/NodeItem.java +++ b/src/main/java/net/kemitix/node/NodeItem.java @@ -67,50 +67,6 @@ class NodeItem implements Node { this.children.addAll(children); } - /** - * Create named root node. - * - * @param data the data or null - * @param name the name - */ - NodeItem(final T data, final String name) { - this(data); - 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 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 parent) { - this.data = data; - this.name = name; - setParent(parent); - } - @Override public String getName() { return name; From f9aba88d0cf97c9dd448d9cb273f746fef2d53c6 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Tue, 20 Sep 2016 22:18:00 +0100 Subject: [PATCH 17/30] NodeItem::getChild: simplify method --- src/main/java/net/kemitix/node/NodeItem.java | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/main/java/net/kemitix/node/NodeItem.java b/src/main/java/net/kemitix/node/NodeItem.java index b4a6eeb..ea26858 100644 --- a/src/main/java/net/kemitix/node/NodeItem.java +++ b/src/main/java/net/kemitix/node/NodeItem.java @@ -224,11 +224,7 @@ class NodeItem implements Node { @Override public Node getChild(final T child) { - Optional> optional = findChild(child); - if (optional.isPresent()) { - return optional.get(); - } - throw new NodeException("Child not found"); + return findChild(child).orElseThrow(() -> new NodeException("Child not found")); } /** From 753b37cc1dfed14cadc4835653196e38e7d95838 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Tue, 20 Sep 2016 22:24:28 +0100 Subject: [PATCH 18/30] NodeItem::insertInPath: clarify local variable name --- src/main/java/net/kemitix/node/NodeItem.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/net/kemitix/node/NodeItem.java b/src/main/java/net/kemitix/node/NodeItem.java index ea26858..a0c330d 100644 --- a/src/main/java/net/kemitix/node/NodeItem.java +++ b/src/main/java/net/kemitix/node/NodeItem.java @@ -264,9 +264,9 @@ class NodeItem implements Node { if (path.length == 0) { insertChild(nodeItem); } else { - val item = path[0]; - findChildByName(item).orElseGet(() -> new NodeItem<>(null, item, this)) - .insertInPath(nodeItem, Arrays.copyOfRange(path, 1, path.length)); + val nextInPath = path[0]; + findChildByName(nextInPath).orElseGet(() -> new NodeItem<>(null, nextInPath, this, new HashSet<>())) + .insertInPath(nodeItem, Arrays.copyOfRange(path, 1, path.length)); } } From 77a7946e9c9b4d652037334248665d9c6289d2cb Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Tue, 20 Sep 2016 22:26:49 +0100 Subject: [PATCH 19/30] NodeItem::findChild: improved Optional idiom --- src/main/java/net/kemitix/node/NodeItem.java | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/main/java/net/kemitix/node/NodeItem.java b/src/main/java/net/kemitix/node/NodeItem.java index a0c330d..f4f69b6 100644 --- a/src/main/java/net/kemitix/node/NodeItem.java +++ b/src/main/java/net/kemitix/node/NodeItem.java @@ -214,12 +214,9 @@ 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(); + .filter(node -> child.equals(node.getData() + .orElseGet(() -> null))) + .findFirst(); } @Override From d73a46b9054235ec73f846a3c339174241ffd01a Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Tue, 20 Sep 2016 22:39:30 +0100 Subject: [PATCH 20/30] NodeItemTest: check that child is added to parent --- src/test/java/net/kemitix/node/NodeItemTest.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/test/java/net/kemitix/node/NodeItemTest.java b/src/test/java/net/kemitix/node/NodeItemTest.java index 2702ae0..653ada5 100644 --- a/src/test/java/net/kemitix/node/NodeItemTest.java +++ b/src/test/java/net/kemitix/node/NodeItemTest.java @@ -10,6 +10,7 @@ import org.junit.rules.ExpectedException; import java.util.Arrays; import java.util.Collections; import java.util.Optional; +import java.util.Set; import static org.assertj.core.api.Assertions.assertThat; @@ -84,6 +85,17 @@ public class NodeItemTest { .contains(parent); } + @Test + public void shouldAddAsChildWhenCreatedWithParent() { + //given + final Node root = Nodes.namedRoot("root data", "root name"); + //when + final Node child = Nodes.namedChild("child data", "child name", root); + //then + final Set> children = root.getChildren(); + assertThat(children).containsExactly(child); + } + /** * Test that setting the parent on a node where the proposed parent is a * child of the node throws an exception. From 93a3f2ad4252c177f1ca47ab5d880dde744c5261 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Tue, 20 Sep 2016 22:39:54 +0100 Subject: [PATCH 21/30] NodesTest: unnamed nodes have an empty string --- src/test/java/net/kemitix/node/NodesTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/java/net/kemitix/node/NodesTest.java b/src/test/java/net/kemitix/node/NodesTest.java index ace4f79..6c9aee4 100644 --- a/src/test/java/net/kemitix/node/NodesTest.java +++ b/src/test/java/net/kemitix/node/NodesTest.java @@ -24,7 +24,7 @@ public class NodesTest { val node = Nodes.unnamedRoot("data"); SoftAssertions softly = new SoftAssertions(); softly.assertThat(node.getData()).contains("data"); - softly.assertThat(node.getName()).isNull(); + softly.assertThat(node.getName()).isEmpty(); softly.assertAll(); } @@ -43,7 +43,7 @@ public class NodesTest { val node = Nodes.unnamedChild("data", parent); SoftAssertions softly = new SoftAssertions(); softly.assertThat(node.getData()).contains("data"); - softly.assertThat(node.getName()).isNull(); + softly.assertThat(node.getName()).isEmpty(); softly.assertThat(node.getParent()).contains(parent); softly.assertAll(); } From ff3a45665798fdad0344d7e891e1e812367e1e01 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Tue, 20 Sep 2016 22:40:32 +0100 Subject: [PATCH 22/30] NodeItem: new constructor sets parent properly --- src/main/java/net/kemitix/node/NodeItem.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/java/net/kemitix/node/NodeItem.java b/src/main/java/net/kemitix/node/NodeItem.java index f4f69b6..efdb49a 100644 --- a/src/main/java/net/kemitix/node/NodeItem.java +++ b/src/main/java/net/kemitix/node/NodeItem.java @@ -63,7 +63,9 @@ class NodeItem implements Node { ) { this.data = data; this.name = name; - this.parent = parent; + if (parent != null) { + setParent(parent); + } this.children.addAll(children); } From 48c5b157204498562615b0845ad1652d62a0099b Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Tue, 20 Sep 2016 22:59:50 +0100 Subject: [PATCH 23/30] ImmutableNodeItem: now extends NodeItem The AbstractNodeItem is now redundant and is removed. --- .../net/kemitix/node/AbstractNodeItem.java | 183 ------------------ .../net/kemitix/node/ImmutableNodeItem.java | 6 +- src/main/java/net/kemitix/node/NodeItem.java | 6 +- 3 files changed, 8 insertions(+), 187 deletions(-) delete mode 100644 src/main/java/net/kemitix/node/AbstractNodeItem.java diff --git a/src/main/java/net/kemitix/node/AbstractNodeItem.java b/src/main/java/net/kemitix/node/AbstractNodeItem.java deleted file mode 100644 index 5f6e5d5..0000000 --- a/src/main/java/net/kemitix/node/AbstractNodeItem.java +++ /dev/null @@ -1,183 +0,0 @@ -/* -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; - -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. - * - * @param the type of data stored in each node - * - * @author Paul Campbell (pcampbell@kemitix.net) - */ -abstract class AbstractNodeItem implements Node { - - private final Set> children; - - private T data; - - private String name; - - 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 - ) { - this.data = data; - this.name = name; - this.parent = parent; - this.children = children; - } - - @Override - public String getName() { - return name; - } - - @Override - public Optional getData() { - return Optional.ofNullable(data); - } - - @Override - public boolean isEmpty() { - return data == null; - } - - @Override - public Optional> getParent() { - return Optional.ofNullable(parent); - } - - @Override - public Set> 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> findChild(@NonNull final T child) { - 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")); - } - - /** - * 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 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> findInPath(@NonNull final List path) { - if (path.isEmpty()) { - return Optional.empty(); - } - Node current = this; - for (int i = 0, pathSize = path.size(); i < pathSize && current != null; i++) { - current = current.findChild(path.get(i)) - .orElse(null); - } - return Optional.ofNullable(current); - } - - @Override - public Optional> findChildByName(@NonNull final String named) { - return children.stream() - .filter(n -> n.getName() - .equals(named)) - .findAny(); - } - - @Override - public Node 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; - } -} diff --git a/src/main/java/net/kemitix/node/ImmutableNodeItem.java b/src/main/java/net/kemitix/node/ImmutableNodeItem.java index 009d2af..fc4d159 100644 --- a/src/main/java/net/kemitix/node/ImmutableNodeItem.java +++ b/src/main/java/net/kemitix/node/ImmutableNodeItem.java @@ -39,14 +39,15 @@ import java.util.Set; * * @author Paul Campbell (pcampbell@kemitix.net) */ -final class ImmutableNodeItem extends AbstractNodeItem { +final class ImmutableNodeItem extends NodeItem { private static final String IMMUTABLE_OBJECT = "Immutable object"; private ImmutableNodeItem( final T data, final String name, final Node parent, final Set> children ) { - super(data, name, parent, children); + super(data, name, null, children); + forceParent(parent); } /** @@ -136,5 +137,4 @@ final class ImmutableNodeItem extends AbstractNodeItem { public void removeParent() { throw new UnsupportedOperationException(IMMUTABLE_OBJECT); } - } diff --git a/src/main/java/net/kemitix/node/NodeItem.java b/src/main/java/net/kemitix/node/NodeItem.java index efdb49a..d933770 100644 --- a/src/main/java/net/kemitix/node/NodeItem.java +++ b/src/main/java/net/kemitix/node/NodeItem.java @@ -69,6 +69,10 @@ class NodeItem implements Node { this.children.addAll(children); } + protected void forceParent(final Node parent) { + this.parent = parent; + } + @Override public String getName() { return name; @@ -105,7 +109,7 @@ class NodeItem implements Node { * @param parent the new parent node */ @Override - public final void setParent(@NonNull final Node parent) { + public void setParent(@NonNull final Node parent) { if (this.equals(parent) || parent.isDescendantOf(this)) { throw new NodeException("Parent is a descendant"); } From c77f9c29d399ce71ee813d3c21c0c21d6b8bf9f8 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Tue, 20 Sep 2016 23:09:45 +0100 Subject: [PATCH 24/30] NodeItem: don't call overridable method from constructor --- src/main/java/net/kemitix/node/NodeItem.java | 21 ++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/main/java/net/kemitix/node/NodeItem.java b/src/main/java/net/kemitix/node/NodeItem.java index d933770..9921dc2 100644 --- a/src/main/java/net/kemitix/node/NodeItem.java +++ b/src/main/java/net/kemitix/node/NodeItem.java @@ -64,13 +64,18 @@ class NodeItem implements Node { this.data = data; this.name = name; if (parent != null) { - setParent(parent); + doSetParent(parent); } this.children.addAll(children); } - protected void forceParent(final Node parent) { - this.parent = parent; + /** + * 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 newParent) { + this.parent = newParent; } @Override @@ -110,15 +115,19 @@ class NodeItem implements Node { */ @Override public void setParent(@NonNull final Node parent) { - if (this.equals(parent) || parent.isDescendantOf(this)) { + doSetParent(parent); + } + + private void doSetParent(@NonNull final Node 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 = parent; - parent.addChild(this); + this.parent = newParent; + newParent.addChild(this); } @Override From 6a93ed9199db57337a0182a84025d3fcbb841533 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Tue, 20 Sep 2016 23:10:15 +0100 Subject: [PATCH 25/30] ImmutableNodeItemTest:: don't pass null to setParent in test --- src/test/java/net/kemitix/node/ImmutableNodeItemTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/net/kemitix/node/ImmutableNodeItemTest.java b/src/test/java/net/kemitix/node/ImmutableNodeItemTest.java index 91eee8a..c4c4bc7 100644 --- a/src/test/java/net/kemitix/node/ImmutableNodeItemTest.java +++ b/src/test/java/net/kemitix/node/ImmutableNodeItemTest.java @@ -126,7 +126,7 @@ public class ImmutableNodeItemTest { immutableNode = Nodes.asImmutable(Nodes.unnamedRoot("subject")); expectImmutableException(); //when - immutableNode.setParent(null); + immutableNode.setParent(Nodes.unnamedRoot("child")); } @Test From 76c4d4ceeba7c7e74af53cb692ade4759625f0cd Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Tue, 20 Sep 2016 23:10:33 +0100 Subject: [PATCH 26/30] Node: drop findOrCreateChild() --- .../net/kemitix/node/ImmutableNodeItem.java | 5 -- src/main/java/net/kemitix/node/Node.java | 13 ------ src/main/java/net/kemitix/node/NodeItem.java | 21 ++------- .../kemitix/node/ImmutableNodeItemTest.java | 33 ------------- .../java/net/kemitix/node/NodeItemTest.java | 46 ------------------- 5 files changed, 4 insertions(+), 114 deletions(-) diff --git a/src/main/java/net/kemitix/node/ImmutableNodeItem.java b/src/main/java/net/kemitix/node/ImmutableNodeItem.java index fc4d159..547dde3 100644 --- a/src/main/java/net/kemitix/node/ImmutableNodeItem.java +++ b/src/main/java/net/kemitix/node/ImmutableNodeItem.java @@ -118,11 +118,6 @@ final class ImmutableNodeItem extends NodeItem { throw new UnsupportedOperationException(IMMUTABLE_OBJECT); } - @Override - public Node findOrCreateChild(final T child) { - return findChild(child).orElseThrow(() -> new UnsupportedOperationException(IMMUTABLE_OBJECT)); - } - @Override public void insertInPath(final Node node, final String... path) { throw new UnsupportedOperationException(IMMUTABLE_OBJECT); diff --git a/src/main/java/net/kemitix/node/Node.java b/src/main/java/net/kemitix/node/Node.java index 029d536..e354227 100644 --- a/src/main/java/net/kemitix/node/Node.java +++ b/src/main/java/net/kemitix/node/Node.java @@ -129,19 +129,6 @@ public interface Node { */ void createDescendantLine(List 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 findOrCreateChild(T child); - /** * Fetches the node for the child if present. * diff --git a/src/main/java/net/kemitix/node/NodeItem.java b/src/main/java/net/kemitix/node/NodeItem.java index 9921dc2..e4d605a 100644 --- a/src/main/java/net/kemitix/node/NodeItem.java +++ b/src/main/java/net/kemitix/node/NodeItem.java @@ -199,26 +199,13 @@ 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())); + val child = descendants.get(0); + 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 findOrCreateChild(@NonNull final T child) { - return findChild(child).orElseGet(() -> createChild(child)); - } - /** * Fetches the node for the child if present. * diff --git a/src/test/java/net/kemitix/node/ImmutableNodeItemTest.java b/src/test/java/net/kemitix/node/ImmutableNodeItemTest.java index c4c4bc7..411e9e5 100644 --- a/src/test/java/net/kemitix/node/ImmutableNodeItemTest.java +++ b/src/test/java/net/kemitix/node/ImmutableNodeItemTest.java @@ -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. */ @@ -431,24 +418,4 @@ public class ImmutableNodeItemTest { //when immutableNode.insertInPath(null, ""); } - - @Test - public void findOrCreateChildShouldReturnChildWhenChildIsFound() { - //given - val root = Nodes.unnamedRoot(""); - Nodes.namedChild("child", "child", root); - immutableNode = Nodes.asImmutable(root); - //when - val found = immutableNode.findOrCreateChild("child"); - assertThat(found).extracting(Node::getName).contains("child"); - } - - @Test - public void findOrCreateChildShouldThrowExceptionWhenChildNotFound() { - //given - immutableNode = Nodes.asImmutable(Nodes.unnamedRoot("")); - expectImmutableException(); - //when - immutableNode.findOrCreateChild("child"); - } } diff --git a/src/test/java/net/kemitix/node/NodeItemTest.java b/src/test/java/net/kemitix/node/NodeItemTest.java index 653ada5..0a92e75 100644 --- a/src/test/java/net/kemitix/node/NodeItemTest.java +++ b/src/test/java/net/kemitix/node/NodeItemTest.java @@ -469,52 +469,6 @@ public class NodeItemTest { + "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. */ From 2226d61b46d77f3d4ba755dbf83f3d1d1b26aa59 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Tue, 20 Sep 2016 23:35:27 +0100 Subject: [PATCH 27/30] Nodes::asImmutableChild: use null parent if none found --- src/main/java/net/kemitix/node/Nodes.java | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/src/main/java/net/kemitix/node/Nodes.java b/src/main/java/net/kemitix/node/Nodes.java index 0a6fa96..ffa8f74 100644 --- a/src/main/java/net/kemitix/node/Nodes.java +++ b/src/main/java/net/kemitix/node/Nodes.java @@ -25,7 +25,6 @@ SOFTWARE. package net.kemitix.node; import java.util.HashSet; -import java.util.Optional; import java.util.Set; import java.util.stream.Collectors; @@ -121,15 +120,11 @@ public final class Nodes { private static Node asImmutableChild( final Node source ) { - final Optional> sourceParent = source.getParent(); - if (sourceParent.isPresent()) { - return ImmutableNodeItem.newChild(source.getData() - .orElse(null), source.getName(), sourceParent.get(), - getImmutableChildren(source) - ); - } else { - throw new IllegalArgumentException("source must not be the root node"); - } + return ImmutableNodeItem.newChild(source.getData() + .orElse(null), source.getName(), source.getParent() + .orElse(null), + getImmutableChildren(source) + ); } } From 012cf7cdbf2edec419f58b4bb0533a2771e515de Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Tue, 20 Sep 2016 23:35:55 +0100 Subject: [PATCH 28/30] ImmutableNodeItemTest: add test for Nodes::asImmutable for non-root --- .../java/net/kemitix/node/ImmutableNodeItemTest.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/test/java/net/kemitix/node/ImmutableNodeItemTest.java b/src/test/java/net/kemitix/node/ImmutableNodeItemTest.java index 411e9e5..3997ef3 100644 --- a/src/test/java/net/kemitix/node/ImmutableNodeItemTest.java +++ b/src/test/java/net/kemitix/node/ImmutableNodeItemTest.java @@ -418,4 +418,13 @@ public class ImmutableNodeItemTest { //when immutableNode.insertInPath(null, ""); } + + @Test + public void AsImmutableShouldThrowIAEWhenNotRoot() { + //given + exception.expect(IllegalArgumentException.class); + exception.expectMessage("source must be the root node"); + //when + Nodes.asImmutable(Nodes.unnamedChild("child", Nodes.unnamedRoot("root"))); + } } From fa5cbcdb13c8be956442f172163d4191acff6bda Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Tue, 20 Sep 2016 23:47:28 +0100 Subject: [PATCH 29/30] pom.xml: version set to 0.5.0 --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 27b0244..c7833d8 100644 --- a/pom.xml +++ b/pom.xml @@ -2,7 +2,7 @@ 4.0.0 node - 0.5.0-SNAPSHOT + 0.5.0 jar Node From fbeef959207aa844631c7e9fb083424f9999e764 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Tue, 20 Sep 2016 23:47:28 +0100 Subject: [PATCH 30/30] CHANGELOG --- CHANGELOG | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/CHANGELOG b/CHANGELOG index 5ee231c..fcf8fdd 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,15 @@ 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 ------