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;
15 import java.util.Date;
16 import java.util.Iterator;
17 import java.util.List;
18 import org.opendaylight.yangtools.yang.common.QName;
19 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
20 import org.opendaylight.yangtools.yang.model.api.NamespaceRevisionAware;
21 import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath;
22 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
23 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
24 import org.opendaylight.yangtools.yang.model.util.RevisionAwareXPathImpl;
25 import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationSchemaBuilder;
26 import org.opendaylight.yangtools.yang.parser.builder.api.Builder;
27 import org.opendaylight.yangtools.yang.parser.builder.api.UnknownSchemaNodeBuilder;
28 import org.opendaylight.yangtools.yang.parser.builder.util.AbstractDocumentedDataNodeContainer;
29 import org.opendaylight.yangtools.yang.parser.builder.util.AbstractDocumentedDataNodeContainerBuilder;
31 public final class AugmentationSchemaBuilderImpl extends AbstractDocumentedDataNodeContainerBuilder implements AugmentationSchemaBuilder {
32 private final int order;
33 private AugmentationSchemaImpl instance;
34 private String whenCondition;
36 private final String augmentTargetStr;
37 private final SchemaPath targetPath;
39 private boolean resolved;
40 private AugmentationSchemaBuilder copyOf;
42 public AugmentationSchemaBuilderImpl(final String moduleName, final int line, final String augmentTargetStr,
43 final SchemaPath targetPath, final int order) {
44 super(moduleName, line, null);
46 this.augmentTargetStr = augmentTargetStr;
47 this.targetPath = targetPath;
51 protected String getStatementName() {
56 public SchemaPath getPath() {
61 public SchemaPath getTargetPath() {
66 public AugmentationSchema build() {
67 if (instance != null) {
73 instance = new AugmentationSchemaImpl(targetPath, order,this);
75 Builder parent = getParent();
76 if (parent instanceof ModuleBuilder) {
77 ModuleBuilder moduleBuilder = (ModuleBuilder) parent;
78 instance.namespace = moduleBuilder.getNamespace();
79 instance.revision = moduleBuilder.getRevision();
83 instance.setCopyOf(copyOf.build());
86 RevisionAwareXPath whenStmt;
87 if (whenCondition == null) {
90 whenStmt = new RevisionAwareXPathImpl(whenCondition, false);
92 instance.whenCondition = whenStmt;
95 for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
96 unknownNodes.add(b.build());
98 instance.unknownNodes = ImmutableList.copyOf(unknownNodes);
104 public boolean isResolved() {
109 public void setResolved(final boolean resolved) {
110 this.resolved = resolved;
114 public String getWhenCondition() {
115 return whenCondition;
119 public void addWhenCondition(final String whenCondition) {
120 this.whenCondition = whenCondition;
124 public String getTargetPathAsString() {
125 return augmentTargetStr;
129 public int getOrder() {
134 public int hashCode() {
135 final int prime = 17;
137 result = prime * result + ((augmentTargetStr == null) ? 0 : augmentTargetStr.hashCode());
138 result = prime * result + ((whenCondition == null) ? 0 : whenCondition.hashCode());
139 result = prime * result + getChildNodeBuilders().hashCode();
144 public boolean equals(final Object obj) {
151 if (getClass() != obj.getClass()) {
154 AugmentationSchemaBuilderImpl other = (AugmentationSchemaBuilderImpl) obj;
155 if (augmentTargetStr == null) {
156 if (other.augmentTargetStr != null) {
159 } else if (!augmentTargetStr.equals(other.augmentTargetStr)) {
162 if (whenCondition == null) {
163 if (other.whenCondition != null) {
166 } else if (!whenCondition.equals(other.whenCondition)) {
169 if (!getChildNodeBuilders().equals(other.getChildNodeBuilders())) {
176 public String toString() {
177 return "augment " + augmentTargetStr;
180 public void setCopyOf(final AugmentationSchemaBuilder old) {
184 private static final class AugmentationSchemaImpl extends AbstractDocumentedDataNodeContainer implements AugmentationSchema, NamespaceRevisionAware, Comparable<AugmentationSchemaImpl> {
185 private final int order;
186 private final SchemaPath targetPath;
187 private RevisionAwareXPath whenCondition;
189 private URI namespace;
190 private Date revision;
191 private ImmutableList<UnknownSchemaNode> unknownNodes;
192 private AugmentationSchema copyOf;
194 public AugmentationSchemaImpl(final SchemaPath targetPath, final int order, final AugmentationSchemaBuilderImpl builder) {
196 this.targetPath = targetPath;
200 public void setCopyOf(final AugmentationSchema build) {
205 public Optional<AugmentationSchema> getOriginalDefinition() {
206 return Optional.fromNullable(this.copyOf);
210 public SchemaPath getTargetPath() {
215 public RevisionAwareXPath getWhenCondition() {
216 return whenCondition;
220 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
225 public URI getNamespace() {
230 public Date getRevision() {
235 public int hashCode() {
236 final int prime = 17;
238 result = prime * result + ((targetPath == null) ? 0 : targetPath.hashCode());
239 result = prime * result + ((whenCondition == null) ? 0 : whenCondition.hashCode());
240 result = prime * result + getChildNodes().hashCode();
245 public boolean equals(final Object obj) {
252 if (getClass() != obj.getClass()) {
255 AugmentationSchemaImpl other = (AugmentationSchemaImpl) obj;
256 if (targetPath == null) {
257 if (other.targetPath != null) {
260 } else if (!targetPath.equals(other.targetPath)) {
263 if (whenCondition == null) {
264 if (other.whenCondition != null) {
267 } else if (!whenCondition.equals(other.whenCondition)) {
270 if (!getChildNodes().equals(other.getChildNodes())) {
277 public String toString() {
278 StringBuilder sb = new StringBuilder(AugmentationSchemaImpl.class.getSimpleName());
280 sb.append("targetPath=").append(targetPath);
281 sb.append(", when=").append(whenCondition);
283 return sb.toString();
287 public int compareTo(final AugmentationSchemaImpl o) {
289 Iterator<QName> thisIt = this.targetPath.getPathFromRoot().iterator();
290 Iterator<QName> otherIt = o.getTargetPath().getPathFromRoot().iterator();
291 while (thisIt.hasNext()) {
292 if (otherIt.hasNext()) {
293 int comp = thisIt.next().compareTo(otherIt.next());
301 if (otherIt.hasNext()) {
304 return this.order - o.order;