2 * Copyright (c) 2015 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.impl.leafref;
10 import java.util.HashMap;
12 import org.opendaylight.yangtools.yang.common.QName;
13 import org.opendaylight.yangtools.yang.common.QNameModule;
14 import org.opendaylight.yangtools.yang.model.api.Module;
15 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
16 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
18 class LeafRefContextBuilder {
20 private QName currentNodeQName;
21 private SchemaPath currentNodePath;
22 private SchemaContext schemaContext;
24 private LeafRefPath leafRefTargetPath = null;
25 private LeafRefPath absoluteLeafRefTargetPath = null;
26 private String leafRefTargetPathString = "";
28 private boolean isReferencedBy = false;
29 private boolean isReferencing = false;
31 private final Map<QName, LeafRefContext> referencingChildren = new HashMap<>();
32 private final Map<QName, LeafRefContext> referencedByChildren = new HashMap<>();
33 private final Map<QName, LeafRefContext> referencedByLeafRefCtx = new HashMap<>();
35 public LeafRefContextBuilder(final QName currentNodeQName,
36 final SchemaPath currentNodePath, final SchemaContext schemaContext) {
37 this.currentNodeQName = currentNodeQName;
38 this.currentNodePath = currentNodePath;
39 this.schemaContext = schemaContext;
42 public LeafRefContext build() {
43 final LeafRefContext leafRefContext = new LeafRefContext(this);
45 // LeafRefContext has made a copy of these
46 referencingChildren.clear();
47 referencedByChildren.clear();
48 referencedByLeafRefCtx.clear();
50 return leafRefContext;
53 public boolean hasLeafRefContextChild() {
54 return hasReferencedByChild() || hasReferencingChild();
57 public boolean hasReferencedByChild() {
58 return !referencedByChildren.isEmpty();
61 public boolean hasReferencingChild() {
62 return !referencingChildren.isEmpty();
65 public boolean isReferencedBy() {
66 return isReferencedBy;
69 public void setReferencedBy(final boolean isReferencedBy) {
70 this.isReferencedBy = isReferencedBy;
73 public boolean isReferencing() {
77 public void setReferencing(final boolean isReferencing) {
78 this.isReferencing = isReferencing;
81 public void addReferencingChild(final LeafRefContext child, final QName childQName) {
82 referencingChildren.put(childQName, child);
85 public LeafRefContext getReferencingChildByName(final QName name) {
86 return referencingChildren.get(name);
89 public Map<QName, LeafRefContext> getReferencingChilds() {
90 return referencingChildren;
93 public void addReferencedByChild(final LeafRefContext child, final QName childQName) {
94 referencedByChildren.put(childQName, child);
97 public LeafRefContext getReferencedByChildByName(final QName name) {
98 return referencedByChildren.get(name);
101 public Map<QName, LeafRefContext> getReferencedByChilds() {
102 return referencedByChildren;
105 public SchemaPath getCurrentNodePath() {
106 return currentNodePath;
109 public void setCurrentNodePath(final SchemaPath currentNodePath) {
110 this.currentNodePath = currentNodePath;
113 public LeafRefPath getLeafRefTargetPath() {
114 return leafRefTargetPath;
117 public void setLeafRefTargetPath(final LeafRefPath leafRefPath) {
118 this.leafRefTargetPath = leafRefPath;
121 public String getLeafRefTargetPathString() {
122 return leafRefTargetPathString;
125 public void setLeafRefTargetPathString(final String leafRefPathString) {
126 this.leafRefTargetPathString = leafRefPathString;
129 public QName getCurrentNodeQName() {
130 return currentNodeQName;
133 public void setCurrentNodeQName(final QName currentNodeQName) {
134 this.currentNodeQName = currentNodeQName;
137 public SchemaContext getSchemaContext() {
138 return schemaContext;
141 public void setSchemaContext(final SchemaContext schemaContext) {
142 this.schemaContext = schemaContext;
145 public LeafRefPath getAbsoluteLeafRefTargetPath() {
147 if (isReferencing && absoluteLeafRefTargetPath == null) {
148 if (leafRefTargetPath.isAbsolute()) {
149 absoluteLeafRefTargetPath = leafRefTargetPath;
151 absoluteLeafRefTargetPath = LeafRefUtils
152 .createAbsoluteLeafRefPath(leafRefTargetPath,
153 currentNodePath, getLeafRefContextModule());
157 return absoluteLeafRefTargetPath;
160 public Module getLeafRefContextModule() {
161 final QNameModule qnameModule = currentNodeQName.getModule();
163 return schemaContext.findModuleByNamespaceAndRevision(
164 qnameModule.getNamespace(), qnameModule.getRevision());
167 public void addReferencedByLeafRefCtx(final QName qname, final LeafRefContext leafRef) {
168 referencedByLeafRefCtx.put(qname, leafRef);
171 public LeafRefContext getReferencedByLeafRefCtxByName(final QName qname) {
172 return referencedByLeafRefCtx.get(qname);
175 public Map<QName, LeafRefContext> getAllReferencedByLeafRefCtxs() {
176 return referencedByLeafRefCtx;