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;
10 import static com.google.common.base.Preconditions.checkNotNull;
12 import com.google.common.base.Optional;
13 import com.google.common.collect.ImmutableList;
16 import java.util.ArrayList;
17 import java.util.Date;
18 import java.util.Iterator;
19 import java.util.List;
21 import org.opendaylight.yangtools.yang.common.QName;
22 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
23 import org.opendaylight.yangtools.yang.model.api.NamespaceRevisionAware;
24 import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath;
25 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
26 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
27 import org.opendaylight.yangtools.yang.model.util.RevisionAwareXPathImpl;
28 import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationSchemaBuilder;
29 import org.opendaylight.yangtools.yang.parser.builder.api.Builder;
30 import org.opendaylight.yangtools.yang.parser.builder.api.UnknownSchemaNodeBuilder;
31 import org.opendaylight.yangtools.yang.parser.builder.api.UsesNodeBuilder;
32 import org.opendaylight.yangtools.yang.parser.builder.util.AbstractDocumentedDataNodeContainer;
33 import org.opendaylight.yangtools.yang.parser.builder.util.AbstractDocumentedDataNodeContainerBuilder;
35 public final class AugmentationSchemaBuilderImpl extends AbstractDocumentedDataNodeContainerBuilder implements 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);
83 List<QName> newPath = new ArrayList<>();
84 for (QName name : targetPath.getPathFromRoot()) {
85 newPath.add(QName.create(mb.getQNameModule(), name.getPrefix(), name.getLocalName()));
87 instance.targetPath = SchemaPath.create(newPath, false);
89 instance.targetPath = targetNodeSchemaPath;
93 instance.setCopyOf(copyOf.build());
96 RevisionAwareXPath whenStmt;
97 if (whenCondition == null) {
100 whenStmt = new RevisionAwareXPathImpl(whenCondition, false);
102 instance.whenCondition = whenStmt;
105 for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
106 unknownNodes.add(b.build());
108 instance.unknownNodes = ImmutableList.copyOf(unknownNodes);
114 public boolean isResolved() {
119 public void setResolved(final boolean resolved) {
120 this.resolved = resolved;
124 public String getWhenCondition() {
125 return whenCondition;
129 public void addWhenCondition(final String whenCondition) {
130 this.whenCondition = whenCondition;
134 public String getTargetPathAsString() {
135 return augmentTargetStr;
139 public SchemaPath getTargetPath() {
144 public SchemaPath getTargetNodeSchemaPath() {
145 return targetNodeSchemaPath;
149 public void setTargetNodeSchemaPath(final SchemaPath path) {
150 this.targetNodeSchemaPath = path;
154 public int getOrder() {
159 public int hashCode() {
160 final int prime = 17;
162 result = prime * result + ((augmentTargetStr == null) ? 0 : augmentTargetStr.hashCode());
163 result = prime * result + ((whenCondition == null) ? 0 : whenCondition.hashCode());
164 result = prime * result + getChildNodeBuilders().hashCode();
169 public boolean equals(final Object obj) {
176 if (getClass() != obj.getClass()) {
179 AugmentationSchemaBuilderImpl other = (AugmentationSchemaBuilderImpl) obj;
180 if (augmentTargetStr == null) {
181 if (other.augmentTargetStr != null) {
184 } else if (!augmentTargetStr.equals(other.augmentTargetStr)) {
187 if (whenCondition == null) {
188 if (other.whenCondition != null) {
191 } else if (!whenCondition.equals(other.whenCondition)) {
194 if (!getChildNodeBuilders().equals(other.getChildNodeBuilders())) {
201 public String toString() {
202 return "augment " + augmentTargetStr;
205 public void setCopyOf(final AugmentationSchemaBuilder old) {
209 private static final class AugmentationSchemaImpl extends AbstractDocumentedDataNodeContainer implements AugmentationSchema, NamespaceRevisionAware, Comparable<AugmentationSchemaImpl> {
210 private final int order;
211 private SchemaPath targetPath;
212 private RevisionAwareXPath whenCondition;
214 private URI namespace;
215 private Date revision;
216 private ImmutableList<UnknownSchemaNode> unknownNodes;
217 private AugmentationSchema copyOf;
219 public AugmentationSchemaImpl(final SchemaPath targetPath, final int order, final AugmentationSchemaBuilderImpl builder) {
221 this.targetPath = targetPath;
225 public void setCopyOf(final AugmentationSchema build) {
230 public Optional<AugmentationSchema> getOriginalDefinition() {
231 return Optional.fromNullable(this.copyOf);
235 public SchemaPath getTargetPath() {
240 public RevisionAwareXPath getWhenCondition() {
241 return whenCondition;
245 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
250 public URI getNamespace() {
255 public Date getRevision() {
260 public int hashCode() {
261 final int prime = 17;
263 result = prime * result + ((targetPath == null) ? 0 : targetPath.hashCode());
264 result = prime * result + ((whenCondition == null) ? 0 : whenCondition.hashCode());
265 result = prime * result + getChildNodes().hashCode();
270 public boolean equals(final Object obj) {
277 if (getClass() != obj.getClass()) {
280 AugmentationSchemaImpl other = (AugmentationSchemaImpl) obj;
281 if (targetPath == null) {
282 if (other.targetPath != null) {
285 } else if (!targetPath.equals(other.targetPath)) {
288 if (whenCondition == null) {
289 if (other.whenCondition != null) {
292 } else if (!whenCondition.equals(other.whenCondition)) {
295 if (!getChildNodes().equals(other.getChildNodes())) {
302 public String toString() {
303 StringBuilder sb = new StringBuilder(AugmentationSchemaImpl.class.getSimpleName());
305 sb.append("targetPath=").append(targetPath);
306 sb.append(", when=").append(whenCondition);
308 return sb.toString();
312 public int compareTo(final AugmentationSchemaImpl o) {
314 Iterator<QName> thisIt = this.targetPath.getPathFromRoot().iterator();
315 Iterator<QName> otherIt = o.getTargetPath().getPathFromRoot().iterator();
316 while (thisIt.hasNext()) {
317 if (otherIt.hasNext()) {
318 int comp = thisIt.next().compareTo(otherIt.next());
326 if (otherIt.hasNext()) {
329 return this.order - o.order;