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.yangtools.yang.parser.builder.impl;
10 import com.google.common.base.Optional;
11 import com.google.common.base.Preconditions;
12 import java.util.Collections;
13 import java.util.List;
14 import java.util.Objects;
15 import org.opendaylight.yangtools.yang.common.QName;
16 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
17 import org.opendaylight.yangtools.yang.model.api.Status;
18 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
19 import org.opendaylight.yangtools.yang.model.api.type.LengthConstraint;
20 import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint;
21 import org.opendaylight.yangtools.yang.model.api.type.RangeConstraint;
22 import org.opendaylight.yangtools.yang.model.util.ExtendedType;
23 import org.opendaylight.yangtools.yang.parser.builder.api.TypeDefinitionBuilder;
24 import org.opendaylight.yangtools.yang.parser.builder.api.UnknownSchemaNodeBuilder;
25 import org.opendaylight.yangtools.yang.parser.builder.util.AbstractTypeAwareBuilder;
26 import org.opendaylight.yangtools.yang.parser.builder.util.Comparators;
27 import org.opendaylight.yangtools.yang.parser.util.YangParseException;
30 * @deprecated Pre-Beryllium implementation, scheduled for removal.
33 public final class TypeDefinitionBuilderImpl extends AbstractTypeAwareBuilder implements TypeDefinitionBuilder {
34 private SchemaPath schemaPath;
35 private List<RangeConstraint> ranges = Collections.emptyList();
36 private List<LengthConstraint> lengths = Collections.emptyList();
37 private List<PatternConstraint> patterns = Collections.emptyList();
38 private Integer fractionDigits = null;
40 private String description;
41 private String reference;
42 private Status status = Status.CURRENT;
44 private Object defaultValue;
45 private boolean addedByUses;
47 public TypeDefinitionBuilderImpl(final String moduleName, final int line, final QName qname, final SchemaPath path) {
48 super(moduleName, line, qname);
49 this.schemaPath = Preconditions.checkNotNull(path, "Schema Path must not be null");
52 public TypeDefinitionBuilderImpl(final String moduleName, final int line, final QName qname, final SchemaPath path, final ExtendedType base) {
53 super(moduleName, line, base.getQName());
54 this.schemaPath = Preconditions.checkNotNull(path, "Schema Path must not be null");
56 this.type = base.getBaseType();
57 this.description = base.getDescription();
58 this.reference = base.getReference();
59 this.status = base.getStatus();
60 this.units = base.getUnits();
61 this.defaultValue = base.getDefaultValue();
63 this.addedByUses = base.isAddedByUses();
64 this.ranges = base.getRangeConstraints();
65 this.lengths = base.getLengthConstraints();
66 this.patterns = base.getPatternConstraints();
67 this.fractionDigits = base.getFractionDigits();
68 this.unknownNodes.addAll(base.getUnknownSchemaNodes());
72 public TypeDefinition<? extends TypeDefinition<?>> build() {
73 TypeDefinition<?> result;
74 ExtendedType.Builder typeBuilder;
76 if (typedef == null) {
77 throw new YangParseException("Unresolved type: '" + qname.getLocalName() + "'.");
79 type = typedef.build();
83 typeBuilder = ExtendedType.builder(qname, type, Optional.fromNullable(description),
84 Optional.fromNullable(reference), schemaPath);
85 typeBuilder.status(status);
86 typeBuilder.units(units);
87 typeBuilder.defaultValue(defaultValue);
88 typeBuilder.addedByUses(addedByUses);
89 typeBuilder.ranges(ranges);
90 typeBuilder.lengths(lengths);
91 typeBuilder.patterns(patterns);
92 typeBuilder.fractionDigits(fractionDigits);
95 for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
96 unknownNodes.add(b.build());
98 Collections.sort(unknownNodes, Comparators.SCHEMA_NODE_COMP);
99 typeBuilder.unknownSchemaNodes(unknownNodes);
100 result = typeBuilder.build();
105 public void setQName(final QName qname) {
110 public SchemaPath getPath() {
115 public void setPath(final SchemaPath path) {
116 this.schemaPath = path;
120 public String getDescription() {
125 public void setDescription(final String description) {
126 this.description = description;
130 public String getReference() {
135 public void setReference(final String reference) {
136 this.reference = reference;
140 public Status getStatus() {
145 public void setStatus(final Status status) {
146 if (status != null) {
147 this.status = status;
152 public boolean isAddedByUses() {
157 public void setAddedByUses(final boolean addedByUses) {
158 this.addedByUses = addedByUses;
162 public String getUnits() {
167 public void setUnits(final String units) {
172 public Object getDefaultValue() {
177 public void setDefaultValue(final Object defaultValue) {
178 this.defaultValue = defaultValue;
182 public List<RangeConstraint> getRanges() {
187 public void setRanges(final List<RangeConstraint> ranges) {
188 if (ranges != null) {
189 this.ranges = ranges;
194 public List<LengthConstraint> getLengths() {
199 public void setLengths(final List<LengthConstraint> lengths) {
200 if (lengths != null) {
201 this.lengths = lengths;
206 public List<PatternConstraint> getPatterns() {
211 public void setPatterns(final List<PatternConstraint> patterns) {
212 if (patterns != null) {
213 this.patterns = patterns;
218 public Integer getFractionDigits() {
219 return fractionDigits;
223 public void setFractionDigits(final Integer fractionDigits) {
224 this.fractionDigits = fractionDigits;
228 public String toString() {
229 return "typedef " + qname.getLocalName();
233 public int hashCode() {
234 final int prime = 31;
235 int result = super.hashCode();
236 result = prime * result + Objects.hashCode(schemaPath);
242 public boolean equals(final Object obj) {
249 if (getClass() != obj.getClass()) {
252 if (!super.equals(obj)) {
255 TypeDefinitionBuilderImpl other = (TypeDefinitionBuilderImpl) obj;
257 if (schemaPath == null) {
258 if (other.schemaPath != null) {
261 } else if (!schemaPath.equals(other.schemaPath)) {