Merge "Six more Equals/HashCode/StringBuilder replacements"
[controller.git] / opendaylight / sal / yang-prototype / code-generator / yang-model-parser-impl / src / main / java / org / opendaylight / controller / yang / parser / builder / impl / UnionTypeBuilder.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.controller.yang.parser.builder.impl;
9
10 import java.net.URI;
11 import java.util.ArrayList;
12 import java.util.Collections;
13 import java.util.Date;
14 import java.util.List;
15
16 import org.opendaylight.controller.yang.common.QName;
17 import org.opendaylight.controller.yang.model.api.SchemaPath;
18 import org.opendaylight.controller.yang.model.api.Status;
19 import org.opendaylight.controller.yang.model.api.TypeDefinition;
20 import org.opendaylight.controller.yang.model.api.type.LengthConstraint;
21 import org.opendaylight.controller.yang.model.api.type.PatternConstraint;
22 import org.opendaylight.controller.yang.model.api.type.RangeConstraint;
23 import org.opendaylight.controller.yang.model.util.UnionType;
24 import org.opendaylight.controller.yang.parser.builder.api.AbstractTypeAwareBuilder;
25 import org.opendaylight.controller.yang.parser.builder.api.Builder;
26 import org.opendaylight.controller.yang.parser.builder.api.TypeDefinitionBuilder;
27
28 /**
29  * Builder for YANG union type. User can add type to this union as
30  * TypeDefinition object (resolved type) or in form of TypeDefinitionBuilder.
31  * When build is called, types in builder form will be built and add to resolved
32  * types.
33  */
34 public class UnionTypeBuilder extends AbstractTypeAwareBuilder implements
35         TypeDefinitionBuilder, Builder {
36     private final static String CLASS_NAME = UnionTypeBuilder.class
37             .getSimpleName();
38
39     private final int line;
40     private final List<TypeDefinition<?>> types;
41     private final List<TypeDefinitionBuilder> typedefs;
42     private final UnionType instance;
43     private boolean built;
44
45     private final List<String> actualPath;
46     private final URI namespace;
47     private final Date revision;
48
49     public UnionTypeBuilder(final List<String> actualPath, final URI namespace,
50             final Date revision, final int line) {
51         this.line = line;
52         types = new ArrayList<TypeDefinition<?>>();
53         typedefs = new ArrayList<TypeDefinitionBuilder>();
54         instance = new UnionType(actualPath, namespace, revision, types);
55
56         this.actualPath = actualPath;
57         this.namespace = namespace;
58         this.revision = revision;
59     }
60
61     @Override
62     public int getLine() {
63         return line;
64     }
65
66     public List<TypeDefinition<?>> getTypes() {
67         return types;
68     }
69
70     @Override
71     public TypeDefinition<?> getType() {
72         return null;
73     }
74
75     public List<TypeDefinitionBuilder> getTypedefs() {
76         return Collections.unmodifiableList(typedefs);
77     }
78
79     @Override
80     public TypeDefinitionBuilder getTypedef() {
81         return null;
82     }
83
84     @Override
85     public void setType(final TypeDefinition<?> type) {
86         types.add(type);
87     }
88
89     @Override
90     public void setType(final TypeDefinitionBuilder tdb) {
91         typedefs.add(tdb);
92     }
93
94     @Override
95     public UnionType build() {
96         if (built) {
97             return instance;
98         } else {
99             for (TypeDefinitionBuilder tdb : typedefs) {
100                 types.add(tdb.build());
101             }
102             built = true;
103             return instance;
104         }
105     }
106
107     @Override
108     public void setPath(final SchemaPath schemaPath) {
109         throw new IllegalStateException("Can not set path to " + CLASS_NAME);
110     }
111
112     @Override
113     public void setDescription(final String description) {
114         throw new IllegalStateException("Can not set description to "
115                 + CLASS_NAME);
116     }
117
118     @Override
119     public void setReference(final String reference) {
120         throw new IllegalStateException("Can not set reference to "
121                 + CLASS_NAME);
122     }
123
124     @Override
125     public void setStatus(final Status status) {
126         throw new IllegalStateException("Can not set status to " + CLASS_NAME);
127     }
128
129     @Override
130     public void addUnknownSchemaNode(final UnknownSchemaNodeBuilder unknownNode) {
131         throw new IllegalStateException("Can not add unknown node to "
132                 + CLASS_NAME);
133     }
134
135     @Override
136     public QName getQName() {
137         return null;
138     }
139
140     @Override
141     public SchemaPath getPath() {
142         return null;
143     }
144
145     @Override
146     public String getDescription() {
147         return null;
148     }
149
150     @Override
151     public String getReference() {
152         return null;
153     }
154
155     @Override
156     public Status getStatus() {
157         return null;
158     }
159
160     @Override
161     public List<RangeConstraint> getRanges() {
162         return Collections.emptyList();
163     }
164
165     @Override
166     public void setRanges(List<RangeConstraint> ranges) {
167         throw new IllegalStateException("Can not set ranges to "
168                 + UnionTypeBuilder.class.getSimpleName());
169     }
170
171     @Override
172     public List<LengthConstraint> getLengths() {
173         return Collections.emptyList();
174     }
175
176     @Override
177     public void setLengths(List<LengthConstraint> lengths) {
178         throw new IllegalStateException("Can not set lengths to "
179                 + UnionTypeBuilder.class.getSimpleName());
180     }
181
182     @Override
183     public List<PatternConstraint> getPatterns() {
184         return Collections.emptyList();
185     }
186
187     @Override
188     public void setPatterns(List<PatternConstraint> patterns) {
189         throw new IllegalStateException("Can not set patterns to "
190                 + UnionTypeBuilder.class.getSimpleName());
191     }
192
193     @Override
194     public Integer getFractionDigits() {
195         return null;
196     }
197
198     @Override
199     public void setFractionDigits(Integer fractionDigits) {
200         throw new IllegalStateException("Can not set fraction digits to "
201                 + UnionTypeBuilder.class.getSimpleName());
202     }
203
204     @Override
205     public List<UnknownSchemaNodeBuilder> getUnknownNodes() {
206         return Collections.emptyList();
207     }
208
209     @Override
210     public Object getDefaultValue() {
211         return null;
212     }
213
214     @Override
215     public void setDefaultValue(Object defaultValue) {
216         throw new IllegalStateException("Can not set default value to "
217                 + UnionTypeBuilder.class.getSimpleName());
218     }
219
220     @Override
221     public String getUnits() {
222         return null;
223     }
224
225     @Override
226     public void setUnits(String units) {
227         throw new IllegalStateException("Can not set units to "
228                 + UnionTypeBuilder.class.getSimpleName());
229     }
230
231     public List<String> getActualPath() {
232         return actualPath;
233     }
234
235     public URI getNamespace() {
236         return namespace;
237     }
238
239     public Date getRevision() {
240         return revision;
241     }
242
243     @Override
244     public String toString() {
245         final StringBuilder result = new StringBuilder(
246                 UnionTypeBuilder.class.getSimpleName() + "[");
247         result.append(", types=" + types);
248         result.append(", typedefs=" + typedefs);
249         result.append("]");
250         return result.toString();
251     }
252
253 }