Disconnect yang-datae-tree-ri from concepts.Builder
[yangtools.git] / data / yang-data-tree-ri / src / main / java / org / opendaylight / yangtools / yang / data / tree / leafref / LeafRefContextBuilder.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.tree.leafref;
9
10 import static java.util.Objects.requireNonNull;
11
12 import java.util.HashMap;
13 import java.util.Iterator;
14 import java.util.Map;
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.opendaylight.yangtools.concepts.Mutable;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.common.QNameModule;
19 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
20 import org.opendaylight.yangtools.yang.model.api.Module;
21 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
22
23 final class LeafRefContextBuilder implements Mutable {
24     private final Map<QName, LeafRefContext> referencingChildren = new HashMap<>();
25     private final Map<QName, LeafRefContext> referencedByChildren = new HashMap<>();
26     private final Map<QName, LeafRefContext> referencedByLeafRefCtx = new HashMap<>();
27
28     private final QName currentNodeQName;
29     private final SchemaPath currentNodePath;
30     private final EffectiveModelContext schemaContext;
31
32     private LeafRefPath leafRefTargetPath = null;
33     private LeafRefPath absoluteLeafRefTargetPath = null;
34     private String leafRefTargetPathString = "";
35
36     private boolean isReferencedBy = false;
37     private boolean isReferencing = false;
38
39     LeafRefContextBuilder(final QName currentNodeQName, final SchemaPath currentNodePath,
40             final EffectiveModelContext schemaContext) {
41         this.currentNodeQName = requireNonNull(currentNodeQName);
42         this.currentNodePath = requireNonNull(currentNodePath);
43         this.schemaContext = requireNonNull(schemaContext);
44     }
45
46     @NonNull LeafRefContext build() {
47         final LeafRefContext leafRefContext = new LeafRefContext(this);
48
49         // LeafRefContext has made a copy of these
50         referencingChildren.clear();
51         referencedByChildren.clear();
52         referencedByLeafRefCtx.clear();
53
54         return leafRefContext;
55     }
56
57     boolean isReferencedBy() {
58         return isReferencedBy;
59     }
60
61     void setReferencedBy(final boolean referencedBy) {
62         isReferencedBy = referencedBy;
63     }
64
65     boolean isReferencing() {
66         return isReferencing;
67     }
68
69     void setReferencing(final boolean referencing) {
70         isReferencing = referencing;
71     }
72
73     void addReferencingChild(final LeafRefContext child, final QName childQName) {
74         referencingChildren.put(childQName, child);
75     }
76
77     Map<QName, LeafRefContext> getReferencingChilds() {
78         return referencingChildren;
79     }
80
81     void addReferencedByChild(final LeafRefContext child, final QName childQName) {
82         referencedByChildren.put(childQName, child);
83     }
84
85     Map<QName, LeafRefContext> getReferencedByChilds() {
86         return referencedByChildren;
87     }
88
89     SchemaPath getCurrentNodePath() {
90         return currentNodePath;
91     }
92
93     LeafRefPath getLeafRefTargetPath() {
94         return leafRefTargetPath;
95     }
96
97     void setLeafRefTargetPath(final LeafRefPath leafRefPath) {
98         leafRefTargetPath = requireNonNull(leafRefPath);
99     }
100
101     String getLeafRefTargetPathString() {
102         return leafRefTargetPathString;
103     }
104
105     void setLeafRefTargetPathString(final String leafRefPathString) {
106         leafRefTargetPathString = requireNonNull(leafRefPathString);
107     }
108
109     QName getCurrentNodeQName() {
110         return currentNodeQName;
111     }
112
113     EffectiveModelContext getSchemaContext() {
114         return schemaContext;
115     }
116
117     LeafRefPath getAbsoluteLeafRefTargetPath() {
118         if (isReferencing && absoluteLeafRefTargetPath == null) {
119             if (leafRefTargetPath.isAbsolute()) {
120                 absoluteLeafRefTargetPath = leafRefTargetPath;
121             } else {
122                 absoluteLeafRefTargetPath = LeafRefUtils.createAbsoluteLeafRefPath(leafRefTargetPath,
123                     currentNodePath, getLeafRefContextModule());
124             }
125         }
126
127         return absoluteLeafRefTargetPath;
128     }
129
130     Module getLeafRefContextModule() {
131         final Iterator<QName> it = currentNodePath.getPathFromRoot().iterator();
132         final QNameModule qnameModule = it.hasNext() ? it.next().getModule() : currentNodeQName.getModule();
133         return schemaContext.findModule(qnameModule).orElse(null);
134     }
135
136     void addReferencedByLeafRefCtx(final QName qname, final LeafRefContext leafRef) {
137         referencedByLeafRefCtx.put(qname, leafRef);
138     }
139
140     LeafRefContext getReferencedByLeafRefCtxByName(final QName qname) {
141         return referencedByLeafRefCtx.get(qname);
142     }
143
144     Map<QName, LeafRefContext> getAllReferencedByLeafRefCtxs() {
145         return referencedByLeafRefCtx;
146     }
147 }