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 int line) {
44 types = new ArrayList<TypeDefinition<?>>();
45 typedefs = new ArrayList<TypeDefinitionBuilder>();
49 public List<TypeDefinition<?>> getTypes() {
54 public TypeDefinition<?> getType() {
58 public List<TypeDefinitionBuilder> getTypedefs() {
59 return Collections.unmodifiableList(typedefs);
63 public TypeDefinitionBuilder getTypedef() {
68 public void setType(final TypeDefinition<?> type) {
73 public void setTypedef(final TypeDefinitionBuilder tdb) {
78 public UnionType build() {
80 instance = new UnionType(path, types);
81 for (TypeDefinitionBuilder tdb : typedefs) {
82 types.add(tdb.build());
90 public void setPath(final SchemaPath schemaPath) {
91 this.path = schemaPath;
95 public void setDescription(final String description) {
96 throw new YangParseException(line, "Can not set description to " + NAME);
100 public void setReference(final String reference) {
101 throw new YangParseException(line, "Can not set reference to " + NAME);
105 public void setStatus(final Status status) {
106 throw new YangParseException(line, "Can not set status to " + NAME);
110 public boolean isAddedByUses() {
115 public void setAddedByUses(final boolean addedByUses) {
116 throw new YangParseException(line, "Union type can not be added by uses.");
120 public List<UnknownSchemaNode> getUnknownNodes() {
121 return Collections.emptyList();
125 public void addUnknownSchemaNode(final UnknownSchemaNodeBuilder unknownNode) {
130 public SchemaPath getPath() {
135 public String getDescription() {
140 public String getReference() {
145 public Status getStatus() {
150 public List<RangeConstraint> getRanges() {
151 return Collections.emptyList();
155 public void setRanges(List<RangeConstraint> ranges) {
156 throw new YangParseException(line, "Can not set ranges to " + NAME);
160 public List<LengthConstraint> getLengths() {
161 return Collections.emptyList();
165 public void setLengths(List<LengthConstraint> lengths) {
166 throw new YangParseException(line, "Can not set lengths to " + NAME);
170 public List<PatternConstraint> getPatterns() {
171 return Collections.emptyList();
175 public void setPatterns(List<PatternConstraint> patterns) {
176 throw new YangParseException(line, "Can not set patterns to " + NAME);
180 public Integer getFractionDigits() {
185 public void setFractionDigits(Integer fractionDigits) {
186 throw new YangParseException(line, "Can not set fraction digits to " + NAME);
190 public List<UnknownSchemaNodeBuilder> getUnknownNodeBuilders() {
191 return Collections.emptyList();
195 public Object getDefaultValue() {
200 public void setDefaultValue(Object defaultValue) {
201 throw new YangParseException(line, "Can not set default value to " + NAME);
205 public String getUnits() {
210 public void setUnits(String units) {
211 throw new YangParseException(line, "Can not set units to " + NAME);
215 public String toString() {
216 final StringBuilder result = new StringBuilder(UnionTypeBuilder.class.getSimpleName() + "[");
217 result.append(", types=" + types);
218 result.append(", typedefs=" + typedefs);
220 return result.toString();