2 * Copyright (c) 2013 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.parser.builder.impl;
11 import java.util.ArrayList;
12 import java.util.Date;
13 import java.util.Iterator;
14 import java.util.List;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.common.QNameModule;
18 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
19 import org.opendaylight.yangtools.yang.model.api.NamespaceRevisionAware;
20 import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath;
21 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
22 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
23 import org.opendaylight.yangtools.yang.model.util.RevisionAwareXPathImpl;
24 import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationSchemaBuilder;
25 import org.opendaylight.yangtools.yang.parser.builder.api.Builder;
26 import org.opendaylight.yangtools.yang.parser.builder.api.UnknownSchemaNodeBuilder;
27 import org.opendaylight.yangtools.yang.parser.builder.api.UsesNodeBuilder;
28 import org.opendaylight.yangtools.yang.parser.builder.util.AbstractDocumentedDataNodeContainer;
29 import org.opendaylight.yangtools.yang.parser.builder.util.AbstractDocumentedDataNodeContainerBuilder;
31 import com.google.common.base.Optional;
32 import com.google.common.collect.ImmutableList;
34 public final class AugmentationSchemaBuilderImpl extends AbstractDocumentedDataNodeContainerBuilder implements
35 AugmentationSchemaBuilder {
36 private final int order;
37 private AugmentationSchemaImpl instance;
38 private String whenCondition;
40 private final String augmentTargetStr;
41 private final SchemaPath targetPath;
42 private SchemaPath targetNodeSchemaPath;
44 private boolean resolved;
45 private AugmentationSchemaBuilder copyOf;
47 public AugmentationSchemaBuilderImpl(final String moduleName, final int line, final String augmentTargetStr, final int order) {
48 super(moduleName, line, null);
50 this.augmentTargetStr = augmentTargetStr;
51 targetPath = BuilderUtils.parseXPathString(augmentTargetStr);
55 protected String getStatementName() {
60 public SchemaPath getPath() {
61 return targetNodeSchemaPath;
65 public AugmentationSchema build() {
66 if (instance != null) {
71 instance = new AugmentationSchemaImpl(targetPath, order,this);
73 Builder parent = getParent();
74 if (parent instanceof ModuleBuilder) {
75 ModuleBuilder moduleBuilder = (ModuleBuilder) parent;
76 instance.namespace = moduleBuilder.getNamespace();
77 instance.revision = moduleBuilder.getRevision();
80 if (parent instanceof UsesNodeBuilder) {
81 final ModuleBuilder mb = BuilderUtils.getParentModule(this);
82 final QNameModule qm = QNameModule.create(mb.getNamespace(), mb.getRevision());
84 List<QName> newPath = new ArrayList<>();
85 for (QName name : targetPath.getPathFromRoot()) {
86 newPath.add(QName.create(qm, name.getPrefix(), name.getLocalName()));
88 instance.targetPath = SchemaPath.create(newPath, false);
90 instance.targetPath = targetNodeSchemaPath;
94 instance.setCopyOf(copyOf.build());
97 RevisionAwareXPath whenStmt;
98 if (whenCondition == null) {
101 whenStmt = new RevisionAwareXPathImpl(whenCondition, false);
103 instance.whenCondition = whenStmt;
106 for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
107 unknownNodes.add(b.build());
109 instance.unknownNodes = ImmutableList.copyOf(unknownNodes);
115 public boolean isResolved() {
120 public void setResolved(final boolean resolved) {
121 this.resolved = resolved;
125 public String getWhenCondition() {
126 return whenCondition;
130 public void addWhenCondition(final String whenCondition) {
131 this.whenCondition = whenCondition;
135 public String getTargetPathAsString() {
136 return augmentTargetStr;
140 public SchemaPath getTargetPath() {
145 public SchemaPath getTargetNodeSchemaPath() {
146 return targetNodeSchemaPath;
150 public void setTargetNodeSchemaPath(final SchemaPath path) {
151 this.targetNodeSchemaPath = path;
155 public int getOrder() {
160 public int hashCode() {
161 final int prime = 17;
163 result = prime * result + ((augmentTargetStr == null) ? 0 : augmentTargetStr.hashCode());
164 result = prime * result + ((whenCondition == null) ? 0 : whenCondition.hashCode());
165 result = prime * result + getChildNodeBuilders().hashCode();
170 public boolean equals(final Object obj) {
177 if (getClass() != obj.getClass()) {
180 AugmentationSchemaBuilderImpl other = (AugmentationSchemaBuilderImpl) obj;
181 if (augmentTargetStr == null) {
182 if (other.augmentTargetStr != null) {
185 } else if (!augmentTargetStr.equals(other.augmentTargetStr)) {
188 if (whenCondition == null) {
189 if (other.whenCondition != null) {
192 } else if (!whenCondition.equals(other.whenCondition)) {
195 if (other.getChildNodeBuilders() == null) {
196 if (other.getChildNodeBuilders() != null) {
199 } else if (!getChildNodeBuilders().equals(other.getChildNodeBuilders())) {
206 public String toString() {
207 return "augment " + augmentTargetStr;
210 public void setCopyOf(final AugmentationSchemaBuilder old) {
214 private static final class AugmentationSchemaImpl extends AbstractDocumentedDataNodeContainer implements AugmentationSchema, NamespaceRevisionAware,
215 Comparable<AugmentationSchemaImpl> {
216 private final int order;
217 private SchemaPath targetPath;
218 private RevisionAwareXPath whenCondition;
220 private URI namespace;
221 private Date revision;
222 private ImmutableList<UnknownSchemaNode> unknownNodes;
223 private AugmentationSchema copyOf;
225 public AugmentationSchemaImpl(final SchemaPath targetPath, final int order, final AugmentationSchemaBuilderImpl builder) {
227 this.targetPath = targetPath;
231 public void setCopyOf(final AugmentationSchema build) {
236 public Optional<AugmentationSchema> getOriginalDefinition() {
237 return Optional.fromNullable(this.copyOf);
241 public SchemaPath getTargetPath() {
246 public RevisionAwareXPath getWhenCondition() {
247 return whenCondition;
251 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
256 public URI getNamespace() {
261 public Date getRevision() {
266 public int hashCode() {
267 final int prime = 17;
269 result = prime * result + ((targetPath == null) ? 0 : targetPath.hashCode());
270 result = prime * result + ((whenCondition == null) ? 0 : whenCondition.hashCode());
271 result = getChildNodes().hashCode();
276 public boolean equals(final Object obj) {
283 if (getClass() != obj.getClass()) {
286 AugmentationSchemaImpl other = (AugmentationSchemaImpl) obj;
287 if (targetPath == null) {
288 if (other.targetPath != null) {
291 } else if (!targetPath.equals(other.targetPath)) {
294 if (whenCondition == null) {
295 if (other.whenCondition != null) {
298 } else if (!whenCondition.equals(other.whenCondition)) {
301 if (!getChildNodes().equals(other.getChildNodes())) {
308 public String toString() {
309 StringBuilder sb = new StringBuilder(AugmentationSchemaImpl.class.getSimpleName());
311 sb.append("targetPath=" + targetPath);
312 sb.append(", when=" + whenCondition);
314 return sb.toString();
318 public int compareTo(final AugmentationSchemaImpl o) {
319 Iterator<QName> thisIt = this.targetPath.getPathFromRoot().iterator();
320 Iterator<QName> otherIt = o.getTargetPath().getPathFromRoot().iterator();
321 while (thisIt.hasNext()) {
322 if (otherIt.hasNext()) {
323 int comp = thisIt.next().compareTo(otherIt.next());
331 if (otherIt.hasNext()) {
334 return this.order - o.order;