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;
11 import java.util.ArrayList;
12 import java.util.Collections;
13 import java.util.Date;
14 import java.util.List;
16 import org.opendaylight.controller.yang.common.QName;
17 import org.opendaylight.controller.yang.model.api.SchemaPath;
18 import org.opendaylight.controller.yang.model.api.Status;
19 import org.opendaylight.controller.yang.model.api.TypeDefinition;
20 import org.opendaylight.controller.yang.model.api.type.LengthConstraint;
21 import org.opendaylight.controller.yang.model.api.type.PatternConstraint;
22 import org.opendaylight.controller.yang.model.api.type.RangeConstraint;
23 import org.opendaylight.controller.yang.model.util.UnionType;
24 import org.opendaylight.controller.yang.parser.builder.api.AbstractTypeAwareBuilder;
25 import org.opendaylight.controller.yang.parser.builder.api.TypeDefinitionBuilder;
26 import org.opendaylight.controller.yang.parser.util.YangParseException;
29 * Builder for YANG union type. User can add type to this union as
30 * TypeDefinition object (resolved type) or in form of TypeDefinitionBuilder.
31 * When build is called, types in builder form will be built and add to resolved
34 public class UnionTypeBuilder extends AbstractTypeAwareBuilder implements
35 TypeDefinitionBuilder {
36 private final static String NAME = "union";
38 private final int line;
39 private final List<TypeDefinition<?>> types;
40 private final List<TypeDefinitionBuilder> typedefs;
41 private final UnionType instance;
42 private boolean built;
44 private final List<String> actualPath;
45 private final URI namespace;
46 private final Date revision;
48 public UnionTypeBuilder(final List<String> actualPath, final URI namespace,
49 final Date revision, final int line) {
51 types = new ArrayList<TypeDefinition<?>>();
52 typedefs = new ArrayList<TypeDefinitionBuilder>();
53 instance = new UnionType(actualPath, namespace, revision, types);
55 this.actualPath = actualPath;
56 this.namespace = namespace;
57 this.revision = revision;
61 public int getLine() {
65 public List<TypeDefinition<?>> getTypes() {
70 public TypeDefinition<?> getType() {
74 public List<TypeDefinitionBuilder> getTypedefs() {
75 return Collections.unmodifiableList(typedefs);
79 public TypeDefinitionBuilder getTypedef() {
84 public void setType(final TypeDefinition<?> type) {
89 public void setType(final TypeDefinitionBuilder tdb) {
94 public UnionType build() {
98 for (TypeDefinitionBuilder tdb : typedefs) {
99 types.add(tdb.build());
107 public void setPath(final SchemaPath schemaPath) {
108 throw new YangParseException(line, "Can not set path to " + NAME);
112 public void setDescription(final String description) {
113 throw new YangParseException(line, "Can not set description to " + NAME);
117 public void setReference(final String reference) {
118 throw new YangParseException(line, "Can not set reference to " + NAME);
122 public void setStatus(final Status status) {
123 throw new YangParseException(line, "Can not set status to " + NAME);
127 public void addUnknownSchemaNode(final UnknownSchemaNodeBuilder unknownNode) {
128 throw new YangParseException(line, "Can not add unknown node to "
133 public QName getQName() {
138 public SchemaPath getPath() {
143 public String getDescription() {
148 public String getReference() {
153 public Status getStatus() {
158 public List<RangeConstraint> getRanges() {
159 return Collections.emptyList();
163 public void setRanges(List<RangeConstraint> ranges) {
164 throw new YangParseException(line, "Can not set ranges to " + NAME);
168 public List<LengthConstraint> getLengths() {
169 return Collections.emptyList();
173 public void setLengths(List<LengthConstraint> lengths) {
174 throw new YangParseException(line, "Can not set lengths to " + NAME);
178 public List<PatternConstraint> getPatterns() {
179 return Collections.emptyList();
183 public void setPatterns(List<PatternConstraint> patterns) {
184 throw new YangParseException(line, "Can not set patterns to " + NAME);
188 public Integer getFractionDigits() {
193 public void setFractionDigits(Integer fractionDigits) {
194 throw new YangParseException(line, "Can not set fraction digits to "
199 public List<UnknownSchemaNodeBuilder> getUnknownNodes() {
200 return Collections.emptyList();
204 public Object getDefaultValue() {
209 public void setDefaultValue(Object defaultValue) {
210 throw new YangParseException(line, "Can not set default value to "
215 public String getUnits() {
220 public void setUnits(String units) {
221 throw new YangParseException(line, "Can not set units to " + NAME);
224 public List<String> getActualPath() {
228 public URI getNamespace() {
232 public Date getRevision() {
237 public String toString() {
238 final StringBuilder result = new StringBuilder(
239 UnionTypeBuilder.class.getSimpleName() + "[");
240 result.append(", types=" + types);
241 result.append(", typedefs=" + typedefs);
243 return result.toString();