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.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;
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<>();
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);
46 @NonNull LeafRefContext build() {
47 final LeafRefContext leafRefContext = new LeafRefContext(this);
49 // LeafRefContext has made a copy of these
50 referencingChildren.clear();
51 referencedByChildren.clear();
52 referencedByLeafRefCtx.clear();
54 return leafRefContext;
57 boolean isReferencedBy() {
58 return isReferencedBy;
61 void setReferencedBy(final boolean referencedBy) {
62 isReferencedBy = referencedBy;
65 boolean isReferencing() {
69 void setReferencing(final boolean referencing) {
70 isReferencing = referencing;
73 void addReferencingChild(final LeafRefContext child, final QName childQName) {
74 referencingChildren.put(childQName, child);
77 Map<QName, LeafRefContext> getReferencingChilds() {
78 return referencingChildren;
81 void addReferencedByChild(final LeafRefContext child, final QName childQName) {
82 referencedByChildren.put(childQName, child);
85 Map<QName, LeafRefContext> getReferencedByChilds() {
86 return referencedByChildren;
89 SchemaPath getCurrentNodePath() {
90 return currentNodePath;
93 LeafRefPath getLeafRefTargetPath() {
94 return leafRefTargetPath;
97 void setLeafRefTargetPath(final LeafRefPath leafRefPath) {
98 leafRefTargetPath = requireNonNull(leafRefPath);
101 String getLeafRefTargetPathString() {
102 return leafRefTargetPathString;
105 void setLeafRefTargetPathString(final String leafRefPathString) {
106 leafRefTargetPathString = requireNonNull(leafRefPathString);
109 QName getCurrentNodeQName() {
110 return currentNodeQName;
113 EffectiveModelContext getSchemaContext() {
114 return schemaContext;
117 LeafRefPath getAbsoluteLeafRefTargetPath() {
118 if (isReferencing && absoluteLeafRefTargetPath == null) {
119 if (leafRefTargetPath.isAbsolute()) {
120 absoluteLeafRefTargetPath = leafRefTargetPath;
122 absoluteLeafRefTargetPath = LeafRefUtils.createAbsoluteLeafRefPath(leafRefTargetPath,
123 currentNodePath, getLeafRefContextModule());
127 return absoluteLeafRefTargetPath;
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);
136 void addReferencedByLeafRefCtx(final QName qname, final LeafRefContext leafRef) {
137 referencedByLeafRefCtx.put(qname, leafRef);
140 LeafRefContext getReferencedByLeafRefCtxByName(final QName qname) {
141 return referencedByLeafRefCtx.get(qname);
144 Map<QName, LeafRefContext> getAllReferencedByLeafRefCtxs() {
145 return referencedByLeafRefCtx;