Introduce DataNodeContainer.findDataChildByName()
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / effective / AbstractEffectiveDocumentedDataNodeContainer.java
index 8d3e6735ab17bb7d10f1dd25b9b22af514a5fdc2..9cbb9c5766a24476882f25f8bc41a72e969a2c65 100644 (file)
@@ -7,17 +7,18 @@
  */
 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective;
 
+import static java.util.Objects.requireNonNull;
+
 import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.ImmutableSet;
-import java.util.Collection;
 import java.util.HashSet;
 import java.util.LinkedHashMap;
 import java.util.LinkedHashSet;
 import java.util.Map;
-import java.util.Objects;
+import java.util.Optional;
 import java.util.Set;
 import org.opendaylight.yangtools.yang.common.QName;
-import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
+import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
@@ -45,25 +46,23 @@ abstract class AbstractEffectiveDocumentedDataNodeContainer<A, D extends Declare
             final StmtContext<A, D, ?> ctx) {
         super(ctx);
 
-        Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements = effectiveSubstatements();
-
         Map<QName, DataSchemaNode> mutableChildNodes = new LinkedHashMap<>();
         Set<GroupingDefinition> mutableGroupings = new HashSet<>();
         Set<UsesNode> mutableUses = new HashSet<>();
         Set<TypeDefinition<?>> mutableTypeDefinitions = new LinkedHashSet<>();
         Set<DataSchemaNode> mutablePublicChildNodes = new LinkedHashSet<>();
 
-        for (EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements) {
-            if (effectiveStatement instanceof DataSchemaNode) {
-                final DataSchemaNode dataSchemaNode = (DataSchemaNode) effectiveStatement;
+        for (EffectiveStatement<?, ?> stmt : effectiveSubstatements()) {
+            if (stmt instanceof DataSchemaNode) {
+                final DataSchemaNode dataSchemaNode = (DataSchemaNode) stmt;
                 if (!mutableChildNodes.containsKey(dataSchemaNode.getQName())) {
                     /**
                      * Add case short hand when augmenting choice with short hand
                      **/
-                    if (this instanceof AugmentationSchema && !(effectiveStatement instanceof ChoiceCaseNode ||
-                            effectiveStatement instanceof ChoiceSchemaNode) &&
-                            (YangValidationBundles.SUPPORTED_CASE_SHORTHANDS.contains(effectiveStatement.statementDefinition())) &&
-                            Objects.equals(true, ctx.getFromNamespace(AugmentToChoiceNamespace.class, ctx))) {
+                    if (this instanceof AugmentationSchemaNode
+                            && !(stmt instanceof ChoiceCaseNode || stmt instanceof ChoiceSchemaNode)
+                            && YangValidationBundles.SUPPORTED_CASE_SHORTHANDS.contains(stmt.statementDefinition())
+                            && Boolean.TRUE.equals(ctx.getFromNamespace(AugmentToChoiceNamespace.class, ctx))) {
                         final CaseShorthandImpl caseShorthand = new CaseShorthandImpl(dataSchemaNode);
                         mutableChildNodes.put(caseShorthand.getQName(), caseShorthand);
                         mutablePublicChildNodes.add(caseShorthand);
@@ -72,32 +71,32 @@ abstract class AbstractEffectiveDocumentedDataNodeContainer<A, D extends Declare
                         mutablePublicChildNodes.add(dataSchemaNode);
                     }
                 } else {
-                    throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
+                    throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, stmt);
                 }
             }
-            if (effectiveStatement instanceof UsesNode) {
-                UsesNode usesNode = (UsesNode) effectiveStatement;
+            if (stmt instanceof UsesNode) {
+                UsesNode usesNode = (UsesNode) stmt;
                 if (!mutableUses.contains(usesNode)) {
                     mutableUses.add(usesNode);
                 } else {
-                    throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
+                    throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, stmt);
                 }
             }
-            if (effectiveStatement instanceof TypeDefEffectiveStatementImpl) {
-                TypeDefEffectiveStatementImpl typeDef = (TypeDefEffectiveStatementImpl) effectiveStatement;
+            if (stmt instanceof TypeDefEffectiveStatementImpl) {
+                TypeDefEffectiveStatementImpl typeDef = (TypeDefEffectiveStatementImpl) stmt;
                 TypeDefinition<?> type = typeDef.getTypeDefinition();
                 if (!mutableTypeDefinitions.contains(type)) {
                     mutableTypeDefinitions.add(type);
                 } else {
-                    throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
+                    throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, stmt);
                 }
             }
-            if (effectiveStatement instanceof GroupingDefinition) {
-                GroupingDefinition grp = (GroupingDefinition) effectiveStatement;
+            if (stmt instanceof GroupingDefinition) {
+                GroupingDefinition grp = (GroupingDefinition) stmt;
                 if (!mutableGroupings.contains(grp)) {
                     mutableGroupings.add(grp);
                 } else {
-                    throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
+                    throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, stmt);
                 }
             }
         }
@@ -125,10 +124,9 @@ abstract class AbstractEffectiveDocumentedDataNodeContainer<A, D extends Declare
     }
 
     @Override
-    public final DataSchemaNode getDataChildByName(final QName name) {
-        // Child nodes are keyed by their container name, so we can do a direct
-        // lookup
-        return childNodes.get(name);
+    public final Optional<DataSchemaNode> findDataChildByName(final QName name) {
+        // Child nodes are keyed by their container name, so we can do a direct lookup
+        return Optional.ofNullable(childNodes.get(requireNonNull(name)));
     }
 
     @Override