import static java.util.Objects.requireNonNull;
import com.google.common.annotations.Beta;
-import com.google.common.base.Strings;
import com.google.common.collect.Iterables;
import com.google.common.collect.Maps;
import java.util.Arrays;
private static final int STRINGTREE_INDENT = 4;
private NormalizedNodes() {
- throw new UnsupportedOperationException("Utility class should not be instantiated");
+ // Hidden on purpose
+ }
+
+ /**
+ * Find duplicate NormalizedNode instances within a subtree. Duplicates are those, which compare
+ * as equal, but do not refer to the same object.
+ *
+ * @param node A normalized node subtree, may not be null
+ * @return A Map of NormalizedNode/DuplicateEntry relationships.
+ */
+ public static Map<NormalizedNode<?, ?>, DuplicateEntry> findDuplicates(final @NonNull NormalizedNode<?, ?> node) {
+ return Maps.filterValues(DuplicateFinder.findDuplicates(node), input -> !input.getDuplicates().isEmpty());
}
public static Optional<NormalizedNode<?, ?>> findNode(final YangInstanceIdentifier rootPath,
public static Optional<NormalizedNode<?, ?>> findNode(final Optional<NormalizedNode<?, ?>> parent,
final Iterable<PathArgument> relativePath) {
- requireNonNull(parent, "Parent must not be null");
- requireNonNull(relativePath, "Relative path must not be null");
-
- Optional<NormalizedNode<?, ?>> currentNode = parent;
- final Iterator<PathArgument> pathIterator = relativePath.iterator();
+ final Iterator<PathArgument> pathIterator = requireNonNull(relativePath, "Relative path must not be null")
+ .iterator();
+ Optional<NormalizedNode<?, ?>> currentNode = requireNonNull(parent, "Parent must not be null");
while (currentNode.isPresent() && pathIterator.hasNext()) {
currentNode = getDirectChild(currentNode.get(), pathIterator.next());
}
public static Optional<NormalizedNode<?, ?>> findNode(final NormalizedNode<?, ?> tree,
final YangInstanceIdentifier path) {
- requireNonNull(tree, "Tree must not be null");
- requireNonNull(path, "Path must not be null");
-
- return findNode(Optional.of(tree), path.getPathArguments());
+ return findNode(Optional.of(requireNonNull(tree, "Tree must not be null")),
+ requireNonNull(path, "Path must not be null").getPathArguments());
}
@SuppressWarnings({ "unchecked", "rawtypes" })
public static Optional<NormalizedNode<?, ?>> getDirectChild(final NormalizedNode<?, ?> node,
final PathArgument pathArg) {
- if (node instanceof LeafNode<?> || node instanceof LeafSetEntryNode<?>) {
+ if (node instanceof ValueNode) {
return Optional.empty();
- } else if (node instanceof DataContainerNode<?>) {
+ } else if (node instanceof DataContainerNode) {
return (Optional) ((DataContainerNode<?>) node).getChild(pathArg);
} else if (node instanceof MapNode && pathArg instanceof NodeIdentifierWithPredicates) {
return (Optional) ((MapNode) node).getChild((NodeIdentifierWithPredicates) pathArg);
- } else if (node instanceof LeafSetNode<?> && pathArg instanceof NodeWithValue) {
+ } else if (node instanceof LeafSetNode && pathArg instanceof NodeWithValue) {
return (Optional) ((LeafSetNode<?>) node).getChild((NodeWithValue) pathArg);
}
return Optional.empty();
}
private static void toStringTree(final StringBuilder builder, final NormalizedNode<?, ?> node, final int offset) {
- final String prefix = Strings.repeat(" ", offset);
+ final String prefix = " ".repeat(offset);
builder.append(prefix).append(toStringTree(node.getIdentifier()));
- if (node instanceof NormalizedNodeContainer<?, ?, ?>) {
+ if (node instanceof NormalizedNodeContainer) {
final NormalizedNodeContainer<?, ?, ?> container = (NormalizedNodeContainer<?, ?, ?>) node;
builder.append(" {\n");
private static String toStringTree(final PathArgument identifier) {
if (identifier instanceof NodeIdentifierWithPredicates) {
- return identifier.getNodeType().getLocalName()
- + ((NodeIdentifierWithPredicates) identifier).getKeyValues().values();
+ return identifier.getNodeType().getLocalName() + ((NodeIdentifierWithPredicates) identifier).values();
} else if (identifier instanceof AugmentationIdentifier) {
return "augmentation";
} else {
return identifier.getNodeType().getLocalName();
}
}
-
- /**
- * Find duplicate NormalizedNode instances within a subtree. Duplicates are those, which compare
- * as equal, but do not refer to the same object.
- *
- * @param node A normalized node subtree, may not be null
- * @return A Map of NormalizedNode/DuplicateEntry relationships.
- */
- public static Map<NormalizedNode<?, ?>, DuplicateEntry> findDuplicates(final @NonNull NormalizedNode<?, ?> node) {
- return Maps.filterValues(DuplicateFinder.findDuplicates(node), input -> !input.getDuplicates().isEmpty());
- }
}