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> {
private final Set<Node<T>> children;
private T data;
private String name;
private Node<T> parent;
private final Set<Node<T>> children;
protected AbstractNodeItem(
final T data, final String name, final Node<T> parent,
final Set<Node<T>> children) {
final T data, final String name, final Node<T> parent, final Set<Node<T>> children
) {
this.data = data;
this.name = name;
this.parent = parent;
@ -92,16 +92,18 @@ abstract class AbstractNodeItem<T> implements Node<T> {
*/
@Override
public Optional<Node<T>> findChild(@NonNull final T child) {
return children.stream().filter(node -> {
return children.stream()
.filter(node -> {
final Optional<T> d = node.getData();
return d.isPresent() && d.get().equals(child);
}).findAny();
return d.isPresent() && d.get()
.equals(child);
})
.findAny();
}
@Override
public Node<T> 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<T> implements Node<T> {
*/
@Override
public boolean isDescendantOf(final Node<T> 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<T> implements Node<T> {
@Override
public Optional<Node<T>> findChildByName(@NonNull final String named) {
return children.stream()
.filter(n -> n.getName().equals(named))
.filter(n -> n.getName()
.equals(named))
.findAny();
}
@Override
public Node<T> 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

View file

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

View file

@ -42,10 +42,10 @@ import java.util.Set;
*/
class NodeItem<T> implements Node<T> {
private T data;
private final Set<Node<T>> children = new HashSet<>();
private T data;
private Node<T> parent;
private String name;
@ -124,6 +124,24 @@ class NodeItem<T> implements Node<T> {
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
public Set<Node<T>> getChildren() {
return children;
@ -141,19 +159,19 @@ class NodeItem<T> implements Node<T> {
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<T> child) {
final @NonNull Node<T> child
) {
if (child.isNamed()) {
findChildByName(child.getName())
.filter(existingChild -> existingChild != child)
findChildByName(child.getName()).filter(existingChild -> existingChild != child)
.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
public void createDescendantLine(@NonNull final List<T> 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<T> implements Node<T> {
*/
@Override
public Optional<Node<T>> findChild(@NonNull final T child) {
return children.stream().filter(node -> {
return children.stream()
.filter(node -> {
final Optional<T> d = node.getData();
return d.isPresent() && d.get().equals(child);
}).findAny();
return d.isPresent() && d.get()
.equals(child);
})
.findAny();
}
@Override
@ -247,25 +267,7 @@ class NodeItem<T> implements Node<T> {
*/
@Override
public boolean isDescendantOf(final Node<T> 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<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);
return parent != null && (node.equals(parent) || parent.isDescendantOf(node));
}
/**
@ -299,10 +301,8 @@ class NodeItem<T> implements Node<T> {
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<T> implements Node<T> {
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,7 +336,8 @@ class NodeItem<T> implements Node<T> {
@Override
public Optional<Node<T>> findChildByName(@NonNull final String named) {
return children.stream()
.filter((Node<T> t) -> t.getName().equals(named))
.filter((Node<T> t) -> t.getName()
.equals(named))
.findAny();
}

View file

@ -87,7 +87,8 @@ public final class Nodes {
* @return the new node
*/
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);
}
@ -100,12 +101,13 @@ public final class Nodes {
* @return the immutable copy of the tree
*/
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");
}
final Set<Node<T>> 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 <T> Set<Node<T>> getImmutableChildren(final Node<T> source) {
@ -116,15 +118,16 @@ public final class Nodes {
}
private static <T> Node<T> asImmutableChild(
final Node<T> source) {
final Node<T> source
) {
final Optional<Node<T>> 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");
}
}