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