Merge "Fix sonar issues in AnyXmlEffectiveStatementImpl"
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / effective / LeafListEffectiveStatementImpl.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.LinkedList;
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.parser.spi.meta.StmtContext;
17 import org.opendaylight.yangtools.yang.model.api.stmt.LeafListStatement;
18 import com.google.common.base.Optional;
19 import com.google.common.collect.ImmutableList;
20 import java.util.List;
21 import org.opendaylight.yangtools.yang.common.QName;
22 import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
23 import org.opendaylight.yangtools.yang.model.api.DerivableSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
26 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
27 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
28
29 public class LeafListEffectiveStatementImpl extends
30         AbstractEffectiveDocumentedNode<QName, LeafListStatement> implements
31         LeafListSchemaNode, DerivableSchemaNode {
32     private final QName qname;
33     private final SchemaPath path;
34
35     boolean augmenting;
36     boolean addedByUses;
37     LeafListSchemaNode original;
38     boolean configuration;
39     ConstraintDefinition constraintsDef;
40     TypeDefinition<?> type;
41     boolean userOrdered;
42
43     private ImmutableList<UnknownSchemaNode> unknownNodes;
44
45     public LeafListEffectiveStatementImpl(
46             StmtContext<QName, LeafListStatement, EffectiveStatement<QName, LeafListStatement>> ctx) {
47         super(ctx);
48         this.qname = ctx.getStatementArgument();
49         this.path = Utils.getSchemaPath(ctx);
50         // :TODO init other fields
51
52         initSubstatementCollections();
53         initCopyType(ctx);
54     }
55
56     private void initCopyType(
57             StmtContext<QName, LeafListStatement, EffectiveStatement<QName, LeafListStatement>> ctx) {
58
59         TypeOfCopy typeOfCopy = ctx.getTypeOfCopy();
60         switch (typeOfCopy) {
61         case ADDED_BY_AUGMENTATION:
62             augmenting = true;
63             original = (LeafListSchemaNode) ctx.getOriginalCtx()
64                     .buildEffective();
65             break;
66         case ADDED_BY_USES:
67             addedByUses = true;
68             original = (LeafListSchemaNode) ctx.getOriginalCtx()
69                     .buildEffective();
70             break;
71         default:
72             break;
73         }
74     }
75
76     private void initSubstatementCollections() {
77         Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements = effectiveSubstatements();
78
79         List<UnknownSchemaNode> unknownNodesInit = new LinkedList<>();
80
81         for (EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements) {
82             if (effectiveStatement instanceof UnknownSchemaNode) {
83                 UnknownSchemaNode unknownNode = (UnknownSchemaNode) effectiveStatement;
84                 unknownNodesInit.add(unknownNode);
85             }
86         }
87
88         this.unknownNodes = ImmutableList.copyOf(unknownNodesInit);
89     }
90
91     @Override
92     public QName getQName() {
93         return qname;
94     }
95
96     @Override
97     public SchemaPath getPath() {
98         return path;
99     }
100
101     @Override
102     public boolean isAugmenting() {
103         return augmenting;
104     }
105
106     @Override
107     public boolean isAddedByUses() {
108         return addedByUses;
109     }
110
111     @Override
112     public Optional<LeafListSchemaNode> getOriginal() {
113         return Optional.fromNullable(original);
114     }
115
116     @Override
117     public boolean isConfiguration() {
118         return configuration;
119     }
120
121     @Override
122     public ConstraintDefinition getConstraints() {
123         return constraintsDef;
124     }
125
126     @Override
127     public TypeDefinition<?> getType() {
128         return type;
129     }
130
131     @Override
132     public boolean isUserOrdered() {
133         return userOrdered;
134     }
135
136     @Override
137     public List<UnknownSchemaNode> getUnknownSchemaNodes() {
138         return unknownNodes;
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         LeafListEffectiveStatementImpl other = (LeafListEffectiveStatementImpl) 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                 LeafListEffectiveStatementImpl.class.getSimpleName());
183         sb.append("[");
184         sb.append(qname);
185         sb.append("]");
186         return sb.toString();
187     }
188 }