X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=yang%2Fyang-parser-impl%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fyangtools%2Fyang%2Fparser%2Fstmt%2Frfc6020%2Feffective%2FChoiceEffectiveStatementImpl.java;h=74fdd97c242d39de19ed18762818326a7cfd2635;hb=3c4bdfac3a8b04ecd1d6eab2cdadbb365b54664e;hp=d74600e37d7b383f565af34968bc225e2f29182e;hpb=ed4fee64bc9bdb4005d11aa35355e9367065998f;p=yangtools.git diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/ChoiceEffectiveStatementImpl.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/ChoiceEffectiveStatementImpl.java index d74600e37d..74fdd97c24 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/ChoiceEffectiveStatementImpl.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/ChoiceEffectiveStatementImpl.java @@ -1,4 +1,4 @@ -/** +/* * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved. * * This program and the accompanying materials are made available under the @@ -7,66 +7,62 @@ */ 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 implements ChoiceSchemaNode, DerivableSchemaNode { - private final QName qname; - private final SchemaPath path; +public final class ChoiceEffectiveStatementImpl extends AbstractEffectiveDataSchemaNode implements + ChoiceSchemaNode, DerivableSchemaNode { + /** + * Comparator based on alphabetical order of local name of SchemaNode's + * qname. + */ + private static final Comparator 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 cases; - ImmutableSet augmentations; - ImmutableList unknownNodes; + private final Set cases; + private final Set augmentations; - public ChoiceEffectiveStatementImpl(StmtContext> ctx) { + public ChoiceEffectiveStatementImpl( + final StmtContext> 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> effectiveSubstatements = effectiveSubstatements(); - - List unknownNodesInit = new LinkedList<>(); - Set augmentationsInit = new HashSet<>(); - Set casesInit = new HashSet<>(); + Set augmentationsInit = new LinkedHashSet<>(); + SortedSet casesInit = new TreeSet<>(SCHEMA_NODE_COMP); for (EffectiveStatement effectiveStatement : effectiveSubstatements) { - if (effectiveStatement instanceof UnknownSchemaNode) { - UnknownSchemaNode unknownNode = (UnknownSchemaNode) effectiveStatement; - unknownNodesInit.add(unknownNode); - } if (effectiveStatement instanceof AugmentationSchema) { AugmentationSchema augmentationSchema = (AugmentationSchema) effectiveStatement; augmentationsInit.add(augmentationSchema); @@ -75,31 +71,41 @@ public class ChoiceEffectiveStatementImpl extends AbstractEffectiveDocumentedNod ChoiceCaseNode choiceCaseNode = (ChoiceCaseNode) effectiveStatement; casesInit.add(choiceCaseNode); } + if (effectiveStatement instanceof AnyXmlSchemaNode || effectiveStatement instanceof ContainerSchemaNode + || effectiveStatement instanceof ListSchemaNode || effectiveStatement instanceof LeafListSchemaNode + || effectiveStatement instanceof LeafSchemaNode) { + + DataSchemaNode dataSchemaNode = (DataSchemaNode) effectiveStatement; + ChoiceCaseNode shorthandCase = new CaseShorthandImpl(dataSchemaNode); + casesInit.add(shorthandCase); + + if (dataSchemaNode.isAugmenting() && !this.augmenting) { + resetAugmenting(dataSchemaNode); + } + } } - this.unknownNodes = ImmutableList.copyOf(unknownNodesInit); this.augmentations = ImmutableSet.copyOf(augmentationsInit); this.cases = ImmutableSet.copyOf(casesInit); } - @Override - public QName getQName() { - return qname; - } - - @Override - public SchemaPath getPath() { - return path; - } - - @Override - public boolean isAugmenting() { - return augmenting; - } - - @Override - public boolean isAddedByUses() { - return addedByUses; + 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 @@ -107,26 +113,11 @@ public class ChoiceEffectiveStatementImpl extends AbstractEffectiveDocumentedNod return Optional.fromNullable(original); } - @Override - public boolean isConfiguration() { - return configuration; - } - - @Override - public ConstraintDefinition getConstraints() { - return constraints; - } - @Override public Set getAvailableAugmentations() { return augmentations; } - @Override - public List getUnknownSchemaNodes() { - return unknownNodes; - } - @Override public Set getCases() { return cases; @@ -134,9 +125,8 @@ public class ChoiceEffectiveStatementImpl extends AbstractEffectiveDocumentedNod @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; @@ -147,12 +137,10 @@ public class ChoiceEffectiveStatementImpl extends AbstractEffectiveDocumentedNod @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; } } @@ -168,8 +156,8 @@ public class ChoiceEffectiveStatementImpl extends AbstractEffectiveDocumentedNod 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; } @@ -185,30 +173,13 @@ public class ChoiceEffectiveStatementImpl extends AbstractEffectiveDocumentedNod 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 +}