d7812e911a10092bcd4b53fc80229a9b7ffde1b7
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / effective / type / EnumSpecificationEffectiveStatementImpl.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.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;
28
29 public class EnumSpecificationEffectiveStatementImpl extends
30         EffectiveStatementBase<String, TypeStatement.EnumSpecification> implements EnumTypeDefinition, TypeDefinitionEffectiveBuilder {
31
32     private static final QName QNAME = QName.create(YangConstants.RFC6020_YANG_MODULE, "enumeration");
33
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 = "";
37
38     private final SchemaPath path;
39     private final EnumPair defaultEnum;
40     private final List<EnumPair> enums;
41     private EnumerationType enumerationTypeInstance = null;
42
43     public EnumSpecificationEffectiveStatementImpl(final StmtContext<String, TypeStatement.EnumSpecification, EffectiveStatement<String, TypeStatement.EnumSpecification>> ctx) {
44         super(ctx);
45
46         List<EnumPair> enumsInit = new ArrayList<>();
47
48         path = Utils.getSchemaPath(ctx.getParentContext()).createChild(QNAME);
49
50         for (final EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements()) {
51             if (effectiveStatement instanceof EnumPair) {
52                 enumsInit.add((EnumPair) effectiveStatement);
53             }
54         }
55
56         // FIXME: get from parentContext
57         defaultEnum = null;
58         enums = ImmutableList.copyOf(enumsInit);
59     }
60
61     @Override
62     public List<EnumPair> getValues() {
63         return enums;
64     }
65
66     @Override
67     public EnumTypeDefinition getBaseType() {
68         return null;
69     }
70
71     @Override
72     public String getUnits() {
73         return UNITS;
74     }
75
76     @Override
77     public Object getDefaultValue() {
78         return defaultEnum;
79     }
80
81     @Override
82     public QName getQName() {
83         return QNAME;
84     }
85
86     @Override
87     public SchemaPath getPath() {
88         return path;
89     }
90
91     @Override
92     public List<UnknownSchemaNode> getUnknownSchemaNodes() {
93         return Collections.emptyList();
94     }
95
96     @Override
97     public String getDescription() {
98         return DESCRIPTION;
99     }
100
101     @Override
102     public String getReference() {
103         return REFERENCE;
104     }
105
106     @Override
107     public Status getStatus() {
108         return Status.CURRENT;
109     }
110
111     @Override
112     public int hashCode() {
113         final int prime = 31;
114         int result = 1;
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());
119         return result;
120     }
121
122     @Override
123     public boolean equals(final Object obj) {
124         if (this == obj) {
125             return true;
126         }
127         if (obj == null) {
128             return false;
129         }
130         if (getClass() != obj.getClass()) {
131             return false;
132         }
133         EnumSpecificationEffectiveStatementImpl other = (EnumSpecificationEffectiveStatementImpl) obj;
134         if (defaultEnum == null) {
135             if (other.defaultEnum != null) {
136                 return false;
137             }
138         } else if (!defaultEnum.equals(other.defaultEnum)) {
139             return false;
140         }
141         if (enums == null) {
142             if (other.enums != null) {
143                 return false;
144             }
145         } else if (!enums.equals(other.enums)) {
146             return false;
147         }
148         if (path == null) {
149             if (other.path != null) {
150                 return false;
151             }
152         } else if (!path.equals(other.path)) {
153             return false;
154         }
155         return true;
156     }
157
158     @Override
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);
176         builder.append("]");
177         return builder.toString();
178     }
179
180     @Override
181     public TypeDefinition<?> buildType() {
182
183         if(enumerationTypeInstance !=null) {
184             return enumerationTypeInstance;
185         }
186
187         // FIXME: set defaultValue as parameter
188         enumerationTypeInstance = EnumerationType.create(path, enums, Optional.<EnumPair>absent());
189
190         return enumerationTypeInstance;
191     }
192 }