Bug 2366 - Effective statments impl merge, retest & bugfix
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / effective / FeatureEffectiveStatementImpl.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;
9
10 import java.util.LinkedList;
11 import java.util.List;
12
13 import org.opendaylight.yangtools.yang.common.QName;
14 import org.opendaylight.yangtools.yang.model.api.FeatureDefinition;
15 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
16 import org.opendaylight.yangtools.yang.model.api.Status;
17 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
18 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
19 import org.opendaylight.yangtools.yang.model.api.stmt.FeatureStatement;
20 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
21 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.Utils;
22
23 import com.google.common.collect.ImmutableList;
24
25 public class FeatureEffectiveStatementImpl extends EffectiveStatementBase<QName, FeatureStatement> implements
26         FeatureDefinition {
27
28     private QName qName;
29     private SchemaPath path;
30     private List<UnknownSchemaNode> unknownSchemaNodes;
31     private String description;
32     private String reference;
33     private Status status;
34
35     public FeatureEffectiveStatementImpl(StmtContext<QName, FeatureStatement, ?> ctx) {
36         super(ctx);
37
38         this.qName = ctx.getStatementArgument();
39         this.path = Utils.getSchemaPath(ctx);
40
41         initFields();
42     }
43
44     private void initFields() {
45
46         List<UnknownSchemaNode> unknownNodesInit = new LinkedList<>();
47
48         for (final EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements()) {
49             if (effectiveStatement instanceof DescriptionEffectiveStatementImpl) {
50                 description = ((DescriptionEffectiveStatementImpl) effectiveStatement).argument();
51             }
52             if (effectiveStatement instanceof ReferenceEffectiveStatementImpl) {
53                 reference = ((ReferenceEffectiveStatementImpl) effectiveStatement).argument();
54             }
55             if (effectiveStatement instanceof StatusEffectiveStatementImpl) {
56                 status = ((StatusEffectiveStatementImpl) effectiveStatement).argument();
57             }
58
59             if (effectiveStatement instanceof UnknownSchemaNode) {
60                 unknownNodesInit.add((UnknownSchemaNode) effectiveStatement);
61             }
62         }
63
64         this.unknownSchemaNodes = ImmutableList.copyOf(unknownNodesInit);
65     }
66
67     @Override
68     public QName getQName() {
69         return qName;
70     }
71
72     @Override
73     public SchemaPath getPath() {
74         return path;
75     }
76
77     @Override
78     public List<UnknownSchemaNode> getUnknownSchemaNodes() {
79         return unknownSchemaNodes;
80     }
81
82     @Override
83     public String getDescription() {
84         return description;
85     }
86
87     @Override
88     public String getReference() {
89         return reference;
90     }
91
92     @Override
93     public Status getStatus() {
94         return status;
95     }
96
97     @Override
98     public int hashCode() {
99         final int prime = 31;
100         int result = 1;
101         result = prime * result + ((qName == null) ? 0 : qName.hashCode());
102         result = prime * result + ((path == null) ? 0 : path.hashCode());
103         return result;
104     }
105
106     @Override
107     public boolean equals(final Object obj) {
108         if (this == obj) {
109             return true;
110         }
111         if (obj == null) {
112             return false;
113         }
114         if (getClass() != obj.getClass()) {
115             return false;
116         }
117         FeatureEffectiveStatementImpl other = (FeatureEffectiveStatementImpl) obj;
118         if (qName == null) {
119             if (other.qName != null) {
120                 return false;
121             }
122         } else if (!qName.equals(other.qName)) {
123             return false;
124         }
125         if (path == null) {
126             if (other.path != null) {
127                 return false;
128             }
129         } else if (!path.equals(other.path)) {
130             return false;
131         }
132         return true;
133     }
134
135     @Override
136     public String toString() {
137         StringBuilder sb = new StringBuilder(FeatureEffectiveStatementImpl.class.getSimpleName());
138         sb.append("[name=").append(qName).append("]");
139         return sb.toString();
140     }
141 }