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;
11 import java.util.Iterator;
13 import org.opendaylight.yangtools.concepts.Builder;
14 import org.opendaylight.yangtools.yang.common.QName;
15 import org.opendaylight.yangtools.yang.common.QNameModule;
16 import org.opendaylight.yangtools.yang.model.api.Module;
17 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
18 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
20 class LeafRefContextBuilder implements Builder<LeafRefContext> {
22 private final Map<QName, LeafRefContext> referencingChildren = new HashMap<>();
23 private final Map<QName, LeafRefContext> referencedByChildren = new HashMap<>();
24 private final Map<QName, LeafRefContext> referencedByLeafRefCtx = new HashMap<>();
26 private QName currentNodeQName;
27 private SchemaPath currentNodePath;
28 private SchemaContext schemaContext;
30 private LeafRefPath leafRefTargetPath = null;
31 private LeafRefPath absoluteLeafRefTargetPath = null;
32 private String leafRefTargetPathString = "";
34 private boolean isReferencedBy = false;
35 private boolean isReferencing = false;
37 LeafRefContextBuilder(final QName currentNodeQName, final SchemaPath currentNodePath,
38 final SchemaContext schemaContext) {
39 this.currentNodeQName = currentNodeQName;
40 this.currentNodePath = currentNodePath;
41 this.schemaContext = schemaContext;
45 public LeafRefContext build() {
46 final LeafRefContext leafRefContext = new LeafRefContext(this);
48 // LeafRefContext has made a copy of these
49 referencingChildren.clear();
50 referencedByChildren.clear();
51 referencedByLeafRefCtx.clear();
53 return leafRefContext;
56 public boolean hasLeafRefContextChild() {
57 return hasReferencedByChild() || hasReferencingChild();
60 public boolean hasReferencedByChild() {
61 return !referencedByChildren.isEmpty();
64 public boolean hasReferencingChild() {
65 return !referencingChildren.isEmpty();
68 public boolean isReferencedBy() {
69 return isReferencedBy;
72 public void setReferencedBy(final boolean isReferencedBy) {
73 this.isReferencedBy = isReferencedBy;
76 public boolean isReferencing() {
80 public void setReferencing(final boolean isReferencing) {
81 this.isReferencing = isReferencing;
84 public void addReferencingChild(final LeafRefContext child, final QName childQName) {
85 referencingChildren.put(childQName, child);
88 public LeafRefContext getReferencingChildByName(final QName name) {
89 return referencingChildren.get(name);
92 public Map<QName, LeafRefContext> getReferencingChilds() {
93 return referencingChildren;
96 public void addReferencedByChild(final LeafRefContext child, final QName childQName) {
97 referencedByChildren.put(childQName, child);
100 public LeafRefContext getReferencedByChildByName(final QName name) {
101 return referencedByChildren.get(name);
104 public Map<QName, LeafRefContext> getReferencedByChilds() {
105 return referencedByChildren;
108 public SchemaPath getCurrentNodePath() {
109 return currentNodePath;
112 public void setCurrentNodePath(final SchemaPath currentNodePath) {
113 this.currentNodePath = currentNodePath;
116 public LeafRefPath getLeafRefTargetPath() {
117 return leafRefTargetPath;
120 public void setLeafRefTargetPath(final LeafRefPath leafRefPath) {
121 this.leafRefTargetPath = leafRefPath;
124 public String getLeafRefTargetPathString() {
125 return leafRefTargetPathString;
128 public void setLeafRefTargetPathString(final String leafRefPathString) {
129 this.leafRefTargetPathString = leafRefPathString;
132 public QName getCurrentNodeQName() {
133 return currentNodeQName;
136 public void setCurrentNodeQName(final QName currentNodeQName) {
137 this.currentNodeQName = currentNodeQName;
140 public SchemaContext getSchemaContext() {
141 return schemaContext;
144 public void setSchemaContext(final SchemaContext schemaContext) {
145 this.schemaContext = schemaContext;
148 public LeafRefPath getAbsoluteLeafRefTargetPath() {
149 if (isReferencing && absoluteLeafRefTargetPath == null) {
150 if (leafRefTargetPath.isAbsolute()) {
151 absoluteLeafRefTargetPath = leafRefTargetPath;
153 absoluteLeafRefTargetPath = LeafRefUtils.createAbsoluteLeafRefPath(leafRefTargetPath,
154 currentNodePath, getLeafRefContextModule());
158 return absoluteLeafRefTargetPath;
161 public Module getLeafRefContextModule() {
162 final Iterator<QName> it = currentNodePath.getPathFromRoot().iterator();
163 final QNameModule qnameModule = it.hasNext() ? it.next().getModule() : currentNodeQName.getModule();
164 return schemaContext.findModule(qnameModule).orElse(null);
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;