fca2532227d5630bacaf0f024d36149ace62b128
[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 java.util.Objects;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.common.YangConstants;
18 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
19 import org.opendaylight.yangtools.yang.model.api.Status;
20 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
21 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
23 import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement;
24 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
25 import org.opendaylight.yangtools.yang.model.util.EnumerationType;
26 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
27 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.Utils;
28 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.EffectiveStatementBase;
29
30 public class EnumSpecificationEffectiveStatementImpl extends
31         EffectiveStatementBase<String, TypeStatement.EnumSpecification> implements EnumTypeDefinition, TypeDefinitionEffectiveBuilder {
32
33     private static final QName QNAME = QName.create(YangConstants.RFC6020_YANG_MODULE, "enumeration");
34
35     private static final String DESCRIPTION = "The enumeration built-in type represents values from a set of assigned names.";
36     private static final String REFERENCE = "https://tools.ietf.org/html/rfc6020#section-9.6";
37     private static final String UNITS = "";
38
39     private final SchemaPath path;
40     private final EnumPair defaultEnum;
41     private final List<EnumPair> enums;
42     private EnumerationType enumerationTypeInstance = null;
43
44     public EnumSpecificationEffectiveStatementImpl(final StmtContext<String, TypeStatement.EnumSpecification, EffectiveStatement<String, TypeStatement.EnumSpecification>> ctx) {
45         super(ctx);
46
47         List<EnumPair> enumsInit = new ArrayList<>();
48
49         path = Utils.getSchemaPath(ctx.getParentContext()).createChild(QNAME);
50
51         for (final EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements()) {
52             if (effectiveStatement instanceof EnumPair) {
53                 enumsInit.add((EnumPair) effectiveStatement);
54             }
55         }
56
57         // FIXME: get from parentContext
58         defaultEnum = null;
59         enums = ImmutableList.copyOf(enumsInit);
60     }
61
62     @Override
63     public List<EnumPair> getValues() {
64         return enums;
65     }
66
67     @Override
68     public EnumTypeDefinition getBaseType() {
69         return null;
70     }
71
72     @Override
73     public String getUnits() {
74         return UNITS;
75     }
76
77     @Override
78     public Object getDefaultValue() {
79         return defaultEnum;
80     }
81
82     @Override
83     public QName getQName() {
84         return QNAME;
85     }
86
87     @Override
88     public SchemaPath getPath() {
89         return path;
90     }
91
92     @Override
93     public List<UnknownSchemaNode> getUnknownSchemaNodes() {
94         return Collections.emptyList();
95     }
96
97     @Override
98     public String getDescription() {
99         return DESCRIPTION;
100     }
101
102     @Override
103     public String getReference() {
104         return REFERENCE;
105     }
106
107     @Override
108     public Status getStatus() {
109         return Status.CURRENT;
110     }
111
112     @Override
113     public int hashCode() {
114         final int prime = 31;
115         int result = 1;
116         result = prime * result + Objects.hashCode(defaultEnum);
117         result = prime * result + Objects.hashCode(enums);
118         result = prime * result + QNAME.hashCode();
119         result = prime * result + Objects.hashCode(path);
120         return result;
121     }
122
123     @Override
124     public boolean equals(final Object obj) {
125         if (this == obj) {
126             return true;
127         }
128         if (obj == null) {
129             return false;
130         }
131         if (getClass() != obj.getClass()) {
132             return false;
133         }
134         EnumSpecificationEffectiveStatementImpl other = (EnumSpecificationEffectiveStatementImpl) obj;
135         if (defaultEnum == null) {
136             if (other.defaultEnum != null) {
137                 return false;
138             }
139         } else if (!defaultEnum.equals(other.defaultEnum)) {
140             return false;
141         }
142         if (enums == null) {
143             if (other.enums != null) {
144                 return false;
145             }
146         } else if (!enums.equals(other.enums)) {
147             return false;
148         }
149         if (path == null) {
150             if (other.path != null) {
151                 return false;
152             }
153         } else if (!path.equals(other.path)) {
154             return false;
155         }
156         return true;
157     }
158
159     @Override
160     public String toString() {
161         StringBuilder builder = new StringBuilder();
162         builder.append(EnumSpecificationEffectiveStatementImpl.class.getSimpleName());
163         builder.append(" [name=");
164         builder.append(QNAME);
165         builder.append(", path=");
166         builder.append(path);
167         builder.append(", description=");
168         builder.append(DESCRIPTION);
169         builder.append(", reference=");
170         builder.append(REFERENCE);
171         builder.append(", defaultEnum=");
172         builder.append(defaultEnum);
173         builder.append(", enums=");
174         builder.append(enums);
175         builder.append(", units=");
176         builder.append(UNITS);
177         builder.append("]");
178         return builder.toString();
179     }
180
181     @Override
182     public TypeDefinition<?> buildType() {
183
184         if(enumerationTypeInstance !=null) {
185             return enumerationTypeInstance;
186         }
187
188         // FIXME: set defaultValue as parameter
189         enumerationTypeInstance = EnumerationType.create(path, enums, Optional.<EnumPair>absent());
190
191         return enumerationTypeInstance;
192     }
193 }