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