Address trivial eclipse warnings
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / leafref / LeafRefContextUtils.java
1 /**
2  * Copyright (c) 2015 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.impl.leafref;
9
10 import java.util.HashMap;
11 import java.util.Iterator;
12 import java.util.LinkedList;
13 import java.util.List;
14 import java.util.Map;
15 import java.util.Map.Entry;
16 import java.util.Set;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
19 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
20
21 public final class LeafRefContextUtils {
22
23     private LeafRefContextUtils() {
24         throw new UnsupportedOperationException();
25     }
26
27     public static LeafRefContext getLeafRefReferencingContext(final SchemaNode node, final LeafRefContext root) {
28         final SchemaPath schemaPath = node.getPath();
29         return getLeafRefReferencingContext(schemaPath, root);
30     }
31
32     public static LeafRefContext getLeafRefReferencingContext(
33             final SchemaPath schemaPath, final LeafRefContext root) {
34         final Iterable<QName> pathFromRoot = schemaPath.getPathFromRoot();
35         return getLeafRefReferencingContext(pathFromRoot, root);
36     }
37
38     public static LeafRefContext getLeafRefReferencingContext(final Iterable<QName> pathFromRoot, LeafRefContext root) {
39         LeafRefContext leafRefCtx = null;
40         final Iterator<QName> iterator = pathFromRoot.iterator();
41         while (iterator.hasNext() && root != null) {
42             final QName qname = iterator.next();
43             leafRefCtx = root.getReferencingChildByName(qname);
44             if (iterator.hasNext()) {
45                 root = leafRefCtx;
46             }
47         }
48
49         return leafRefCtx;
50     }
51
52     public static LeafRefContext getLeafRefReferencedByContext(final SchemaNode node, final LeafRefContext root) {
53         final SchemaPath schemaPath = node.getPath();
54         return getLeafRefReferencedByContext(schemaPath, root);
55     }
56
57     public static LeafRefContext getLeafRefReferencedByContext(
58             final SchemaPath schemaPath, final LeafRefContext root) {
59         final Iterable<QName> pathFromRoot = schemaPath.getPathFromRoot();
60         return getLeafRefReferencedByContext(pathFromRoot, root);
61     }
62
63     public static LeafRefContext getLeafRefReferencedByContext(final Iterable<QName> pathFromRoot,
64             LeafRefContext root) {
65
66         LeafRefContext leafRefCtx = null;
67         final Iterator<QName> iterator = pathFromRoot.iterator();
68         while (iterator.hasNext() && root != null) {
69             final QName qname = iterator.next();
70             leafRefCtx = root.getReferencedChildByName(qname);
71             if (iterator.hasNext()) {
72                 root = leafRefCtx;
73             }
74         }
75
76         return leafRefCtx;
77     }
78
79     public static boolean isLeafRef(final SchemaNode node, final LeafRefContext root) {
80         if (node == null || root == null) {
81             return false;
82         }
83
84         final LeafRefContext leafRefReferencingContext = getLeafRefReferencingContext(node, root);
85         if (leafRefReferencingContext == null) {
86             return false;
87         }
88
89         return leafRefReferencingContext.isReferencing();
90     }
91
92     public static boolean hasLeafRefChild(final SchemaNode node, final LeafRefContext root) {
93         if (node == null || root == null) {
94             return false;
95         }
96
97         final LeafRefContext leafRefReferencingContext = getLeafRefReferencingContext(node, root);
98         if (leafRefReferencingContext == null) {
99             return false;
100         }
101
102         return leafRefReferencingContext.hasReferencingChild();
103     }
104
105     public static boolean isReferencedByLeafRef(final SchemaNode node, final LeafRefContext root) {
106         if (node == null || root == null) {
107             return false;
108         }
109
110         final LeafRefContext leafRefReferencedByContext = getLeafRefReferencedByContext(node, root);
111         if (leafRefReferencedByContext == null) {
112             return false;
113         }
114
115         return leafRefReferencedByContext.isReferenced();
116     }
117
118     public static boolean hasChildReferencedByLeafRef(final SchemaNode node, final LeafRefContext root) {
119         if (node == null || root == null) {
120             return false;
121         }
122
123         final LeafRefContext leafRefReferencedByContext = getLeafRefReferencedByContext(node, root);
124         if (leafRefReferencedByContext == null) {
125             return false;
126         }
127
128         return leafRefReferencedByContext.hasReferencedChild();
129     }
130
131     public static List<LeafRefContext> findAllLeafRefChilds(final SchemaNode node, final LeafRefContext root) {
132         return findAllLeafRefChilds(node.getPath(), root);
133     }
134
135     public static List<LeafRefContext> findAllLeafRefChilds(final SchemaPath schemaPath, final LeafRefContext root) {
136         return findAllLeafRefChilds(schemaPath.getPathFromRoot(), root);
137     }
138
139     public static List<LeafRefContext> findAllLeafRefChilds(final Iterable<QName> pathFromRoot,
140             final LeafRefContext root) {
141         final LeafRefContext leafRefReferencingContext = getLeafRefReferencingContext(pathFromRoot, root);
142         final List<LeafRefContext> allLeafRefsChilds = findAllLeafRefChilds(leafRefReferencingContext);
143
144         return allLeafRefsChilds;
145     }
146
147     public static List<LeafRefContext> findAllLeafRefChilds(final LeafRefContext parent) {
148         final LinkedList<LeafRefContext> leafRefChilds = new LinkedList<>();
149         if (parent == null) {
150             return leafRefChilds;
151         }
152
153         if (parent.isReferencing()) {
154             leafRefChilds.add(parent);
155             return leafRefChilds;
156         }
157
158         final Set<Entry<QName, LeafRefContext>> childs = parent.getReferencingChilds().entrySet();
159         for (final Entry<QName, LeafRefContext> child : childs) {
160             leafRefChilds.addAll(findAllLeafRefChilds(child.getValue()));
161         }
162         return leafRefChilds;
163     }
164
165     public static List<LeafRefContext> findAllChildsReferencedByLeafRef(final SchemaNode node,
166             final LeafRefContext root) {
167         return findAllChildsReferencedByLeafRef(node.getPath(), root);
168     }
169
170     public static List<LeafRefContext> findAllChildsReferencedByLeafRef(final SchemaPath schemaPath,
171             final LeafRefContext root) {
172         return findAllChildsReferencedByLeafRef(schemaPath.getPathFromRoot(), root);
173     }
174
175     public static List<LeafRefContext> findAllChildsReferencedByLeafRef(final Iterable<QName> pathFromRoot,
176             final LeafRefContext root) {
177
178         final LeafRefContext leafRefReferencedByContext = getLeafRefReferencedByContext(pathFromRoot, root);
179         final List<LeafRefContext> allChildsReferencedByLeafRef =
180                 findAllChildsReferencedByLeafRef(leafRefReferencedByContext);
181
182         return allChildsReferencedByLeafRef;
183     }
184
185     public static List<LeafRefContext> findAllChildsReferencedByLeafRef(final LeafRefContext parent) {
186         final LinkedList<LeafRefContext> childsReferencedByLeafRef = new LinkedList<>();
187         if (parent == null) {
188             return childsReferencedByLeafRef;
189         }
190
191         if (parent.isReferenced()) {
192             childsReferencedByLeafRef.add(parent);
193             return childsReferencedByLeafRef;
194         }
195
196         final Set<Entry<QName, LeafRefContext>> childs = parent.getReferencedByChilds().entrySet();
197         for (final Entry<QName, LeafRefContext> child : childs) {
198             childsReferencedByLeafRef.addAll(findAllChildsReferencedByLeafRef(child.getValue()));
199         }
200         return childsReferencedByLeafRef;
201     }
202
203     public static Map<QName, LeafRefContext> getAllLeafRefsReferencingThisNode(
204             final SchemaNode node, final LeafRefContext root) {
205         return getAllLeafRefsReferencingThisNode(node.getPath(), root);
206     }
207
208     public static Map<QName, LeafRefContext> getAllLeafRefsReferencingThisNode(final SchemaPath path,
209             final LeafRefContext root) {
210         return getAllLeafRefsReferencingThisNode(path.getPathFromRoot(), root);
211     }
212
213     public static Map<QName, LeafRefContext> getAllLeafRefsReferencingThisNode(final Iterable<QName> pathFromRoot,
214             final LeafRefContext root) {
215
216         final LeafRefContext leafRefReferencedByContext = getLeafRefReferencedByContext(pathFromRoot, root);
217         if (leafRefReferencedByContext == null) {
218             return new HashMap<>();
219         }
220
221         return leafRefReferencedByContext.getAllReferencedByLeafRefCtxs();
222     }
223 }