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.collect.ImmutableList;
11 import java.util.ArrayList;
12 import java.util.Collections;
13 import java.util.List;
14 import org.opendaylight.yangtools.yang.common.QName;
15 import org.opendaylight.yangtools.yang.common.YangConstants;
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.UnknownSchemaNode;
20 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
21 import org.opendaylight.yangtools.yang.model.api.stmt.TypeEffectiveStatement;
22 import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement.UnionSpecification;
23 import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition;
24 import org.opendaylight.yangtools.yang.model.util.UnionType;
25 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
26 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.TypeUtils;
27 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.DeclaredEffectiveStatementBase;
29 public class UnionSpecificationEffectiveStatementImpl extends
30 DeclaredEffectiveStatementBase<String, UnionSpecification> implements UnionTypeDefinition,
31 TypeEffectiveStatement<UnionSpecification> {
33 private static final QName QNAME = QName.create(YangConstants.RFC6020_YANG_MODULE, "union");
34 private static final SchemaPath PATH = SchemaPath.create(true, QNAME);
35 private static final String DESCRIPTION = "The union built-in type represents a value that corresponds to one of its member types.";
36 private static final String REFERENCE = "https://tools.ietf.org/html/rfc6020#section-9.12";
38 private final List<TypeDefinition<?>> types;
39 private UnionType unionTypeInstance = null;
41 public UnionSpecificationEffectiveStatementImpl(
42 final StmtContext<String, UnionSpecification, EffectiveStatement<String, UnionSpecification>> ctx) {
45 List<TypeDefinition<?>> typesInit = new ArrayList<>();
47 for (final EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements()) {
48 if (effectiveStatement instanceof TypeDefinition) {
49 typesInit.add(TypeUtils.getTypeFromEffectiveStatement(effectiveStatement));
53 TypeUtils.sortTypes(typesInit);
55 types = ImmutableList.copyOf(typesInit);
59 public List<TypeDefinition<?>> getTypes() {
64 public UnionTypeDefinition getBaseType() {
69 public String getUnits() {
74 public Object getDefaultValue() {
79 public QName getQName() {
84 public SchemaPath getPath() {
89 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
90 return Collections.emptyList();
94 public String getDescription() {
99 public String getReference() {
104 public Status getStatus() {
105 return Status.CURRENT;
109 public int hashCode() {
110 final int prime = 31;
112 result = prime * result + types.hashCode();
117 public boolean equals(final Object obj) {
124 if (getClass() != obj.getClass()) {
127 UnionSpecificationEffectiveStatementImpl other = (UnionSpecificationEffectiveStatementImpl) obj;
128 return types.equals(other.types);
132 public String toString() {
133 StringBuilder builder = new StringBuilder();
134 builder.append("type ");
135 builder.append(QNAME);
136 builder.append(" (types=[");
137 for (TypeDefinition<?> td : types) {
138 builder.append(", ").append(td.getQName().getLocalName());
141 return builder.toString();
145 public TypeDefinition<?> getTypeDefinition() {
147 if (unionTypeInstance != null) {
148 return unionTypeInstance;
151 unionTypeInstance = UnionType.create(types);
153 return unionTypeInstance;