From 29648c9f701d6e6359c012c0311a26360cb7f7b0 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Tue, 13 Sep 2016 22:32:56 +0100 Subject: [PATCH] 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"); } }