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.tree.leafref;
10 import static java.util.Objects.requireNonNull;
12 import java.util.HashMap;
13 import java.util.Iterator;
15 import org.opendaylight.yangtools.concepts.Builder;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.common.QNameModule;
18 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
19 import org.opendaylight.yangtools.yang.model.api.Module;
20 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
22 final class LeafRefContextBuilder implements Builder<LeafRefContext> {
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<>();
28 private final QName currentNodeQName;
29 private final SchemaPath currentNodePath;
30 private final EffectiveModelContext schemaContext;
32 private LeafRefPath leafRefTargetPath = null;
33 private LeafRefPath absoluteLeafRefTargetPath = null;
34 private String leafRefTargetPathString = "";
36 private boolean isReferencedBy = false;
37 private boolean isReferencing = false;
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);
47 public LeafRefContext build() {
48 final LeafRefContext leafRefContext = new LeafRefContext(this);
50 // LeafRefContext has made a copy of these
51 referencingChildren.clear();
52 referencedByChildren.clear();
53 referencedByLeafRefCtx.clear();
55 return leafRefContext;
58 boolean isReferencedBy() {
59 return isReferencedBy;
62 void setReferencedBy(final boolean referencedBy) {
63 this.isReferencedBy = referencedBy;
66 boolean isReferencing() {
70 void setReferencing(final boolean referencing) {
71 this.isReferencing = referencing;
74 void addReferencingChild(final LeafRefContext child, final QName childQName) {
75 referencingChildren.put(childQName, child);
78 Map<QName, LeafRefContext> getReferencingChilds() {
79 return referencingChildren;
82 void addReferencedByChild(final LeafRefContext child, final QName childQName) {
83 referencedByChildren.put(childQName, child);
86 Map<QName, LeafRefContext> getReferencedByChilds() {
87 return referencedByChildren;
90 SchemaPath getCurrentNodePath() {
91 return currentNodePath;
94 LeafRefPath getLeafRefTargetPath() {
95 return leafRefTargetPath;
98 void setLeafRefTargetPath(final LeafRefPath leafRefPath) {
99 this.leafRefTargetPath = requireNonNull(leafRefPath);
102 String getLeafRefTargetPathString() {
103 return leafRefTargetPathString;
106 void setLeafRefTargetPathString(final String leafRefPathString) {
107 this.leafRefTargetPathString = requireNonNull(leafRefPathString);
110 QName getCurrentNodeQName() {
111 return currentNodeQName;
114 EffectiveModelContext getSchemaContext() {
115 return schemaContext;
118 LeafRefPath getAbsoluteLeafRefTargetPath() {
119 if (isReferencing && absoluteLeafRefTargetPath == null) {
120 if (leafRefTargetPath.isAbsolute()) {
121 absoluteLeafRefTargetPath = leafRefTargetPath;
123 absoluteLeafRefTargetPath = LeafRefUtils.createAbsoluteLeafRefPath(leafRefTargetPath,
124 currentNodePath, getLeafRefContextModule());
128 return absoluteLeafRefTargetPath;
131 Module getLeafRefContextModule() {
132 final Iterator<QName> it = currentNodePath.getPathFromRoot().iterator();
133 final QNameModule qnameModule = it.hasNext() ? it.next().getModule() : currentNodeQName.getModule();
134 return schemaContext.findModule(qnameModule).orElse(null);
137 void addReferencedByLeafRefCtx(final QName qname, final LeafRefContext leafRef) {
138 referencedByLeafRefCtx.put(qname, leafRef);
141 LeafRefContext getReferencedByLeafRefCtxByName(final QName qname) {
142 return referencedByLeafRefCtx.get(qname);
145 Map<QName, LeafRefContext> getAllReferencedByLeafRefCtxs() {
146 return referencedByLeafRefCtx;