2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.yangtools.yang.data.api.schema.tree;
10 import com.google.common.annotations.Beta;
11 import com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
13 import com.google.common.base.Predicate;
14 import com.google.common.base.Predicates;
15 import com.google.common.collect.Iterables;
16 import java.util.AbstractMap.SimpleEntry;
17 import java.util.Iterator;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
23 * A set of utility methods for interacting with {@link StoreTreeNode} objects.
26 public final class StoreTreeNodes {
27 private StoreTreeNodes() {
28 throw new UnsupportedOperationException("Utility class should not be instantiated");
32 * Finds a node in tree
34 * @param tree Data Tree
35 * @param path Path to the node
36 * @return Optional with node if the node is present in tree, {@link Optional#absent()} otherwise.
38 public static <T extends StoreTreeNode<T>> Optional<T> findNode(final T tree, final YangInstanceIdentifier path) {
39 Optional<T> current = Optional.<T> of(tree);
40 Iterator<PathArgument> pathIter = path.getPathArguments().iterator();
41 while (current.isPresent() && pathIter.hasNext()) {
42 current = current.get().getChild(pathIter.next());
47 public static <T extends StoreTreeNode<T>> T findNodeChecked(final T tree, final YangInstanceIdentifier path) {
51 for(PathArgument pathArg : path.getPathArguments()) {
52 Optional<T> potential = current.getChild(pathArg);
53 if (!potential.isPresent()) {
54 throw new IllegalArgumentException(String.format("Child %s is not present in tree.",
55 Iterables.toString(Iterables.limit(path.getPathArguments(), i))));
57 current = potential.get();
64 * Finds a node or closest parent in the tree
66 * @param tree Data Tree
67 * @param path Path to the node
68 * @return Map.Entry Entry with key which is path to closest parent and value is parent node.
71 public static <T extends StoreTreeNode<T>> Map.Entry<YangInstanceIdentifier, T> findClosest(final T tree, final YangInstanceIdentifier path) {
72 return findClosestsOrFirstMatch(tree, path, Predicates.<T>alwaysFalse());
75 public static <T extends StoreTreeNode<T>> Map.Entry<YangInstanceIdentifier, T> findClosestsOrFirstMatch(final T tree, final YangInstanceIdentifier path, final Predicate<T> predicate) {
76 Optional<T> parent = Optional.<T>of(tree);
77 Optional<T> current = Optional.<T> of(tree);
80 Iterator<PathArgument> pathIter = path.getPathArguments().iterator();
81 while (current.isPresent() && pathIter.hasNext() && !predicate.apply(current.get())) {
83 current = current.get().getChild(pathIter.next());
86 if (current.isPresent()) {
87 final YangInstanceIdentifier currentPath = YangInstanceIdentifier.create(Iterables.limit(path.getPathArguments(), nesting));
88 return new SimpleEntry<YangInstanceIdentifier,T>(currentPath,current.get());
92 * Subtracting 1 from nesting level at this point is safe, because we
93 * cannot reach here with nesting == 0: that would mean the above check
94 * for current.isPresent() failed, which it cannot, as current is always
95 * present. At any rate we check state just to be on the safe side.
97 Preconditions.checkState(nesting > 0);
98 final YangInstanceIdentifier parentPath = YangInstanceIdentifier.create(Iterables.limit(path.getPathArguments(), nesting - 1));
99 return new SimpleEntry<YangInstanceIdentifier,T>(parentPath,parent.get());
102 public static <T extends StoreTreeNode<T>> Optional<T> getChild(final Optional<T> parent, final PathArgument child) {
103 if (parent.isPresent()) {
104 return parent.get().getChild(child);
106 return Optional.absent();