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 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.Module;
19 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
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 SchemaContext 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 SchemaContext schemaContext) {
41 this.currentNodeQName = requireNonNull(currentNodeQName);
42 this.currentNodePath = requireNonNull(currentNodePath);
43 // FIXME: requireNonNull
44 this.schemaContext = schemaContext;
48 public LeafRefContext build() {
49 final LeafRefContext leafRefContext = new LeafRefContext(this);
51 // LeafRefContext has made a copy of these
52 referencingChildren.clear();
53 referencedByChildren.clear();
54 referencedByLeafRefCtx.clear();
56 return leafRefContext;
59 boolean isReferencedBy() {
60 return isReferencedBy;
63 void setReferencedBy(final boolean referencedBy) {
64 this.isReferencedBy = referencedBy;
67 boolean isReferencing() {
71 void setReferencing(final boolean referencing) {
72 this.isReferencing = referencing;
75 void addReferencingChild(final LeafRefContext child, final QName childQName) {
76 referencingChildren.put(childQName, child);
79 Map<QName, LeafRefContext> getReferencingChilds() {
80 return referencingChildren;
83 void addReferencedByChild(final LeafRefContext child, final QName childQName) {
84 referencedByChildren.put(childQName, child);
87 Map<QName, LeafRefContext> getReferencedByChilds() {
88 return referencedByChildren;
91 SchemaPath getCurrentNodePath() {
92 return currentNodePath;
95 LeafRefPath getLeafRefTargetPath() {
96 return leafRefTargetPath;
99 void setLeafRefTargetPath(final LeafRefPath leafRefPath) {
100 this.leafRefTargetPath = requireNonNull(leafRefPath);
103 String getLeafRefTargetPathString() {
104 return leafRefTargetPathString;
107 void setLeafRefTargetPathString(final String leafRefPathString) {
108 this.leafRefTargetPathString = requireNonNull(leafRefPathString);
111 QName getCurrentNodeQName() {
112 return currentNodeQName;
115 SchemaContext getSchemaContext() {
116 return schemaContext;
119 LeafRefPath getAbsoluteLeafRefTargetPath() {
120 if (isReferencing && absoluteLeafRefTargetPath == null) {
121 if (leafRefTargetPath.isAbsolute()) {
122 absoluteLeafRefTargetPath = leafRefTargetPath;
124 absoluteLeafRefTargetPath = LeafRefUtils.createAbsoluteLeafRefPath(leafRefTargetPath,
125 currentNodePath, getLeafRefContextModule());
129 return absoluteLeafRefTargetPath;
132 Module getLeafRefContextModule() {
133 final Iterator<QName> it = currentNodePath.getPathFromRoot().iterator();
134 final QNameModule qnameModule = it.hasNext() ? it.next().getModule() : currentNodeQName.getModule();
135 return schemaContext.findModule(qnameModule).orElse(null);
138 void addReferencedByLeafRefCtx(final QName qname, final LeafRefContext leafRef) {
139 referencedByLeafRefCtx.put(qname, leafRef);
142 LeafRefContext getReferencedByLeafRefCtxByName(final QName qname) {
143 return referencedByLeafRefCtx.get(qname);
146 Map<QName, LeafRefContext> getAllReferencedByLeafRefCtxs() {
147 return referencedByLeafRefCtx;