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.controller.yang.parser.builder.impl;
10 import java.util.ArrayList;
11 import java.util.Collections;
12 import java.util.List;
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;
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
32 public final class UnionTypeBuilder extends AbstractTypeAwareBuilder implements TypeDefinitionBuilder {
33 private final static String NAME = "union";
35 private final List<TypeDefinition<?>> types;
36 private final List<TypeDefinitionBuilder> typedefs;
37 private UnionType instance;
38 private boolean isBuilt;
40 private SchemaPath path;
42 public UnionTypeBuilder(final String moduleName, final int line) {
43 super(moduleName, line, null);
44 types = new ArrayList<TypeDefinition<?>>();
45 typedefs = new ArrayList<TypeDefinitionBuilder>();
48 public List<TypeDefinition<?>> getTypes() {
53 public TypeDefinition<?> getType() {
57 public List<TypeDefinitionBuilder> getTypedefs() {
58 return Collections.unmodifiableList(typedefs);
62 public TypeDefinitionBuilder getTypedef() {
67 public void setType(final TypeDefinition<?> type) {
72 public void setTypedef(final TypeDefinitionBuilder tdb) {
77 public UnionType build() {
79 instance = new UnionType(path, types);
80 for (TypeDefinitionBuilder tdb : typedefs) {
81 types.add(tdb.build());
89 public void setPath(final SchemaPath schemaPath) {
90 this.path = schemaPath;
94 public void setDescription(final String description) {
95 throw new YangParseException(moduleName, line, "Can not set description to " + NAME);
99 public void setReference(final String reference) {
100 throw new YangParseException(moduleName, line, "Can not set reference to " + NAME);
104 public void setStatus(final Status status) {
105 throw new YangParseException(moduleName, line, "Can not set status to " + NAME);
109 public boolean isAddedByUses() {
114 public void setAddedByUses(final boolean addedByUses) {
115 throw new YangParseException(moduleName, line, "Union type can not be added by uses.");
119 public List<UnknownSchemaNode> getUnknownNodes() {
120 return Collections.emptyList();
124 public void addUnknownNodeBuilder(final UnknownSchemaNodeBuilder unknownNode) {
129 public SchemaPath getPath() {
134 public String getDescription() {
139 public String getReference() {
144 public Status getStatus() {
149 public List<RangeConstraint> getRanges() {
150 return Collections.emptyList();
154 public void setRanges(List<RangeConstraint> ranges) {
155 throw new YangParseException(moduleName, line, "Can not set ranges to " + NAME);
159 public List<LengthConstraint> getLengths() {
160 return Collections.emptyList();
164 public void setLengths(List<LengthConstraint> lengths) {
165 throw new YangParseException(moduleName, line, "Can not set lengths to " + NAME);
169 public List<PatternConstraint> getPatterns() {
170 return Collections.emptyList();
174 public void setPatterns(List<PatternConstraint> patterns) {
175 throw new YangParseException(moduleName, line, "Can not set patterns to " + NAME);
179 public Integer getFractionDigits() {
184 public void setFractionDigits(Integer fractionDigits) {
185 throw new YangParseException(moduleName, line, "Can not set fraction digits to " + NAME);
189 public List<UnknownSchemaNodeBuilder> getUnknownNodeBuilders() {
190 return Collections.emptyList();
194 public Object getDefaultValue() {
199 public void setDefaultValue(Object defaultValue) {
200 throw new YangParseException(moduleName, line, "Can not set default value to " + NAME);
204 public String getUnits() {
209 public void setUnits(String units) {
210 throw new YangParseException(moduleName, line, "Can not set units to " + NAME);
214 public String toString() {
215 final StringBuilder result = new StringBuilder(UnionTypeBuilder.class.getSimpleName() + "[");
216 result.append(", types=" + types);
217 result.append(", typedefs=" + typedefs);
219 return result.toString();