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