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