61b7dab27b3edce8ef0eab087e1cff598e0c3b4e
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / builder / impl / ContainerSchemaNodeBuilder.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.base.Preconditions;
11 import com.google.common.collect.ImmutableList;
12 import com.google.common.collect.ImmutableSet;
13 import java.util.ArrayList;
14 import java.util.List;
15 import java.util.Objects;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
18 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
19 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
20 import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationSchemaBuilder;
21 import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationTargetBuilder;
22 import org.opendaylight.yangtools.yang.parser.builder.api.ConstraintsBuilder;
23 import org.opendaylight.yangtools.yang.parser.builder.api.DataSchemaNodeBuilder;
24 import org.opendaylight.yangtools.yang.parser.builder.api.SchemaNodeBuilder;
25 import org.opendaylight.yangtools.yang.parser.builder.api.UnknownSchemaNodeBuilder;
26 import org.opendaylight.yangtools.yang.parser.builder.util.AbstractDocumentedDataNodeContainerBuilder;
27
28 public final class ContainerSchemaNodeBuilder extends AbstractDocumentedDataNodeContainerBuilder implements
29         AugmentationTargetBuilder, DataSchemaNodeBuilder {
30     private ContainerSchemaNodeImpl instance;
31     private boolean presence;
32     // SchemaNode args
33     private SchemaPath path;
34     // DataSchemaNode args
35     private boolean augmenting;
36     private boolean addedByUses;
37     private boolean configuration;
38     private ContainerSchemaNode originalNode;
39     private ContainerSchemaNodeBuilder originalBuilder;
40     private final ConstraintsBuilder constraints;
41     // AugmentationTarget args
42     private final List<AugmentationSchema> augmentations = new ArrayList<>();
43     private final List<AugmentationSchemaBuilder> augmentationBuilders = new ArrayList<>();
44
45     public ContainerSchemaNodeBuilder(final String moduleName, final int line, final QName qname, final SchemaPath path) {
46         super(moduleName, line, qname);
47         this.path = Preconditions.checkNotNull(path, "Schema Path must not be null");
48         this.constraints = new ConstraintsBuilderImpl(moduleName, line);
49     }
50
51     // constructor for uses
52     public ContainerSchemaNodeBuilder(final String moduleName, final int line, final QName qname,
53             final SchemaPath path, final ContainerSchemaNode base) {
54         super(moduleName, line, qname, path, base);
55         this.path = Preconditions.checkNotNull(path, "Schema Path must not be null");
56
57         constraints = new ConstraintsBuilderImpl(moduleName, line, base.getConstraints());
58
59         augmenting = base.isAugmenting();
60         addedByUses = base.isAddedByUses();
61         originalNode = base;
62         configuration = base.isConfiguration();
63         presence = base.isPresenceContainer();
64
65         augmentations.addAll(base.getAvailableAugmentations());
66
67     }
68
69     @Override
70     protected String getStatementName() {
71         return "container";
72     }
73
74     @Override
75     public ContainerSchemaNode build() {
76         if (instance != null) {
77             return instance;
78         }
79
80         buildChildren();
81         instance = new ContainerSchemaNodeImpl(this);
82
83         instance.augmenting = augmenting;
84         instance.addedByUses = addedByUses;
85         instance.configuration = configuration;
86         instance.constraints = constraints.build();
87         instance.presence = presence;
88
89         // ORIGINAL NODE
90         if (originalNode == null && originalBuilder != null) {
91             originalNode = originalBuilder.build();
92         }
93         instance.original = originalNode;
94
95         // AUGMENTATIONS
96         for (AugmentationSchemaBuilder builder : augmentationBuilders) {
97             augmentations.add(builder.build());
98         }
99         instance.augmentations = ImmutableSet.copyOf(augmentations);
100
101         // UNKNOWN NODES
102         for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
103             unknownNodes.add(b.build());
104         }
105         instance.unknownNodes = ImmutableList.copyOf(unknownNodes);
106
107         return instance;
108     }
109
110     public List<AugmentationSchemaBuilder> getAugmentationBuilders() {
111         return augmentationBuilders;
112     }
113
114     @Override
115     public void addAugmentation(final AugmentationSchemaBuilder augment) {
116         augmentationBuilders.add(augment);
117     }
118
119     @Override
120     public SchemaPath getPath() {
121         return path;
122     }
123
124     @Override
125     public void setPath(final SchemaPath path) {
126         this.path = path;
127     }
128
129     @Override
130     public boolean isAugmenting() {
131         return augmenting;
132     }
133
134     @Override
135     public void setAugmenting(final boolean augmenting) {
136         this.augmenting = augmenting;
137     }
138
139     @Override
140     public boolean isAddedByUses() {
141         return addedByUses;
142     }
143
144     @Override
145     public void setAddedByUses(final boolean addedByUses) {
146         this.addedByUses = addedByUses;
147     }
148
149     @Override
150     public ContainerSchemaNodeBuilder getOriginal() {
151         return originalBuilder;
152     }
153
154     @Override
155     public void setOriginal(final SchemaNodeBuilder builder) {
156         Preconditions.checkArgument(builder instanceof ContainerSchemaNodeBuilder, "Original of container cannot be "
157                 + builder);
158         this.originalBuilder = (ContainerSchemaNodeBuilder) builder;
159     }
160
161     @Override
162     public boolean isConfiguration() {
163         return configuration;
164     }
165
166     @Override
167     public void setConfiguration(final boolean configuration) {
168         this.configuration = configuration;
169     }
170
171     @Override
172     public ConstraintsBuilder getConstraints() {
173         return constraints;
174     }
175
176     public boolean isPresence() {
177         return presence;
178     }
179
180     public void setPresence(final boolean presence) {
181         this.presence = presence;
182     }
183
184     @Override
185     public int hashCode() {
186         final int prime = 31;
187         int result = 1;
188         result = prime * result + Objects.hashCode(path);
189         return result;
190     }
191
192     @Override
193     public boolean equals(final Object obj) {
194         if (this == obj) {
195             return true;
196         }
197         if (obj == null) {
198             return false;
199         }
200         if (getClass() != obj.getClass()) {
201             return false;
202         }
203         ContainerSchemaNodeBuilder other = (ContainerSchemaNodeBuilder) obj;
204         if (path == null) {
205             if (other.path != null) {
206                 return false;
207             }
208         } else if (!path.equals(other.path)) {
209             return false;
210         }
211         if (getParent() == null) {
212             if (other.getParent() != null) {
213                 return false;
214             }
215         } else if (!getParent().equals(other.getParent())) {
216             return false;
217         }
218         return true;
219     }
220
221     @Override
222     public String toString() {
223         return "container " + qname.getLocalName();
224     }
225
226 }