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.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.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
33 TypeDefinitionBuilder {
34 private final static String NAME = "union";
36 private final int line;
37 private final List<TypeDefinition<?>> types;
38 private final List<TypeDefinitionBuilder> typedefs;
39 private UnionType instance;
40 private boolean isBuilt;
42 private SchemaPath path;
44 public UnionTypeBuilder(final int line) {
46 types = new ArrayList<TypeDefinition<?>>();
47 typedefs = new ArrayList<TypeDefinitionBuilder>();
51 public int getLine() {
55 public List<TypeDefinition<?>> getTypes() {
60 public TypeDefinition<?> getType() {
64 public List<TypeDefinitionBuilder> getTypedefs() {
65 return Collections.unmodifiableList(typedefs);
69 public TypeDefinitionBuilder getTypedef() {
74 public void setType(final TypeDefinition<?> type) {
79 public void setTypedef(final TypeDefinitionBuilder tdb) {
84 public UnionType build() {
86 instance = new UnionType(path, types);
87 for (TypeDefinitionBuilder tdb : typedefs) {
88 types.add(tdb.build());
96 public void setPath(final SchemaPath schemaPath) {
97 this.path = schemaPath;
101 public void setDescription(final String description) {
102 throw new YangParseException(line, "Can not set description to " + NAME);
106 public void setReference(final String reference) {
107 throw new YangParseException(line, "Can not set reference to " + NAME);
111 public void setStatus(final Status status) {
112 throw new YangParseException(line, "Can not set status to " + NAME);
116 public void addUnknownSchemaNode(final UnknownSchemaNodeBuilder unknownNode) {
117 throw new YangParseException(line, "Can not add unknown node to "
122 public QName getQName() {
127 public SchemaPath getPath() {
132 public String getDescription() {
137 public String getReference() {
142 public Status getStatus() {
147 public List<RangeConstraint> getRanges() {
148 return Collections.emptyList();
152 public void setRanges(List<RangeConstraint> ranges) {
153 throw new YangParseException(line, "Can not set ranges to " + NAME);
157 public List<LengthConstraint> getLengths() {
158 return Collections.emptyList();
162 public void setLengths(List<LengthConstraint> lengths) {
163 throw new YangParseException(line, "Can not set lengths to " + NAME);
167 public List<PatternConstraint> getPatterns() {
168 return Collections.emptyList();
172 public void setPatterns(List<PatternConstraint> patterns) {
173 throw new YangParseException(line, "Can not set patterns to " + NAME);
177 public Integer getFractionDigits() {
182 public void setFractionDigits(Integer fractionDigits) {
183 throw new YangParseException(line, "Can not set fraction digits to "
188 public List<UnknownSchemaNodeBuilder> getUnknownNodes() {
189 return Collections.emptyList();
193 public Object getDefaultValue() {
198 public void setDefaultValue(Object defaultValue) {
199 throw new YangParseException(line, "Can not set default value to "
204 public String getUnits() {
209 public void setUnits(String units) {
210 throw new YangParseException(line, "Can not set units to " + NAME);
214 public String toString() {
215 final StringBuilder result = new StringBuilder(
216 UnionTypeBuilder.class.getSimpleName() + "[");
217 result.append(", types=" + types);
218 result.append(", typedefs=" + typedefs);
220 return result.toString();