Merge "Package names for enclosed Types of TOs were changed to fully qualified. Fully...
[controller.git] / opendaylight / sal / yang-prototype / yang / 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.util.ArrayList;
11 import java.util.Collections;
12 import java.util.List;
13
14 import org.opendaylight.controller.yang.model.api.SchemaPath;
15 import org.opendaylight.controller.yang.model.api.Status;
16 import org.opendaylight.controller.yang.model.api.TypeDefinition;
17 import org.opendaylight.controller.yang.model.api.UnknownSchemaNode;
18 import org.opendaylight.controller.yang.model.api.type.LengthConstraint;
19 import org.opendaylight.controller.yang.model.api.type.PatternConstraint;
20 import org.opendaylight.controller.yang.model.api.type.RangeConstraint;
21 import org.opendaylight.controller.yang.model.util.UnionType;
22 import org.opendaylight.controller.yang.parser.builder.api.AbstractTypeAwareBuilder;
23 import org.opendaylight.controller.yang.parser.builder.api.TypeDefinitionBuilder;
24 import org.opendaylight.controller.yang.parser.util.YangParseException;
25
26 /**
27  * Builder for YANG union type. User can add type to this union as
28  * TypeDefinition object (resolved type) or in form of TypeDefinitionBuilder.
29  * When build is called, types in builder form will be built and add to resolved
30  * types.
31  */
32 public final class UnionTypeBuilder extends AbstractTypeAwareBuilder implements TypeDefinitionBuilder {
33     private final static String NAME = "union";
34
35     private final List<TypeDefinition<?>> types;
36     private final List<TypeDefinitionBuilder> typedefs;
37     private UnionType instance;
38     private boolean isBuilt;
39
40     private SchemaPath path;
41
42     public UnionTypeBuilder(final String moduleName, final int line) {
43         super(moduleName, line, null);
44         types = new ArrayList<TypeDefinition<?>>();
45         typedefs = new ArrayList<TypeDefinitionBuilder>();
46     }
47
48     public List<TypeDefinition<?>> getTypes() {
49         return types;
50     }
51
52     @Override
53     public TypeDefinition<?> getType() {
54         return null;
55     }
56
57     public List<TypeDefinitionBuilder> getTypedefs() {
58         return Collections.unmodifiableList(typedefs);
59     }
60
61     @Override
62     public TypeDefinitionBuilder getTypedef() {
63         return null;
64     }
65
66     @Override
67     public void setType(final TypeDefinition<?> type) {
68         types.add(type);
69     }
70
71     @Override
72     public void setTypedef(final TypeDefinitionBuilder tdb) {
73         typedefs.add(tdb);
74     }
75
76     @Override
77     public UnionType build() {
78         if (!isBuilt) {
79             instance = new UnionType(path, types);
80             for (TypeDefinitionBuilder tdb : typedefs) {
81                 types.add(tdb.build());
82             }
83             isBuilt = true;
84         }
85         return instance;
86     }
87
88     @Override
89     public void setPath(final SchemaPath schemaPath) {
90         this.path = schemaPath;
91     }
92
93     @Override
94     public void setDescription(final String description) {
95         throw new YangParseException(moduleName, line, "Can not set description to " + NAME);
96     }
97
98     @Override
99     public void setReference(final String reference) {
100         throw new YangParseException(moduleName, line, "Can not set reference to " + NAME);
101     }
102
103     @Override
104     public void setStatus(final Status status) {
105         throw new YangParseException(moduleName, line, "Can not set status to " + NAME);
106     }
107
108     @Override
109     public boolean isAddedByUses() {
110         return false;
111     }
112
113     @Override
114     public void setAddedByUses(final boolean addedByUses) {
115         throw new YangParseException(moduleName, line, "Union type can not be added by uses.");
116     }
117
118     @Override
119     public List<UnknownSchemaNode> getUnknownNodes() {
120         return Collections.emptyList();
121     }
122
123     @Override
124     public void addUnknownNodeBuilder(final UnknownSchemaNodeBuilder unknownNode) {
125         // not yet supported
126     }
127
128     @Override
129     public SchemaPath getPath() {
130         return path;
131     }
132
133     @Override
134     public String getDescription() {
135         return null;
136     }
137
138     @Override
139     public String getReference() {
140         return null;
141     }
142
143     @Override
144     public Status getStatus() {
145         return null;
146     }
147
148     @Override
149     public List<RangeConstraint> getRanges() {
150         return Collections.emptyList();
151     }
152
153     @Override
154     public void setRanges(List<RangeConstraint> ranges) {
155         throw new YangParseException(moduleName, line, "Can not set ranges to " + NAME);
156     }
157
158     @Override
159     public List<LengthConstraint> getLengths() {
160         return Collections.emptyList();
161     }
162
163     @Override
164     public void setLengths(List<LengthConstraint> lengths) {
165         throw new YangParseException(moduleName, line, "Can not set lengths to " + NAME);
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 YangParseException(moduleName, line, "Can not set patterns to " + NAME);
176     }
177
178     @Override
179     public Integer getFractionDigits() {
180         return null;
181     }
182
183     @Override
184     public void setFractionDigits(Integer fractionDigits) {
185         throw new YangParseException(moduleName, line, "Can not set fraction digits to " + NAME);
186     }
187
188     @Override
189     public List<UnknownSchemaNodeBuilder> getUnknownNodeBuilders() {
190         return Collections.emptyList();
191     }
192
193     @Override
194     public Object getDefaultValue() {
195         return null;
196     }
197
198     @Override
199     public void setDefaultValue(Object defaultValue) {
200         throw new YangParseException(moduleName, line, "Can not set default value to " + NAME);
201     }
202
203     @Override
204     public String getUnits() {
205         return null;
206     }
207
208     @Override
209     public void setUnits(String units) {
210         throw new YangParseException(moduleName, line, "Can not set units to " + NAME);
211     }
212
213     @Override
214     public String toString() {
215         final StringBuilder result = new StringBuilder(UnionTypeBuilder.class.getSimpleName() + "[");
216         result.append(", types=" + types);
217         result.append(", typedefs=" + typedefs);
218         result.append("]");
219         return result.toString();
220     }
221
222 }