2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.parser.builder.impl;
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;
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
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);
31 private final List<TypeDefinition<?>> types;
32 private final List<TypeDefinitionBuilder> typedefs;
33 private UnionType instance;
34 private boolean isBuilt;
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>();
42 public List<TypeDefinition<?>> getTypes() {
47 public void setQName(QName qname) {
48 throw new UnsupportedOperationException("Can not set qname to union type");
52 public TypeDefinition<?> getType() {
56 public List<TypeDefinitionBuilder> getTypedefs() {
61 public TypeDefinitionBuilder getTypedef() {
66 public void setType(final TypeDefinition<?> type) {
71 public void setTypedef(final TypeDefinitionBuilder tdb) {
76 public UnionType build() {
78 instance = new UnionType(types);
79 for (TypeDefinitionBuilder tdb : typedefs) {
80 types.add(tdb.build());
88 public void setDescription(final String description) {
89 throw new YangParseException(moduleName, line, "Can not set description to " + NAME);
93 public void setReference(final String reference) {
94 throw new YangParseException(moduleName, line, "Can not set reference to " + NAME);
98 public void setStatus(final Status status) {
99 throw new YangParseException(moduleName, line, "Can not set status to " + NAME);
103 public boolean isAddedByUses() {
108 public void setAddedByUses(final boolean addedByUses) {
109 throw new YangParseException(moduleName, line, "Union type can not be added by uses.");
113 public void addUnknownNodeBuilder(final UnknownSchemaNodeBuilder unknownNode) {
118 public SchemaPath getPath() {
119 return BaseTypes.schemaPath(QNAME);
123 public String getDescription() {
128 public String getReference() {
133 public Status getStatus() {
138 public List<RangeConstraint> getRanges() {
139 return Collections.emptyList();
143 public void setRanges(List<RangeConstraint> ranges) {
144 throw new YangParseException(moduleName, line, "Can not set ranges to " + NAME);
148 public List<LengthConstraint> getLengths() {
149 return Collections.emptyList();
153 public void setLengths(List<LengthConstraint> lengths) {
154 throw new YangParseException(moduleName, line, "Can not set lengths to " + NAME);
158 public List<PatternConstraint> getPatterns() {
159 return Collections.emptyList();
163 public void setPatterns(List<PatternConstraint> patterns) {
164 throw new YangParseException(moduleName, line, "Can not set patterns to " + NAME);
168 public Integer getFractionDigits() {
173 public void setFractionDigits(Integer fractionDigits) {
174 throw new YangParseException(moduleName, line, "Can not set fraction digits to " + NAME);
178 public List<UnknownSchemaNodeBuilder> getUnknownNodes() {
179 return Collections.emptyList();
183 public Object getDefaultValue() {
188 public void setDefaultValue(Object defaultValue) {
189 throw new YangParseException(moduleName, line, "Can not set default value to " + NAME);
193 public String getUnits() {
198 public void setUnits(String units) {
199 throw new YangParseException(moduleName, line, "Can not set units to " + NAME);
203 public String toString() {
204 final StringBuilder result = new StringBuilder(UnionTypeBuilder.class.getSimpleName());
206 result.append("types=");
207 result.append(types);
208 result.append(", typedefs=");
209 result.append(typedefs);
211 return result.toString();