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