-/**
+/*
* Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
*
* This program and the accompanying materials are made available under the
*/
package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective;
-import java.util.Collection;
-import java.util.HashSet;
-import java.util.LinkedList;
-
-import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.Utils;
-import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
-import org.opendaylight.yangtools.yang.model.api.stmt.ChoiceStatement;
-import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
import com.google.common.base.Optional;
-import com.google.common.collect.ImmutableList;
+import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableSet;
-import java.util.List;
+import java.util.Collection;
+import java.util.Comparator;
+import java.util.LinkedHashSet;
+import java.util.Objects;
import java.util.Set;
+import java.util.SortedSet;
+import java.util.TreeSet;
import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode;
import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
+import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
import org.opendaylight.yangtools.yang.model.api.DerivableSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.SchemaPath;
-import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.SchemaNode;
+import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
+import org.opendaylight.yangtools.yang.model.api.stmt.ChoiceStatement;
+import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
-public class ChoiceEffectiveStatementImpl extends AbstractEffectiveDocumentedNode<QName, ChoiceStatement> implements ChoiceSchemaNode, DerivableSchemaNode {
- private final QName qname;
- private final SchemaPath path;
+public final class ChoiceEffectiveStatementImpl extends AbstractEffectiveDataSchemaNode<ChoiceStatement> implements
+ ChoiceSchemaNode, DerivableSchemaNode {
+ /**
+ * Comparator based on alphabetical order of local name of SchemaNode's
+ * qname.
+ */
+ private static final Comparator<SchemaNode> SCHEMA_NODE_COMP = (o1, o2) -> {
+ return o1.getQName().compareTo(o2.getQName());
+ };
- boolean augmenting;
- boolean addedByUses;
- ChoiceSchemaNode original;
- boolean configuration;
- ConstraintDefinition constraints;
- String defaultCase;
+ private final ChoiceSchemaNode original;
+ private final String defaultCase;
- ImmutableSet<ChoiceCaseNode> cases;
- ImmutableSet<AugmentationSchema> augmentations;
- ImmutableList<UnknownSchemaNode> unknownNodes;
+ private final Set<ChoiceCaseNode> cases;
+ private final Set<AugmentationSchema> augmentations;
- public ChoiceEffectiveStatementImpl(StmtContext<QName, ChoiceStatement, EffectiveStatement<QName, ChoiceStatement>> ctx) {
+ public ChoiceEffectiveStatementImpl(
+ final StmtContext<QName, ChoiceStatement, EffectiveStatement<QName, ChoiceStatement>> ctx) {
super(ctx);
+ this.original = ctx.getOriginalCtx() == null ? null : (ChoiceSchemaNode) ctx.getOriginalCtx().buildEffective();
- this.qname = ctx.getStatementArgument();
- this.path = Utils.getSchemaPath(ctx);
- //:TODO init other fields
-
- initSubstatementCollections();
-
- }
+ DefaultEffectiveStatementImpl defaultStmt = firstEffective(DefaultEffectiveStatementImpl.class);
+ this.defaultCase = (defaultStmt == null) ? null : defaultStmt.argument();
- private void initSubstatementCollections() {
+ // initSubstatementCollectionsAndFields
Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements = effectiveSubstatements();
-
- LinkedList<UnknownSchemaNode> unknownNodes = new LinkedList<UnknownSchemaNode>();
- HashSet<AugmentationSchema> augmentations = new HashSet<AugmentationSchema>();
- HashSet<ChoiceCaseNode> cases = new HashSet<ChoiceCaseNode>();
+ Set<AugmentationSchema> augmentationsInit = new LinkedHashSet<>();
+ SortedSet<ChoiceCaseNode> casesInit = new TreeSet<>(SCHEMA_NODE_COMP);
for (EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements) {
- if (effectiveStatement instanceof UnknownSchemaNode) {
- UnknownSchemaNode unknownNode = (UnknownSchemaNode) effectiveStatement;
- unknownNodes.add(unknownNode);
- }
if (effectiveStatement instanceof AugmentationSchema) {
AugmentationSchema augmentationSchema = (AugmentationSchema) effectiveStatement;
- augmentations.add(augmentationSchema);
+ augmentationsInit.add(augmentationSchema);
}
if (effectiveStatement instanceof ChoiceCaseNode) {
ChoiceCaseNode choiceCaseNode = (ChoiceCaseNode) effectiveStatement;
- cases.add(choiceCaseNode);
+ casesInit.add(choiceCaseNode);
}
- }
-
- this.unknownNodes = ImmutableList.copyOf(unknownNodes);
- this.augmentations = ImmutableSet.copyOf(augmentations);
- this.cases = ImmutableSet.copyOf(cases);
- }
-
- @Override
- public QName getQName() {
- return qname;
- }
+ if (effectiveStatement instanceof AnyXmlSchemaNode || effectiveStatement instanceof ContainerSchemaNode
+ || effectiveStatement instanceof ListSchemaNode || effectiveStatement instanceof LeafListSchemaNode
+ || effectiveStatement instanceof LeafSchemaNode) {
- @Override
- public SchemaPath getPath() {
- return path;
- }
+ DataSchemaNode dataSchemaNode = (DataSchemaNode) effectiveStatement;
+ ChoiceCaseNode shorthandCase = new CaseShorthandImpl(dataSchemaNode);
+ casesInit.add(shorthandCase);
- @Override
- public boolean isAugmenting() {
- return augmenting;
- }
+ if (dataSchemaNode.isAugmenting() && !this.augmenting) {
+ resetAugmenting(dataSchemaNode);
+ }
+ }
+ }
- @Override
- public boolean isAddedByUses() {
- return addedByUses;
+ this.augmentations = ImmutableSet.copyOf(augmentationsInit);
+ this.cases = ImmutableSet.copyOf(casesInit);
+ }
+
+ private static void resetAugmenting(final DataSchemaNode dataSchemaNode) {
+ if (dataSchemaNode instanceof LeafEffectiveStatementImpl) {
+ LeafEffectiveStatementImpl leaf = (LeafEffectiveStatementImpl) dataSchemaNode;
+ leaf.augmenting = false;
+ } else if (dataSchemaNode instanceof ContainerEffectiveStatementImpl) {
+ ContainerEffectiveStatementImpl container = (ContainerEffectiveStatementImpl) dataSchemaNode;
+ container.augmenting = false;
+ } else if (dataSchemaNode instanceof LeafListEffectiveStatementImpl) {
+ LeafListEffectiveStatementImpl leafList = (LeafListEffectiveStatementImpl) dataSchemaNode;
+ leafList.augmenting = false;
+ } else if (dataSchemaNode instanceof ListEffectiveStatementImpl) {
+ ListEffectiveStatementImpl list = (ListEffectiveStatementImpl) dataSchemaNode;
+ list.augmenting = false;
+ } else if (dataSchemaNode instanceof AnyXmlEffectiveStatementImpl) {
+ AnyXmlEffectiveStatementImpl anyXml = (AnyXmlEffectiveStatementImpl) dataSchemaNode;
+ anyXml.augmenting = false;
+ }
}
@Override
return Optional.fromNullable(original);
}
- @Override
- public boolean isConfiguration() {
- return configuration;
- }
-
- @Override
- public ConstraintDefinition getConstraints() {
- return constraints;
- }
-
@Override
public Set<AugmentationSchema> getAvailableAugmentations() {
return augmentations;
}
- @Override
- public List<UnknownSchemaNode> getUnknownSchemaNodes() {
- return unknownNodes;
- }
-
@Override
public Set<ChoiceCaseNode> getCases() {
return cases;
@Override
public ChoiceCaseNode getCaseNodeByName(final QName name) {
- if (name == null) {
- throw new IllegalArgumentException("Choice Case QName cannot be NULL!");
- }
+ Preconditions.checkArgument(name != null, "Choice Case QName cannot be NULL!");
+
for (final ChoiceCaseNode caseNode : cases) {
if (caseNode != null && name.equals(caseNode.getQName())) {
return caseNode;
@Override
public ChoiceCaseNode getCaseNodeByName(final String name) {
- if (name == null) {
- throw new IllegalArgumentException("Choice Case string Name cannot be NULL!");
- }
+ Preconditions.checkArgument(name != null, "Choice Case string Name cannot be NULL!");
+
for (final ChoiceCaseNode caseNode : cases) {
- if (caseNode != null && (caseNode.getQName() != null)
- && name.equals(caseNode.getQName().getLocalName())) {
+ if (caseNode != null && (caseNode.getQName() != null) && name.equals(caseNode.getQName().getLocalName())) {
return caseNode;
}
}
public int hashCode() {
final int prime = 31;
int result = 1;
- result = prime * result + ((qname == null) ? 0 : qname.hashCode());
- result = prime * result + ((path == null) ? 0 : path.hashCode());
+ result = prime * result + Objects.hashCode(getQName());
+ result = prime * result + Objects.hashCode(getPath());
return result;
}
return false;
}
ChoiceEffectiveStatementImpl other = (ChoiceEffectiveStatementImpl) obj;
- if (qname == null) {
- if (other.qname != null) {
- return false;
- }
- } else if (!qname.equals(other.qname)) {
- return false;
- }
- if (path == null) {
- if (other.path != null) {
- return false;
- }
- } else if (!path.equals(other.path)) {
- return false;
- }
- return true;
+ return Objects.equals(getQName(), other.getQName()) && Objects.equals(getPath(), other.getPath());
}
@Override
public String toString() {
- StringBuilder sb = new StringBuilder(ChoiceEffectiveStatementImpl.class.getSimpleName());
- sb.append("[");
- sb.append("qname=").append(qname);
- sb.append("]");
- return sb.toString();
+ return ChoiceEffectiveStatementImpl.class.getSimpleName() + "[" +
+ "qname=" + getQName() +
+ "]";
}
-
-}
\ No newline at end of file
+}