Sonar issues clean-up
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / effective / CaseEffectiveStatementImpl.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.Collection;
11 import java.util.HashSet;
12 import java.util.LinkedList;
13
14 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.Utils;
15 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
16 import org.opendaylight.yangtools.yang.model.api.stmt.CaseStatement;
17 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
18 import com.google.common.base.Optional;
19 import com.google.common.collect.ImmutableList;
20 import com.google.common.collect.ImmutableSet;
21 import java.util.List;
22 import java.util.Set;
23 import org.opendaylight.yangtools.yang.common.QName;
24 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
25 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
26 import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
27 import org.opendaylight.yangtools.yang.model.api.DerivableSchemaNode;
28 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
29 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
30
31 public class CaseEffectiveStatementImpl extends
32         AbstractEffectiveDocumentedDataNodeContainer<QName, CaseStatement>
33         implements ChoiceCaseNode, DerivableSchemaNode {
34     private final QName qname;
35     private final SchemaPath path;
36
37     boolean augmenting;
38     boolean addedByUses;
39     ChoiceCaseNode original;
40     ConstraintDefinition constraints;
41
42     ImmutableSet<AugmentationSchema> augmentations;
43     ImmutableList<UnknownSchemaNode> unknownNodes;
44
45     public CaseEffectiveStatementImpl(
46             StmtContext<QName, CaseStatement, EffectiveStatement<QName, CaseStatement>> ctx) {
47         super(ctx);
48         this.qname = ctx.getStatementArgument();
49         this.path = Utils.getSchemaPath(ctx);
50         // :TODO init other fields
51
52         initSubstatementCollections();
53     }
54
55     private void initSubstatementCollections() {
56         Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements = effectiveSubstatements();
57
58         List<UnknownSchemaNode> unknownNodesInit = new LinkedList<>();
59         Set<AugmentationSchema> augmentationsInit = new HashSet<>();
60
61         for (EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements) {
62             if (effectiveStatement instanceof UnknownSchemaNode) {
63                 UnknownSchemaNode unknownNode = (UnknownSchemaNode) effectiveStatement;
64                 unknownNodesInit.add(unknownNode);
65             }
66             if (effectiveStatement instanceof AugmentationSchema) {
67                 AugmentationSchema augmentationSchema = (AugmentationSchema) effectiveStatement;
68                 augmentationsInit.add(augmentationSchema);
69             }
70         }
71
72         this.unknownNodes = ImmutableList.copyOf(unknownNodesInit);
73         this.augmentations = ImmutableSet.copyOf(augmentationsInit);
74     }
75
76     @Override
77     public QName getQName() {
78         return qname;
79     }
80
81     @Override
82     public SchemaPath getPath() {
83         return path;
84     }
85
86     @Override
87     public boolean isConfiguration() {
88         return false;
89     }
90
91     @Override
92     public ConstraintDefinition getConstraints() {
93         return constraints;
94     }
95
96     @Override
97     public boolean isAugmenting() {
98         return augmenting;
99     }
100
101     @Override
102     public boolean isAddedByUses() {
103         return addedByUses;
104     }
105
106     @Override
107     public Optional<ChoiceCaseNode> getOriginal() {
108         return Optional.fromNullable(original);
109     }
110
111     @Override
112     public List<UnknownSchemaNode> getUnknownSchemaNodes() {
113         return unknownNodes;
114     }
115
116     @Override
117     public Set<AugmentationSchema> getAvailableAugmentations() {
118         return augmentations;
119     }
120
121     @Override
122     public int hashCode() {
123         final int prime = 31;
124         int result = 1;
125         result = prime * result + ((qname == null) ? 0 : qname.hashCode());
126         result = prime * result + ((path == null) ? 0 : path.hashCode());
127         return result;
128     }
129
130     @Override
131     public boolean equals(final Object obj) {
132         if (this == obj) {
133             return true;
134         }
135         if (obj == null) {
136             return false;
137         }
138         if (getClass() != obj.getClass()) {
139             return false;
140         }
141         CaseEffectiveStatementImpl other = (CaseEffectiveStatementImpl) obj;
142         if (qname == null) {
143             if (other.qname != null) {
144                 return false;
145             }
146         } else if (!qname.equals(other.qname)) {
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 sb = new StringBuilder(
162                 CaseEffectiveStatementImpl.class.getSimpleName());
163         sb.append("[");
164         sb.append("qname=");
165         sb.append(qname);
166         sb.append("]");
167         return sb.toString();
168     }
169
170 }