Code Style update - use wider limits

This commit is contained in:
Paul Campbell 2016-09-13 22:32:56 +01:00
parent 915a63f1b0
commit 29648c9f70
4 changed files with 81 additions and 76 deletions

View file

@ -41,17 +41,17 @@ import java.util.Set;
*/ */
abstract class AbstractNodeItem<T> implements Node<T> { abstract class AbstractNodeItem<T> implements Node<T> {
private final Set<Node<T>> children;
private T data; private T data;
private String name; private String name;
private Node<T> parent; private Node<T> parent;
private final Set<Node<T>> children;
protected AbstractNodeItem( protected AbstractNodeItem(
final T data, final String name, final Node<T> parent, final T data, final String name, final Node<T> parent, final Set<Node<T>> children
final Set<Node<T>> children) { ) {
this.data = data; this.data = data;
this.name = name; this.name = name;
this.parent = parent; this.parent = parent;
@ -92,16 +92,18 @@ abstract class AbstractNodeItem<T> implements Node<T> {
*/ */
@Override @Override
public Optional<Node<T>> findChild(@NonNull final T child) { public Optional<Node<T>> findChild(@NonNull final T child) {
return children.stream().filter(node -> { return children.stream()
final Optional<T> d = node.getData(); .filter(node -> {
return d.isPresent() && d.get().equals(child); final Optional<T> d = node.getData();
}).findAny(); return d.isPresent() && d.get()
.equals(child);
})
.findAny();
} }
@Override @Override
public Node<T> getChild(final T child) { public Node<T> getChild(final T child) {
return findChild(child).orElseThrow( return findChild(child).orElseThrow(() -> new NodeException("Child not found"));
() -> new NodeException("Child not found"));
} }
/** /**
@ -113,8 +115,7 @@ abstract class AbstractNodeItem<T> implements Node<T> {
*/ */
@Override @Override
public boolean isDescendantOf(final Node<T> node) { public boolean isDescendantOf(final Node<T> node) {
return parent != null && (node.equals(parent) || parent.isDescendantOf( return parent != null && (node.equals(parent) || parent.isDescendantOf(node));
node));
} }
/** /**
@ -145,14 +146,14 @@ abstract class AbstractNodeItem<T> implements Node<T> {
@Override @Override
public Optional<Node<T>> findChildByName(@NonNull final String named) { public Optional<Node<T>> findChildByName(@NonNull final String named) {
return children.stream() return children.stream()
.filter(n -> n.getName().equals(named)) .filter(n -> n.getName()
.equals(named))
.findAny(); .findAny();
} }
@Override @Override
public Node<T> getChildByName(final String named) { public Node<T> getChildByName(final String named) {
return findChildByName(named).orElseThrow( return findChildByName(named).orElseThrow(() -> new NodeException("Named child not found"));
() -> new NodeException("Named child not found"));
} }
@Override @Override

View file

@ -44,19 +44,20 @@ final class ImmutableNodeItem<T> extends AbstractNodeItem<T> {
private static final String IMMUTABLE_OBJECT = "Immutable object"; private static final String IMMUTABLE_OBJECT = "Immutable object";
private ImmutableNodeItem( private ImmutableNodeItem(
final T data, final String name, final Node<T> parent, final T data, final String name, final Node<T> parent, final Set<Node<T>> children
final Set<Node<T>> children) { ) {
super(data, name, parent, children); super(data, name, parent, children);
} }
static <T> ImmutableNodeItem<T> newRoot( static <T> ImmutableNodeItem<T> newRoot(
final T data, final String name, final Set<Node<T>> children) { final T data, final String name, final Set<Node<T>> children
) {
return new ImmutableNodeItem<>(data, name, null, children); return new ImmutableNodeItem<>(data, name, null, children);
} }
static <T> ImmutableNodeItem<T> newChild( static <T> ImmutableNodeItem<T> newChild(
final T data, final String name, final Node<T> parent, final T data, final String name, final Node<T> parent, final Set<Node<T>> children
final Set<Node<T>> children) { ) {
return new ImmutableNodeItem<>(data, name, parent, children); return new ImmutableNodeItem<>(data, name, parent, children);
} }
@ -97,8 +98,7 @@ final class ImmutableNodeItem<T> extends AbstractNodeItem<T> {
@Override @Override
public Node<T> findOrCreateChild(final T child) { public Node<T> findOrCreateChild(final T child) {
return findChild(child).orElseThrow( return findChild(child).orElseThrow(() -> new UnsupportedOperationException(IMMUTABLE_OBJECT));
() -> new UnsupportedOperationException(IMMUTABLE_OBJECT));
} }
@Override @Override

View file

@ -42,10 +42,10 @@ import java.util.Set;
*/ */
class NodeItem<T> implements Node<T> { class NodeItem<T> implements Node<T> {
private T data;
private final Set<Node<T>> children = new HashSet<>(); private final Set<Node<T>> children = new HashSet<>();
private T data;
private Node<T> parent; private Node<T> parent;
private String name; private String name;
@ -124,6 +124,24 @@ class NodeItem<T> implements Node<T> {
return Optional.ofNullable(parent); return Optional.ofNullable(parent);
} }
/**
* Make the current node a direct child of the parent.
*
* @param parent the new parent node
*/
@Override
public final void setParent(@NonNull final Node<T> parent) {
if (this.equals(parent) || parent.isDescendantOf(this)) {
throw new NodeException("Parent is a descendant");
}
if (this.parent != null) {
this.parent.getChildren()
.remove(this);
}
this.parent = parent;
parent.addChild(this);
}
@Override @Override
public Set<Node<T>> getChildren() { public Set<Node<T>> getChildren() {
return children; return children;
@ -141,20 +159,20 @@ class NodeItem<T> implements Node<T> {
children.add(child); children.add(child);
// update the child's parent if they don't have one or it is not this // update the child's parent if they don't have one or it is not this
val childParent = child.getParent(); val childParent = child.getParent();
if (!childParent.isPresent() || !childParent.get().equals(this)) { if (!childParent.isPresent() || !childParent.get()
.equals(this)) {
child.setParent(this); child.setParent(this);
} }
} }
private void verifyChildWithSameNameDoesNotAlreadyExist( private void verifyChildWithSameNameDoesNotAlreadyExist(
final @NonNull Node<T> child) { final @NonNull Node<T> child
) {
if (child.isNamed()) { if (child.isNamed()) {
findChildByName(child.getName()) findChildByName(child.getName()).filter(existingChild -> existingChild != child)
.filter(existingChild -> existingChild != child) .ifPresent(existingChild -> {
.ifPresent(existingChild -> { throw new NodeException("Node with that name already exists here");
throw new NodeException( });
"Node with that name already exists here");
});
} }
} }
@ -193,8 +211,7 @@ class NodeItem<T> implements Node<T> {
@Override @Override
public void createDescendantLine(@NonNull final List<T> descendants) { public void createDescendantLine(@NonNull final List<T> descendants) {
if (!descendants.isEmpty()) { if (!descendants.isEmpty()) {
findOrCreateChild(descendants.get(0)).createDescendantLine( findOrCreateChild(descendants.get(0)).createDescendantLine(descendants.subList(1, descendants.size()));
descendants.subList(1, descendants.size()));
} }
} }
@ -223,10 +240,13 @@ class NodeItem<T> implements Node<T> {
*/ */
@Override @Override
public Optional<Node<T>> findChild(@NonNull final T child) { public Optional<Node<T>> findChild(@NonNull final T child) {
return children.stream().filter(node -> { return children.stream()
final Optional<T> d = node.getData(); .filter(node -> {
return d.isPresent() && d.get().equals(child); final Optional<T> d = node.getData();
}).findAny(); return d.isPresent() && d.get()
.equals(child);
})
.findAny();
} }
@Override @Override
@ -247,25 +267,7 @@ class NodeItem<T> implements Node<T> {
*/ */
@Override @Override
public boolean isDescendantOf(final Node<T> node) { public boolean isDescendantOf(final Node<T> node) {
return parent != null && (node.equals(parent) || parent.isDescendantOf( return parent != null && (node.equals(parent) || parent.isDescendantOf(node));
node));
}
/**
* Make the current node a direct child of the parent.
*
* @param parent the new parent node
*/
@Override
public final void setParent(@NonNull final Node<T> parent) {
if (this.equals(parent) || parent.isDescendantOf(this)) {
throw new NodeException("Parent is a descendant");
}
if (this.parent != null) {
this.parent.getChildren().remove(this);
}
this.parent = parent;
parent.addChild(this);
} }
/** /**
@ -299,10 +301,8 @@ class NodeItem<T> implements Node<T> {
insertChild(nodeItem); insertChild(nodeItem);
} else { } else {
val item = path[0]; val item = path[0];
findChildByName(item) findChildByName(item).orElseGet(() -> new NodeItem<>(null, item, this))
.orElseGet(() -> new NodeItem<>(null, item, this)) .insertInPath(nodeItem, Arrays.copyOfRange(path, 1, path.length));
.insertInPath(nodeItem,
Arrays.copyOfRange(path, 1, path.length));
} }
} }
@ -322,10 +322,10 @@ class NodeItem<T> implements Node<T> {
val existing = childByName.get(); val existing = childByName.get();
if (existing.isEmpty()) { if (existing.isEmpty()) {
// place any data in the new node into the existing empty node // place any data in the new node into the existing empty node
nodeItem.getData().ifPresent(existing::setData); nodeItem.getData()
.ifPresent(existing::setData);
} else { } else {
throw new NodeException("A non-empty node named '" throw new NodeException("A non-empty node named '" + nodeItem.getName() + "' already exists here");
+ nodeItem.getName() + "' already exists here");
} }
} else { } else {
// nothing with the same name exists // nothing with the same name exists
@ -336,8 +336,9 @@ class NodeItem<T> implements Node<T> {
@Override @Override
public Optional<Node<T>> findChildByName(@NonNull final String named) { public Optional<Node<T>> findChildByName(@NonNull final String named) {
return children.stream() return children.stream()
.filter((Node<T> t) -> t.getName().equals(named)) .filter((Node<T> t) -> t.getName()
.findAny(); .equals(named))
.findAny();
} }
@Override @Override

View file

@ -87,7 +87,8 @@ public final class Nodes {
* @return the new node * @return the new node
*/ */
public static <T> Node<T> namedChild( public static <T> Node<T> namedChild(
final T data, final String name, final Node<T> parent) { final T data, final String name, final Node<T> parent
) {
return new NodeItem<>(data, name, parent); return new NodeItem<>(data, name, parent);
} }
@ -100,12 +101,13 @@ public final class Nodes {
* @return the immutable copy of the tree * @return the immutable copy of the tree
*/ */
public static <T> Node<T> asImmutable(final Node<T> root) { public static <T> Node<T> asImmutable(final Node<T> root) {
if (root.getParent().isPresent()) { if (root.getParent()
.isPresent()) {
throw new IllegalArgumentException("source must be the root node"); throw new IllegalArgumentException("source must be the root node");
} }
final Set<Node<T>> children = getImmutableChildren(root); final Set<Node<T>> children = getImmutableChildren(root);
return ImmutableNodeItem.newRoot(root.getData().orElse(null), return ImmutableNodeItem.newRoot(root.getData()
root.getName(), children); .orElse(null), root.getName(), children);
} }
private static <T> Set<Node<T>> getImmutableChildren(final Node<T> source) { private static <T> Set<Node<T>> getImmutableChildren(final Node<T> source) {
@ -116,15 +118,16 @@ public final class Nodes {
} }
private static <T> Node<T> asImmutableChild( private static <T> Node<T> asImmutableChild(
final Node<T> source) { final Node<T> source
) {
final Optional<Node<T>> sourceParent = source.getParent(); final Optional<Node<T>> sourceParent = source.getParent();
if (sourceParent.isPresent()) { if (sourceParent.isPresent()) {
return ImmutableNodeItem.newChild(source.getData().orElse(null), return ImmutableNodeItem.newChild(source.getData()
source.getName(), sourceParent.get(), .orElse(null), source.getName(), sourceParent.get(),
getImmutableChildren(source)); getImmutableChildren(source)
);
} else { } else {
throw new IllegalArgumentException( throw new IllegalArgumentException("source must not be the root node");
"source must not be the root node");
} }
} }