Fix eclipse/checkstyle warnings
[yangtools.git] / yang / yang-data-api / src / main / java / org / opendaylight / yangtools / yang / data / api / schema / tree / StoreTreeNodes.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.data.api.schema.tree;
9
10 import com.google.common.annotations.Beta;
11 import com.google.common.base.Optional;
12 import com.google.common.base.Predicate;
13 import com.google.common.base.Predicates;
14 import com.google.common.base.Verify;
15 import java.util.AbstractMap.SimpleImmutableEntry;
16 import java.util.Iterator;
17 import java.util.Map.Entry;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
20
21 /**
22  * A set of utility methods for interacting with {@link StoreTreeNode} objects.
23  */
24 @Beta
25 public final class StoreTreeNodes {
26     private StoreTreeNodes() {
27         throw new UnsupportedOperationException("Utility class should not be instantiated");
28     }
29
30     /**
31      * Finds a node in tree.
32      *
33      * @param <T>
34      *          Store tree node type.
35      * @param tree Data Tree
36      * @param path Path to the node
37      * @return Optional with node if the node is present in tree, {@link Optional#absent()} otherwise.
38      */
39     public static <T extends StoreTreeNode<T>> Optional<T> findNode(final T tree, final YangInstanceIdentifier path) {
40         Optional<T> current = Optional.of(tree);
41         Iterator<PathArgument> pathIter = path.getPathArguments().iterator();
42         while (current.isPresent() && pathIter.hasNext()) {
43             current = current.get().getChild(pathIter.next());
44         }
45         return current;
46     }
47
48     public static <T extends StoreTreeNode<T>> T findNodeChecked(final T tree, final YangInstanceIdentifier path) {
49         T current = tree;
50
51         int depth = 1;
52         for (PathArgument pathArg : path.getPathArguments()) {
53             Optional<T> potential = current.getChild(pathArg);
54             if (!potential.isPresent()) {
55                 throw new IllegalArgumentException(String.format("Child %s is not present in tree.",
56                         path.getAncestor(depth)));
57             }
58             current = potential.get();
59             ++depth;
60         }
61         return current;
62     }
63
64     /**
65      * Finds a node or closest parent in the tree.
66      *
67      * @param <T>
68      *          Store tree node type.
69      * @param tree Data Tree
70      * @param path Path to the node
71      * @return Map.Entry Entry with key which is path to closest parent and value is parent node.
72      */
73     public static <T extends StoreTreeNode<T>> Entry<YangInstanceIdentifier, T> findClosest(final T tree,
74             final YangInstanceIdentifier path) {
75         return findClosestsOrFirstMatch(tree, path, Predicates.alwaysFalse());
76     }
77
78     public static <T extends StoreTreeNode<T>> Entry<YangInstanceIdentifier, T> findClosestsOrFirstMatch(final T tree,
79             final YangInstanceIdentifier path, final Predicate<T> predicate) {
80         Optional<T> parent = Optional.of(tree);
81         Optional<T> current = Optional.of(tree);
82
83         int nesting = 0;
84         Iterator<PathArgument> pathIter = path.getPathArguments().iterator();
85         while (current.isPresent() && pathIter.hasNext() && !predicate.apply(current.get())) {
86             parent = current;
87             current = current.get().getChild(pathIter.next());
88             nesting++;
89         }
90         if (current.isPresent()) {
91             final YangInstanceIdentifier currentPath = path.getAncestor(nesting);
92             return new SimpleImmutableEntry<>(currentPath, current.get());
93         }
94
95         /*
96          * Subtracting 1 from nesting level at this point is safe, because we
97          * cannot reach here with nesting == 0: that would mean the above check
98          * for current.isPresent() failed, which it cannot, as current is always
99          * present. At any rate we verify state just to be on the safe side.
100          */
101         Verify.verify(nesting > 0);
102         return new SimpleImmutableEntry<>(path.getAncestor(nesting - 1), parent.get());
103     }
104
105     public static <T extends StoreTreeNode<T>> Optional<T> getChild(final Optional<T> parent,
106             final PathArgument child) {
107         if (parent.isPresent()) {
108             return parent.get().getChild(child);
109         }
110         return Optional.absent();
111     }
112 }