Merge "Convert various result into Status Signed-off-by: Jason Ye <yisye@cisco.com>"
[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.util.ArrayList;
11 import java.util.Collections;
12 import java.util.List;
13
14 import org.opendaylight.controller.yang.common.QName;
15 import org.opendaylight.controller.yang.model.api.SchemaPath;
16 import org.opendaylight.controller.yang.model.api.Status;
17 import org.opendaylight.controller.yang.model.api.TypeDefinition;
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.parser.builder.api.AbstractTypeAwareBuilder;
22 import org.opendaylight.controller.yang.model.parser.builder.api.Builder;
23 import org.opendaylight.controller.yang.model.parser.builder.api.TypeDefinitionBuilder;
24 import org.opendaylight.controller.yang.model.util.UnionType;
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 class UnionTypeBuilder extends AbstractTypeAwareBuilder implements
33         TypeDefinitionBuilder, Builder {
34     private final List<TypeDefinition<?>> types;
35     private final List<TypeDefinitionBuilder> typedefs;
36     private final UnionType instance;
37
38     public UnionTypeBuilder() {
39         types = new ArrayList<TypeDefinition<?>>();
40         typedefs = new ArrayList<TypeDefinitionBuilder>();
41         instance = new UnionType(types);
42     }
43
44     public List<TypeDefinition<?>> getTypes() {
45         return types;
46     }
47
48     @Override
49     public TypeDefinition<?> getType() {
50         return null;
51     }
52
53     public List<TypeDefinitionBuilder> getTypedefs() {
54         return Collections.unmodifiableList(typedefs);
55     }
56
57     @Override
58     public TypeDefinitionBuilder getTypedef() {
59         return null;
60     }
61
62     @Override
63     public void setType(final TypeDefinition<?> type) {
64         types.add(type);
65     }
66
67     @Override
68     public void setType(final TypeDefinitionBuilder tdb) {
69         typedefs.add(tdb);
70     }
71
72     @Override
73     public UnionType build() {
74         for (TypeDefinitionBuilder tdb : typedefs) {
75             types.add(tdb.build());
76         }
77         return instance;
78     }
79
80     @Override
81     public void setPath(final SchemaPath schemaPath) {
82         throw new IllegalStateException("Can not set path to "
83                 + UnionTypeBuilder.class.getSimpleName());
84     }
85
86     @Override
87     public void setDescription(final String description) {
88         throw new IllegalStateException("Can not set description to "
89                 + UnionTypeBuilder.class.getSimpleName());
90     }
91
92     @Override
93     public void setReference(final String reference) {
94         throw new IllegalStateException("Can not set reference to "
95                 + UnionTypeBuilder.class.getSimpleName());
96     }
97
98     @Override
99     public void setStatus(final Status status) {
100         throw new IllegalStateException("Can not set status to "
101                 + UnionTypeBuilder.class.getSimpleName());
102     }
103
104     @Override
105     public void addUnknownSchemaNode(final UnknownSchemaNodeBuilder unknownNode) {
106         throw new IllegalStateException("Can not add unknown node to "
107                 + UnionTypeBuilder.class.getSimpleName());
108     }
109
110     @Override
111     public QName getQName() {
112         return null;
113     }
114
115     @Override
116     public SchemaPath getPath() {
117         return null;
118     }
119
120     @Override
121     public String getDescription() {
122         return null;
123     }
124
125     @Override
126     public String getReference() {
127         return null;
128     }
129
130     @Override
131     public Status getStatus() {
132         return null;
133     }
134
135     @Override
136     public List<RangeConstraint> getRanges() {
137         return Collections.emptyList();
138     }
139
140     @Override
141     public void setRanges(List<RangeConstraint> ranges) {
142         throw new IllegalStateException("Can not set ranges to "
143                 + UnionTypeBuilder.class.getSimpleName());
144     }
145
146     @Override
147     public List<LengthConstraint> getLengths() {
148         return Collections.emptyList();
149     }
150
151     @Override
152     public void setLengths(List<LengthConstraint> lengths) {
153         throw new IllegalStateException("Can not set lengths to "
154                 + UnionTypeBuilder.class.getSimpleName());
155     }
156
157     @Override
158     public List<PatternConstraint> getPatterns() {
159         return Collections.emptyList();
160     }
161
162     @Override
163     public void setPatterns(List<PatternConstraint> patterns) {
164         throw new IllegalStateException("Can not set patterns to "
165                 + UnionTypeBuilder.class.getSimpleName());
166     }
167
168     @Override
169     public Integer getFractionDigits() {
170         return null;
171     }
172
173     @Override
174     public void setFractionDigits(Integer fractionDigits) {
175         throw new IllegalStateException("Can not set fraction digits to "
176                 + UnionTypeBuilder.class.getSimpleName());
177     }
178
179     @Override
180     public List<UnknownSchemaNodeBuilder> getUnknownNodes() {
181         return Collections.emptyList();
182     }
183
184     @Override
185     public Object getDefaultValue() {
186         return null;
187     }
188
189     @Override
190     public void setDefaultValue(Object defaultValue) {
191         throw new IllegalStateException("Can not set default value to "
192                 + UnionTypeBuilder.class.getSimpleName());
193     }
194
195     @Override
196     public String getUnits() {
197         return null;
198     }
199
200     @Override
201     public void setUnits(String units) {
202         throw new IllegalStateException("Can not set units to "
203                 + UnionTypeBuilder.class.getSimpleName());
204     }
205
206     @Override
207     public String toString() {
208         final StringBuilder result = new StringBuilder(
209                 UnionTypeBuilder.class.getSimpleName() + "[");
210         result.append(", types=" + types);
211         result.append(", typedefs=" + typedefs);
212         result.append("]");
213         return result.toString();
214     }
215
216 }