Merge "Bug 1372 - toString methods in generated classes"
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / builder / impl / AugmentationSchemaBuilderImpl.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.parser.builder.impl;
9
10 import static com.google.common.base.Preconditions.checkNotNull;
11
12 import com.google.common.base.Optional;
13 import com.google.common.collect.ImmutableList;
14 import java.net.URI;
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;
30
31 public final class AugmentationSchemaBuilderImpl extends AbstractDocumentedDataNodeContainerBuilder implements AugmentationSchemaBuilder {
32     private final int order;
33     private AugmentationSchemaImpl instance;
34     private String whenCondition;
35
36     private final String augmentTargetStr;
37     private final SchemaPath targetPath;
38
39     private boolean resolved;
40     private AugmentationSchemaBuilder copyOf;
41
42     public AugmentationSchemaBuilderImpl(final String moduleName, final int line, final String augmentTargetStr,
43             final SchemaPath targetPath, final int order) {
44         super(moduleName, line, null);
45         this.order = order;
46         this.augmentTargetStr = augmentTargetStr;
47         this.targetPath = targetPath;
48     }
49
50     @Override
51     protected String getStatementName() {
52         return "augment";
53     }
54
55     @Override
56     public SchemaPath getPath() {
57         return targetPath;
58     }
59
60     @Override
61     public SchemaPath getTargetPath() {
62         return targetPath;
63     }
64
65     @Override
66     public AugmentationSchema build() {
67         if (instance != null) {
68             return instance;
69         }
70
71         buildChildren();
72
73         instance = new AugmentationSchemaImpl(targetPath, order,this);
74
75         Builder parent = getParent();
76         if (parent instanceof ModuleBuilder) {
77             ModuleBuilder moduleBuilder = (ModuleBuilder) parent;
78             instance.namespace = moduleBuilder.getNamespace();
79             instance.revision = moduleBuilder.getRevision();
80         }
81
82         if (copyOf != null) {
83             instance.setCopyOf(copyOf.build());
84         }
85
86         RevisionAwareXPath whenStmt;
87         if (whenCondition == null) {
88             whenStmt = null;
89         } else {
90             whenStmt = new RevisionAwareXPathImpl(whenCondition, false);
91         }
92         instance.whenCondition = whenStmt;
93
94         // UNKNOWN NODES
95         for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
96             unknownNodes.add(b.build());
97         }
98         instance.unknownNodes = ImmutableList.copyOf(unknownNodes);
99
100         return instance;
101     }
102
103     @Override
104     public boolean isResolved() {
105         return resolved;
106     }
107
108     @Override
109     public void setResolved(final boolean resolved) {
110         this.resolved = resolved;
111     }
112
113     @Override
114     public String getWhenCondition() {
115         return whenCondition;
116     }
117
118     @Override
119     public void addWhenCondition(final String whenCondition) {
120         this.whenCondition = whenCondition;
121     }
122
123     @Override
124     public String getTargetPathAsString() {
125         return augmentTargetStr;
126     }
127
128     @Override
129     public int getOrder() {
130         return order;
131     }
132
133     @Override
134     public int hashCode() {
135         final int prime = 17;
136         int result = 1;
137         result = prime * result + ((augmentTargetStr == null) ? 0 : augmentTargetStr.hashCode());
138         result = prime * result + ((whenCondition == null) ? 0 : whenCondition.hashCode());
139         result = prime * result + getChildNodeBuilders().hashCode();
140         return result;
141     }
142
143     @Override
144     public boolean equals(final Object obj) {
145         if (this == obj) {
146             return true;
147         }
148         if (obj == null) {
149             return false;
150         }
151         if (getClass() != obj.getClass()) {
152             return false;
153         }
154         AugmentationSchemaBuilderImpl other = (AugmentationSchemaBuilderImpl) obj;
155         if (augmentTargetStr == null) {
156             if (other.augmentTargetStr != null) {
157                 return false;
158             }
159         } else if (!augmentTargetStr.equals(other.augmentTargetStr)) {
160             return false;
161         }
162         if (whenCondition == null) {
163             if (other.whenCondition != null) {
164                 return false;
165             }
166         } else if (!whenCondition.equals(other.whenCondition)) {
167             return false;
168         }
169         if (!getChildNodeBuilders().equals(other.getChildNodeBuilders())) {
170             return false;
171         }
172         return true;
173     }
174
175     @Override
176     public String toString() {
177         return "augment " + augmentTargetStr;
178     }
179
180     public void setCopyOf(final AugmentationSchemaBuilder old) {
181         copyOf = old;
182     }
183
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;
188
189         private URI namespace;
190         private Date revision;
191         private ImmutableList<UnknownSchemaNode> unknownNodes;
192         private AugmentationSchema copyOf;
193
194         public AugmentationSchemaImpl(final SchemaPath targetPath, final int order, final AugmentationSchemaBuilderImpl builder) {
195             super(builder);
196             this.targetPath = targetPath;
197             this.order = order;
198         }
199
200         public void setCopyOf(final AugmentationSchema build) {
201             this.copyOf = build;
202         }
203
204         @Override
205         public Optional<AugmentationSchema> getOriginalDefinition() {
206             return Optional.fromNullable(this.copyOf);
207         }
208
209         @Override
210         public SchemaPath getTargetPath() {
211             return targetPath;
212         }
213
214         @Override
215         public RevisionAwareXPath getWhenCondition() {
216             return whenCondition;
217         }
218
219         @Override
220         public List<UnknownSchemaNode> getUnknownSchemaNodes() {
221             return unknownNodes;
222         }
223
224         @Override
225         public URI getNamespace() {
226             return namespace;
227         }
228
229         @Override
230         public Date getRevision() {
231             return revision;
232         }
233
234         @Override
235         public int hashCode() {
236             final int prime = 17;
237             int result = 1;
238             result = prime * result + ((targetPath == null) ? 0 : targetPath.hashCode());
239             result = prime * result + ((whenCondition == null) ? 0 : whenCondition.hashCode());
240             result = prime * result + getChildNodes().hashCode();
241             return result;
242         }
243
244         @Override
245         public boolean equals(final Object obj) {
246             if (this == obj) {
247                 return true;
248             }
249             if (obj == null) {
250                 return false;
251             }
252             if (getClass() != obj.getClass()) {
253                 return false;
254             }
255             AugmentationSchemaImpl other = (AugmentationSchemaImpl) obj;
256             if (targetPath == null) {
257                 if (other.targetPath != null) {
258                     return false;
259                 }
260             } else if (!targetPath.equals(other.targetPath)) {
261                 return false;
262             }
263             if (whenCondition == null) {
264                 if (other.whenCondition != null) {
265                     return false;
266                 }
267             } else if (!whenCondition.equals(other.whenCondition)) {
268                 return false;
269             }
270             if (!getChildNodes().equals(other.getChildNodes())) {
271                 return false;
272             }
273             return true;
274         }
275
276         @Override
277         public String toString() {
278             StringBuilder sb = new StringBuilder(AugmentationSchemaImpl.class.getSimpleName());
279             sb.append("[");
280             sb.append("targetPath=").append(targetPath);
281             sb.append(", when=").append(whenCondition);
282             sb.append("]");
283             return sb.toString();
284         }
285
286         @Override
287         public int compareTo(final AugmentationSchemaImpl o) {
288             checkNotNull(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());
294                     if (comp != 0) {
295                         return comp;
296                     }
297                 } else {
298                     return 1;
299                 }
300             }
301             if (otherIt.hasNext()) {
302                 return -1;
303             }
304             return this.order - o.order;
305         }
306     }
307 }