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.Builder;
26 import org.opendaylight.controller.yang.parser.builder.api.TypeDefinitionBuilder;
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, Builder {
36 private final static String CLASS_NAME = UnionTypeBuilder.class
39 private final int line;
40 private final List<TypeDefinition<?>> types;
41 private final List<TypeDefinitionBuilder> typedefs;
42 private final UnionType instance;
43 private boolean built;
45 private final List<String> actualPath;
46 private final URI namespace;
47 private final Date revision;
49 public UnionTypeBuilder(final List<String> actualPath, final URI namespace,
50 final Date revision, final int line) {
52 types = new ArrayList<TypeDefinition<?>>();
53 typedefs = new ArrayList<TypeDefinitionBuilder>();
54 instance = new UnionType(actualPath, namespace, revision, types);
56 this.actualPath = actualPath;
57 this.namespace = namespace;
58 this.revision = revision;
62 public int getLine() {
66 public List<TypeDefinition<?>> getTypes() {
71 public TypeDefinition<?> getType() {
75 public List<TypeDefinitionBuilder> getTypedefs() {
76 return Collections.unmodifiableList(typedefs);
80 public TypeDefinitionBuilder getTypedef() {
85 public void setType(final TypeDefinition<?> type) {
90 public void setType(final TypeDefinitionBuilder tdb) {
95 public UnionType build() {
99 for (TypeDefinitionBuilder tdb : typedefs) {
100 types.add(tdb.build());
108 public void setPath(final SchemaPath schemaPath) {
109 throw new IllegalStateException("Can not set path to " + CLASS_NAME);
113 public void setDescription(final String description) {
114 throw new IllegalStateException("Can not set description to "
119 public void setReference(final String reference) {
120 throw new IllegalStateException("Can not set reference to "
125 public void setStatus(final Status status) {
126 throw new IllegalStateException("Can not set status to " + CLASS_NAME);
130 public void addUnknownSchemaNode(final UnknownSchemaNodeBuilder unknownNode) {
131 throw new IllegalStateException("Can not add unknown node to "
136 public QName getQName() {
141 public SchemaPath getPath() {
146 public String getDescription() {
151 public String getReference() {
156 public Status getStatus() {
161 public List<RangeConstraint> getRanges() {
162 return Collections.emptyList();
166 public void setRanges(List<RangeConstraint> ranges) {
167 throw new IllegalStateException("Can not set ranges to "
168 + UnionTypeBuilder.class.getSimpleName());
172 public List<LengthConstraint> getLengths() {
173 return Collections.emptyList();
177 public void setLengths(List<LengthConstraint> lengths) {
178 throw new IllegalStateException("Can not set lengths to "
179 + UnionTypeBuilder.class.getSimpleName());
183 public List<PatternConstraint> getPatterns() {
184 return Collections.emptyList();
188 public void setPatterns(List<PatternConstraint> patterns) {
189 throw new IllegalStateException("Can not set patterns to "
190 + UnionTypeBuilder.class.getSimpleName());
194 public Integer getFractionDigits() {
199 public void setFractionDigits(Integer fractionDigits) {
200 throw new IllegalStateException("Can not set fraction digits to "
201 + UnionTypeBuilder.class.getSimpleName());
205 public List<UnknownSchemaNodeBuilder> getUnknownNodes() {
206 return Collections.emptyList();
210 public Object getDefaultValue() {
215 public void setDefaultValue(Object defaultValue) {
216 throw new IllegalStateException("Can not set default value to "
217 + UnionTypeBuilder.class.getSimpleName());
221 public String getUnits() {
226 public void setUnits(String units) {
227 throw new IllegalStateException("Can not set units to "
228 + UnionTypeBuilder.class.getSimpleName());
231 public List<String> getActualPath() {
235 public URI getNamespace() {
239 public Date getRevision() {
244 public String toString() {
245 final StringBuilder result = new StringBuilder(
246 UnionTypeBuilder.class.getSimpleName() + "[");
247 result.append(", types=" + types);
248 result.append(", typedefs=" + typedefs);
250 return result.toString();