Split off DeclaredEffectiveStatementBase
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / effective / type / UnionSpecificationEffectiveStatementImpl.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type;
9
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;
28
29 public class UnionSpecificationEffectiveStatementImpl extends
30         DeclaredEffectiveStatementBase<String, UnionSpecification> implements UnionTypeDefinition,
31         TypeEffectiveStatement<UnionSpecification> {
32
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";
37
38     private final List<TypeDefinition<?>> types;
39     private UnionType unionTypeInstance = null;
40
41     public UnionSpecificationEffectiveStatementImpl(
42             final StmtContext<String, UnionSpecification, EffectiveStatement<String, UnionSpecification>> ctx) {
43         super(ctx);
44
45         List<TypeDefinition<?>> typesInit = new ArrayList<>();
46
47         for (final EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements()) {
48             if (effectiveStatement instanceof TypeDefinition) {
49                 typesInit.add(TypeUtils.getTypeFromEffectiveStatement(effectiveStatement));
50             }
51         }
52
53         TypeUtils.sortTypes(typesInit);
54
55         types = ImmutableList.copyOf(typesInit);
56     }
57
58     @Override
59     public List<TypeDefinition<?>> getTypes() {
60         return types;
61     }
62
63     @Override
64     public UnionTypeDefinition getBaseType() {
65         return null;
66     }
67
68     @Override
69     public String getUnits() {
70         return null;
71     }
72
73     @Override
74     public Object getDefaultValue() {
75         return null;
76     }
77
78     @Override
79     public QName getQName() {
80         return QNAME;
81     }
82
83     @Override
84     public SchemaPath getPath() {
85         return PATH;
86     }
87
88     @Override
89     public List<UnknownSchemaNode> getUnknownSchemaNodes() {
90         return Collections.emptyList();
91     }
92
93     @Override
94     public String getDescription() {
95         return DESCRIPTION;
96     }
97
98     @Override
99     public String getReference() {
100         return REFERENCE;
101     }
102
103     @Override
104     public Status getStatus() {
105         return Status.CURRENT;
106     }
107
108     @Override
109     public int hashCode() {
110         final int prime = 31;
111         int result = 1;
112         result = prime * result + types.hashCode();
113         return result;
114     }
115
116     @Override
117     public boolean equals(final Object obj) {
118         if (this == obj) {
119             return true;
120         }
121         if (obj == null) {
122             return false;
123         }
124         if (getClass() != obj.getClass()) {
125             return false;
126         }
127         UnionSpecificationEffectiveStatementImpl other = (UnionSpecificationEffectiveStatementImpl) obj;
128         return types.equals(other.types);
129     }
130
131     @Override
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());
139         }
140         builder.append(']');
141         return builder.toString();
142     }
143
144     @Override
145     public TypeDefinition<?> getTypeDefinition() {
146
147         if (unionTypeInstance != null) {
148             return unionTypeInstance;
149         }
150
151         unionTypeInstance = UnionType.create(types);
152
153         return unionTypeInstance;
154     }
155 }