2 * Copyright (c) 2015 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.stmt.rfc6020.effective.type;
10 import com.google.common.base.Optional;
11 import com.google.common.collect.ImmutableList;
12 import java.util.ArrayList;
13 import java.util.Collections;
14 import java.util.List;
15 import org.opendaylight.yangtools.yang.common.QName;
16 import org.opendaylight.yangtools.yang.common.YangConstants;
17 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
18 import org.opendaylight.yangtools.yang.model.api.Status;
19 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
20 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
21 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
22 import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement;
23 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
24 import org.opendaylight.yangtools.yang.model.util.EnumerationType;
25 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
26 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.Utils;
27 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.EffectiveStatementBase;
29 public class EnumSpecificationEffectiveStatementImpl extends
30 EffectiveStatementBase<String, TypeStatement.EnumSpecification> implements EnumTypeDefinition, TypeDefinitionEffectiveBuilder {
32 private static final QName QNAME = QName.create(YangConstants.RFC6020_YANG_MODULE, "enumeration");
34 private static final String DESCRIPTION = "The enumeration built-in type represents values from a set of assigned names.";
35 private static final String REFERENCE = "https://tools.ietf.org/html/rfc6020#section-9.6";
36 private static final String UNITS = "";
38 private final SchemaPath path;
39 private final EnumPair defaultEnum;
40 private final List<EnumPair> enums;
41 private EnumerationType enumerationTypeInstance = null;
43 public EnumSpecificationEffectiveStatementImpl(final StmtContext<String, TypeStatement.EnumSpecification, EffectiveStatement<String, TypeStatement.EnumSpecification>> ctx) {
46 List<EnumPair> enumsInit = new ArrayList<>();
48 path = Utils.getSchemaPath(ctx.getParentContext()).createChild(QNAME);
50 for (final EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements()) {
51 if (effectiveStatement instanceof EnumPair) {
52 enumsInit.add((EnumPair) effectiveStatement);
56 // FIXME: get from parentContext
58 enums = ImmutableList.copyOf(enumsInit);
62 public List<EnumPair> getValues() {
67 public EnumTypeDefinition getBaseType() {
72 public String getUnits() {
77 public Object getDefaultValue() {
82 public QName getQName() {
87 public SchemaPath getPath() {
92 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
93 return Collections.emptyList();
97 public String getDescription() {
102 public String getReference() {
107 public Status getStatus() {
108 return Status.CURRENT;
112 public int hashCode() {
113 final int prime = 31;
115 result = prime * result + ((defaultEnum == null) ? 0 : defaultEnum.hashCode());
116 result = prime * result + ((enums == null) ? 0 : enums.hashCode());
117 result = prime * result + QNAME.hashCode();
118 result = prime * result + ((path == null) ? 0 : path.hashCode());
123 public boolean equals(final Object obj) {
130 if (getClass() != obj.getClass()) {
133 EnumSpecificationEffectiveStatementImpl other = (EnumSpecificationEffectiveStatementImpl) obj;
134 if (defaultEnum == null) {
135 if (other.defaultEnum != null) {
138 } else if (!defaultEnum.equals(other.defaultEnum)) {
142 if (other.enums != null) {
145 } else if (!enums.equals(other.enums)) {
149 if (other.path != null) {
152 } else if (!path.equals(other.path)) {
159 public String toString() {
160 StringBuilder builder = new StringBuilder();
161 builder.append(EnumSpecificationEffectiveStatementImpl.class.getSimpleName());
162 builder.append(" [name=");
163 builder.append(QNAME);
164 builder.append(", path=");
165 builder.append(path);
166 builder.append(", description=");
167 builder.append(DESCRIPTION);
168 builder.append(", reference=");
169 builder.append(REFERENCE);
170 builder.append(", defaultEnum=");
171 builder.append(defaultEnum);
172 builder.append(", enums=");
173 builder.append(enums);
174 builder.append(", units=");
175 builder.append(UNITS);
177 return builder.toString();
181 public TypeDefinition<?> buildType() {
183 if(enumerationTypeInstance !=null) {
184 return enumerationTypeInstance;
187 // FIXME: set defaultValue as parameter
188 enumerationTypeInstance = EnumerationType.create(path, enums, Optional.<EnumPair>absent());
190 return enumerationTypeInstance;