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