Merge "Bug 1434 - Make arrays non-mutable"
[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 com.google.common.collect.ImmutableList;
11 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
12 import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath;
13 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
14 import org.opendaylight.yangtools.yang.model.util.RevisionAwareXPathImpl;
15 import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationSchemaBuilder;
16 import org.opendaylight.yangtools.yang.parser.builder.api.Builder;
17 import org.opendaylight.yangtools.yang.parser.builder.api.UnknownSchemaNodeBuilder;
18 import org.opendaylight.yangtools.yang.parser.builder.util.AbstractDocumentedDataNodeContainerBuilder;
19
20 public final class AugmentationSchemaBuilderImpl extends AbstractDocumentedDataNodeContainerBuilder implements AugmentationSchemaBuilder {
21     private final int order;
22     private AugmentationSchemaImpl instance;
23     private String whenCondition;
24
25     private final String augmentTargetStr;
26     private final SchemaPath targetPath;
27
28     private boolean resolved;
29     private boolean unsupportedTarget = false;
30
31     private AugmentationSchemaBuilder copyOf;
32
33     public AugmentationSchemaBuilderImpl(final String moduleName, final int line, final String augmentTargetStr,
34             final SchemaPath targetPath, final int order) {
35         super(moduleName, line, null);
36         this.order = order;
37         this.augmentTargetStr = augmentTargetStr;
38         this.targetPath = targetPath;
39     }
40
41     @Override
42     protected String getStatementName() {
43         return "augment";
44     }
45
46     @Override
47     public SchemaPath getPath() {
48         return targetPath;
49     }
50
51     @Override
52     public SchemaPath getTargetPath() {
53         return targetPath;
54     }
55
56     @Override
57     public AugmentationSchema build() {
58         if (instance != null) {
59             return instance;
60         }
61
62         buildChildren();
63
64         instance = new AugmentationSchemaImpl(targetPath, order,this);
65
66         Builder parent = getParent();
67         if (parent instanceof ModuleBuilder) {
68             ModuleBuilder moduleBuilder = (ModuleBuilder) parent;
69             instance.namespace = moduleBuilder.getNamespace();
70             instance.revision = moduleBuilder.getRevision();
71         }
72
73         if (copyOf != null) {
74             instance.setCopyOf(copyOf.build());
75         }
76
77         RevisionAwareXPath whenStmt;
78         if (whenCondition == null) {
79             whenStmt = null;
80         } else {
81             whenStmt = new RevisionAwareXPathImpl(whenCondition, false);
82         }
83         instance.whenCondition = whenStmt;
84
85         // UNKNOWN NODES
86         for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
87             unknownNodes.add(b.build());
88         }
89         instance.unknownNodes = ImmutableList.copyOf(unknownNodes);
90
91         return instance;
92     }
93
94     @Override
95     public boolean isResolved() {
96         return resolved;
97     }
98
99     @Override
100     public void setResolved(final boolean resolved) {
101         this.resolved = resolved;
102     }
103
104     /**
105      *  Set true if target of augment is unsupported (e.g. node in body of extension).
106      *  In such case, augmentation is skipped and AugmentationSchema is not built.
107      */
108     @Override
109     public void setUnsupportedTarget(boolean unsupportedTarget) {
110         this.unsupportedTarget = unsupportedTarget;
111     }
112
113     /**
114      *  Return true if target of augment is unsupported (e.g. node in body of extension).
115      *  In such case, augmentation is skipped and AugmentationSchema is not built.
116      */
117     @Override
118     public boolean isUnsupportedTarget() {
119         return unsupportedTarget;
120     }
121
122     @Override
123     public String getWhenCondition() {
124         return whenCondition;
125     }
126
127     @Override
128     public void addWhenCondition(final String whenCondition) {
129         this.whenCondition = whenCondition;
130     }
131
132     @Override
133     public String getTargetPathAsString() {
134         return augmentTargetStr;
135     }
136
137     @Override
138     public int getOrder() {
139         return order;
140     }
141
142     @Override
143     public int hashCode() {
144         final int prime = 17;
145         int result = 1;
146         result = prime * result + ((augmentTargetStr == null) ? 0 : augmentTargetStr.hashCode());
147         result = prime * result + ((whenCondition == null) ? 0 : whenCondition.hashCode());
148         result = prime * result + getChildNodeBuilders().hashCode();
149         return result;
150     }
151
152     @Override
153     public boolean equals(final Object obj) {
154         if (this == obj) {
155             return true;
156         }
157         if (obj == null) {
158             return false;
159         }
160         if (getClass() != obj.getClass()) {
161             return false;
162         }
163         AugmentationSchemaBuilderImpl other = (AugmentationSchemaBuilderImpl) obj;
164         if (augmentTargetStr == null) {
165             if (other.augmentTargetStr != null) {
166                 return false;
167             }
168         } else if (!augmentTargetStr.equals(other.augmentTargetStr)) {
169             return false;
170         }
171         if (whenCondition == null) {
172             if (other.whenCondition != null) {
173                 return false;
174             }
175         } else if (!whenCondition.equals(other.whenCondition)) {
176             return false;
177         }
178         if (!getChildNodeBuilders().equals(other.getChildNodeBuilders())) {
179             return false;
180         }
181         return true;
182     }
183
184     @Override
185     public String toString() {
186         return "augment " + augmentTargetStr;
187     }
188
189     public void setCopyOf(final AugmentationSchemaBuilder old) {
190         copyOf = old;
191     }
192 }