Drop unneeded generic type specifiers
[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,
28             final LeafRefContext root) {
29         final SchemaPath schemaPath = node.getPath();
30         return getLeafRefReferencingContext(schemaPath, root);
31     }
32
33     public static LeafRefContext getLeafRefReferencingContext(
34             final SchemaPath schemaPath, final LeafRefContext root) {
35         final Iterable<QName> pathFromRoot = schemaPath.getPathFromRoot();
36         return getLeafRefReferencingContext(pathFromRoot, root);
37     }
38
39     public static LeafRefContext getLeafRefReferencingContext(
40             final Iterable<QName> pathFromRoot, LeafRefContext root) {
41
42         LeafRefContext leafRefCtx = null;
43         final Iterator<QName> iterator = pathFromRoot.iterator();
44         while (iterator.hasNext() && root != null) {
45             final QName qname = iterator.next();
46             leafRefCtx = root.getReferencingChildByName(qname);
47             if (iterator.hasNext()) {
48                 root = leafRefCtx;
49             }
50         }
51
52         return leafRefCtx;
53     }
54
55     public static LeafRefContext getLeafRefReferencedByContext(final SchemaNode node,
56             final LeafRefContext root) {
57         final SchemaPath schemaPath = node.getPath();
58         return getLeafRefReferencedByContext(schemaPath, root);
59     }
60
61     public static LeafRefContext getLeafRefReferencedByContext(
62             final SchemaPath schemaPath, final LeafRefContext root) {
63         final Iterable<QName> pathFromRoot = schemaPath.getPathFromRoot();
64         return getLeafRefReferencedByContext(pathFromRoot, root);
65     }
66
67     public static LeafRefContext getLeafRefReferencedByContext(
68             final Iterable<QName> pathFromRoot, LeafRefContext root) {
69
70         LeafRefContext leafRefCtx = null;
71         final Iterator<QName> iterator = pathFromRoot.iterator();
72         while (iterator.hasNext() && root != null) {
73             final QName qname = iterator.next();
74             leafRefCtx = root.getReferencedChildByName(qname);
75             if (iterator.hasNext()) {
76                 root = leafRefCtx;
77             }
78         }
79
80         return leafRefCtx;
81     }
82
83     public static boolean isLeafRef(final SchemaNode node, final LeafRefContext root) {
84
85         if (node == null || root == null) {
86             return false;
87         }
88
89         final LeafRefContext leafRefReferencingContext = getLeafRefReferencingContext(
90                 node, root);
91         if (leafRefReferencingContext == null) {
92             return false;
93         }
94
95         return leafRefReferencingContext.isReferencing();
96     }
97
98     public static boolean hasLeafRefChild(final SchemaNode node, final LeafRefContext root) {
99
100         if (node == null || root == null) {
101             return false;
102         }
103
104         final LeafRefContext leafRefReferencingContext = getLeafRefReferencingContext(
105                 node, root);
106         if (leafRefReferencingContext == null) {
107             return false;
108         }
109
110         return leafRefReferencingContext.hasReferencingChild();
111     }
112
113     public static boolean isReferencedByLeafRef(final SchemaNode node,
114             final LeafRefContext root) {
115
116         if (node == null || root == null) {
117             return false;
118         }
119
120         final LeafRefContext leafRefReferencedByContext = getLeafRefReferencedByContext(
121                 node, root);
122         if (leafRefReferencedByContext == null) {
123             return false;
124         }
125
126         return leafRefReferencedByContext.isReferenced();
127     }
128
129     public static boolean hasChildReferencedByLeafRef(final SchemaNode node,
130             final LeafRefContext root) {
131
132         if ((node == null) || (root == null)) {
133             return false;
134         }
135
136         final LeafRefContext leafRefReferencedByContext = getLeafRefReferencedByContext(
137                 node, root);
138         if (leafRefReferencedByContext == null) {
139             return false;
140         }
141
142         return leafRefReferencedByContext.hasReferencedChild();
143     }
144
145     public static List<LeafRefContext> findAllLeafRefChilds(final SchemaNode node,
146             final LeafRefContext root) {
147
148         return findAllLeafRefChilds(node.getPath(), root);
149     }
150
151     public static List<LeafRefContext> findAllLeafRefChilds(
152             final SchemaPath schemaPath, final LeafRefContext root) {
153
154         return findAllLeafRefChilds(schemaPath.getPathFromRoot(), root);
155     }
156
157     public static List<LeafRefContext> findAllLeafRefChilds(
158             final Iterable<QName> pathFromRoot, final LeafRefContext root) {
159
160         final LeafRefContext leafRefReferencingContext = getLeafRefReferencingContext(
161                 pathFromRoot, root);
162         final List<LeafRefContext> allLeafRefsChilds = findAllLeafRefChilds(leafRefReferencingContext);
163
164         return allLeafRefsChilds;
165     }
166
167     public static List<LeafRefContext> findAllLeafRefChilds(
168             final LeafRefContext parent) {
169
170         final LinkedList<LeafRefContext> leafRefChilds = new LinkedList<>();
171
172         if (parent == null) {
173             return leafRefChilds;
174         }
175
176         if (parent.isReferencing()) {
177             leafRefChilds.add(parent);
178             return leafRefChilds;
179         } else {
180             final Set<Entry<QName, LeafRefContext>> childs = parent
181                     .getReferencingChilds().entrySet();
182             for (final Entry<QName, LeafRefContext> child : childs) {
183                 leafRefChilds.addAll(findAllLeafRefChilds(child.getValue()));
184             }
185         }
186         return leafRefChilds;
187     }
188
189     public static List<LeafRefContext> findAllChildsReferencedByLeafRef(
190             final SchemaNode node, final LeafRefContext root) {
191
192         return findAllChildsReferencedByLeafRef(node.getPath(), root);
193     }
194
195     public static List<LeafRefContext> findAllChildsReferencedByLeafRef(
196             final SchemaPath schemaPath, final LeafRefContext root) {
197
198         return findAllChildsReferencedByLeafRef(schemaPath.getPathFromRoot(),
199                 root);
200     }
201
202     public static List<LeafRefContext> findAllChildsReferencedByLeafRef(
203             final Iterable<QName> pathFromRoot, final LeafRefContext root) {
204
205         final LeafRefContext leafRefReferencedByContext = getLeafRefReferencedByContext(
206                 pathFromRoot, root);
207         final List<LeafRefContext> allChildsReferencedByLeafRef = findAllChildsReferencedByLeafRef(leafRefReferencedByContext);
208
209         return allChildsReferencedByLeafRef;
210     }
211
212     public static List<LeafRefContext> findAllChildsReferencedByLeafRef(
213             final LeafRefContext parent) {
214
215         final LinkedList<LeafRefContext> childsReferencedByLeafRef = new LinkedList<>();
216
217         if (parent == null) {
218             return childsReferencedByLeafRef;
219         }
220
221         if (parent.isReferenced()) {
222             childsReferencedByLeafRef.add(parent);
223             return childsReferencedByLeafRef;
224         } else {
225             final Set<Entry<QName, LeafRefContext>> childs = parent
226                     .getReferencedByChilds().entrySet();
227             for (final Entry<QName, LeafRefContext> child : childs) {
228                 childsReferencedByLeafRef
229                         .addAll(findAllChildsReferencedByLeafRef(child
230                                 .getValue()));
231             }
232         }
233         return childsReferencedByLeafRef;
234     }
235
236     public static Map<QName, LeafRefContext> getAllLeafRefsReferencingThisNode(
237             final SchemaNode node, final LeafRefContext root) {
238         return getAllLeafRefsReferencingThisNode(node.getPath(), root);
239     }
240
241     public static Map<QName, LeafRefContext> getAllLeafRefsReferencingThisNode(
242             final SchemaPath path, final LeafRefContext root) {
243         return getAllLeafRefsReferencingThisNode(path.getPathFromRoot(), root);
244     }
245
246     public static Map<QName, LeafRefContext> getAllLeafRefsReferencingThisNode(
247             final Iterable<QName> pathFromRoot, final LeafRefContext root) {
248
249         final LeafRefContext leafRefReferencedByContext = getLeafRefReferencedByContext(
250                 pathFromRoot, root);
251
252         if (leafRefReferencedByContext == null) {
253             return new HashMap<>();
254         }
255
256         return leafRefReferencedByContext.getAllReferencedByLeafRefCtxs();
257     }
258
259 }