2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective;
10 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.TypeOfCopy;
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.ChoiceStatement;
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;
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.ChoiceSchemaNode;
28 import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
29 import org.opendaylight.yangtools.yang.model.api.DerivableSchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
31 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
33 public class ChoiceEffectiveStatementImpl extends AbstractEffectiveDocumentedNode<QName, ChoiceStatement> implements ChoiceSchemaNode, DerivableSchemaNode {
34 private final QName qname;
35 private final SchemaPath path;
39 ChoiceSchemaNode original;
40 boolean configuration;
41 ConstraintDefinition constraints;
44 ImmutableSet<ChoiceCaseNode> cases;
45 ImmutableSet<AugmentationSchema> augmentations;
46 ImmutableList<UnknownSchemaNode> unknownNodes;
48 public ChoiceEffectiveStatementImpl(StmtContext<QName, ChoiceStatement, EffectiveStatement<QName, ChoiceStatement>> ctx) {
51 this.qname = ctx.getStatementArgument();
52 this.path = Utils.getSchemaPath(ctx);
53 //:TODO init other fields
55 initSubstatementCollections();
59 private void initCopyType(
60 StmtContext<QName, ChoiceStatement, EffectiveStatement<QName, ChoiceStatement>> ctx) {
62 TypeOfCopy typeOfCopy = ctx.getTypeOfCopy();
64 case ADDED_BY_AUGMENTATION:
66 original = (ChoiceSchemaNode) ctx.getOriginalCtx().buildEffective();
70 original = (ChoiceSchemaNode) ctx.getOriginalCtx().buildEffective();
77 private void initSubstatementCollections() {
78 Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements = effectiveSubstatements();
80 List<UnknownSchemaNode> unknownNodesInit = new LinkedList<>();
81 Set<AugmentationSchema> augmentationsInit = new HashSet<>();
82 Set<ChoiceCaseNode> casesInit = new HashSet<>();
84 for (EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements) {
85 if (effectiveStatement instanceof UnknownSchemaNode) {
86 UnknownSchemaNode unknownNode = (UnknownSchemaNode) effectiveStatement;
87 unknownNodesInit.add(unknownNode);
89 if (effectiveStatement instanceof AugmentationSchema) {
90 AugmentationSchema augmentationSchema = (AugmentationSchema) effectiveStatement;
91 augmentationsInit.add(augmentationSchema);
93 if (effectiveStatement instanceof ChoiceCaseNode) {
94 ChoiceCaseNode choiceCaseNode = (ChoiceCaseNode) effectiveStatement;
95 casesInit.add(choiceCaseNode);
99 this.unknownNodes = ImmutableList.copyOf(unknownNodesInit);
100 this.augmentations = ImmutableSet.copyOf(augmentationsInit);
101 this.cases = ImmutableSet.copyOf(casesInit);
105 public QName getQName() {
110 public SchemaPath getPath() {
115 public boolean isAugmenting() {
120 public boolean isAddedByUses() {
125 public Optional<ChoiceSchemaNode> getOriginal() {
126 return Optional.fromNullable(original);
130 public boolean isConfiguration() {
131 return configuration;
135 public ConstraintDefinition getConstraints() {
140 public Set<AugmentationSchema> getAvailableAugmentations() {
141 return augmentations;
145 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
150 public Set<ChoiceCaseNode> getCases() {
155 public ChoiceCaseNode getCaseNodeByName(final QName name) {
157 throw new IllegalArgumentException("Choice Case QName cannot be NULL!");
159 for (final ChoiceCaseNode caseNode : cases) {
160 if (caseNode != null && name.equals(caseNode.getQName())) {
168 public ChoiceCaseNode getCaseNodeByName(final String name) {
170 throw new IllegalArgumentException("Choice Case string Name cannot be NULL!");
172 for (final ChoiceCaseNode caseNode : cases) {
173 if (caseNode != null && (caseNode.getQName() != null)
174 && name.equals(caseNode.getQName().getLocalName())) {
182 public String getDefaultCase() {
187 public int hashCode() {
188 final int prime = 31;
190 result = prime * result + ((qname == null) ? 0 : qname.hashCode());
191 result = prime * result + ((path == null) ? 0 : path.hashCode());
196 public boolean equals(final Object obj) {
203 if (getClass() != obj.getClass()) {
206 ChoiceEffectiveStatementImpl other = (ChoiceEffectiveStatementImpl) obj;
208 if (other.qname != null) {
211 } else if (!qname.equals(other.qname)) {
215 if (other.path != null) {
218 } else if (!path.equals(other.path)) {
225 public String toString() {
226 StringBuilder sb = new StringBuilder(ChoiceEffectiveStatementImpl.class.getSimpleName());
228 sb.append("qname=").append(qname);
230 return sb.toString();