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 com.google.common.collect.ImmutableList;
13 import java.util.HashMap;
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;
22 final class LeafRefContextBuilder implements Mutable {
23 private final Map<QName, LeafRefContext> referencingChildren = new HashMap<>();
24 private final Map<QName, LeafRefContext> referencedByChildren = new HashMap<>();
25 private final Map<QName, LeafRefContext> referencedByLeafRefCtx = new HashMap<>();
27 private final QName currentNodeQName;
28 private final ImmutableList<QName> currentNodePath;
29 private final EffectiveModelContext modelContext;
31 private LeafRefPath leafRefTargetPath = null;
32 private LeafRefPath absoluteLeafRefTargetPath = null;
33 private String leafRefTargetPathString = "";
35 private boolean isReferencedBy = false;
36 private boolean isReferencing = false;
38 LeafRefContextBuilder(final QName currentNodeQName, final ImmutableList<QName> currentNodePath,
39 final EffectiveModelContext modelContext) {
40 this.currentNodeQName = requireNonNull(currentNodeQName);
41 this.currentNodePath = requireNonNull(currentNodePath);
42 this.modelContext = requireNonNull(modelContext);
45 @NonNull 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 boolean isReferencedBy() {
57 return isReferencedBy;
60 void setReferencedBy(final boolean referencedBy) {
61 isReferencedBy = referencedBy;
64 boolean isReferencing() {
68 void setReferencing(final boolean referencing) {
69 isReferencing = referencing;
72 void addReferencingChild(final LeafRefContext child, final QName childQName) {
73 referencingChildren.put(childQName, child);
76 Map<QName, LeafRefContext> getReferencingChilds() {
77 return referencingChildren;
80 void addReferencedByChild(final LeafRefContext child, final QName childQName) {
81 referencedByChildren.put(childQName, child);
84 Map<QName, LeafRefContext> getReferencedByChilds() {
85 return referencedByChildren;
88 ImmutableList<QName> getCurrentNodePath() {
89 return currentNodePath;
92 LeafRefPath getLeafRefTargetPath() {
93 return leafRefTargetPath;
96 void setLeafRefTargetPath(final LeafRefPath leafRefPath) {
97 leafRefTargetPath = requireNonNull(leafRefPath);
100 String getLeafRefTargetPathString() {
101 return leafRefTargetPathString;
104 void setLeafRefTargetPathString(final String leafRefPathString) {
105 leafRefTargetPathString = requireNonNull(leafRefPathString);
108 QName getCurrentNodeQName() {
109 return currentNodeQName;
112 EffectiveModelContext modelContext() {
116 LeafRefPath getAbsoluteLeafRefTargetPath() {
117 if (isReferencing && absoluteLeafRefTargetPath == null) {
118 if (leafRefTargetPath.isAbsolute()) {
119 absoluteLeafRefTargetPath = leafRefTargetPath;
121 absoluteLeafRefTargetPath = LeafRefUtils.createAbsoluteLeafRefPath(leafRefTargetPath,
122 currentNodePath, getLeafRefContextModule());
126 return absoluteLeafRefTargetPath;
129 Module getLeafRefContextModule() {
130 final QNameModule qnameModule = currentNodePath.isEmpty() ? currentNodeQName.getModule()
131 : currentNodePath.get(0).getModule();
132 return modelContext.findModule(qnameModule).orElse(null);
135 void addReferencedByLeafRefCtx(final QName qname, final LeafRefContext leafRef) {
136 referencedByLeafRefCtx.put(qname, leafRef);
139 LeafRefContext getReferencedByLeafRefCtxByName(final QName qname) {
140 return referencedByLeafRefCtx.get(qname);
143 Map<QName, LeafRefContext> getAllReferencedByLeafRefCtxs() {
144 return referencedByLeafRefCtx;