Add nonnullFoo() getters for non-semantic objects
[mdsal.git] / binding / mdsal-binding-generator / src / main / java / org / opendaylight / mdsal / binding / generator / impl / reactor / AbstractCompositeGenerator.java
index a675e036566d1aa9a2f92137981714cb0c46a99c..8ca6b4079a81a58a9a55685380a9ec5f86f8a644 100644 (file)
@@ -36,6 +36,7 @@ import org.opendaylight.yangtools.yang.model.api.stmt.AugmentEffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.CaseEffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.ChoiceEffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.ContainerEffectiveStatement;
+import org.opendaylight.yangtools.yang.model.api.stmt.FeatureEffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.GroupingEffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.IdentityEffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.InputEffectiveStatement;
@@ -160,11 +161,6 @@ public abstract class AbstractCompositeGenerator<S extends EffectiveStatement<?,
         return childGenerators.iterator();
     }
 
-    @Override
-    final GeneratedType runtimeJavaType() {
-        return generatedType().orElse(null);
-    }
-
     final @NonNull List<AbstractAugmentGenerator> augments() {
         return augments;
     }
@@ -199,19 +195,19 @@ public abstract class AbstractCompositeGenerator<S extends EffectiveStatement<?,
     final @Nullable AbstractExplicitGenerator<?, ?> findGenerator(final MatchStrategy childStrategy,
             // TODO: Wouldn't this method be nicer with Deque<EffectiveStatement<?, ?>> ?
             final List<EffectiveStatement<?, ?>> stmtPath, final int offset) {
-        final EffectiveStatement<?, ?> stmt = stmtPath.get(offset);
+        final var stmt = stmtPath.get(offset);
 
         // Try direct children first, which is simple
-        AbstractExplicitGenerator<?, ?> ret = childStrategy.findGenerator(stmt, childGenerators);
+        var ret = childStrategy.findGenerator(stmt, childGenerators);
         if (ret != null) {
             final int next = offset + 1;
             if (stmtPath.size() == next) {
                 // Final step, return child
                 return ret;
             }
-            if (ret instanceof AbstractCompositeGenerator) {
+            if (ret instanceof AbstractCompositeGenerator<?, ?> composite) {
                 // We know how to descend down
-                return ((AbstractCompositeGenerator<?, ?>) ret).findGenerator(childStrategy, stmtPath, next);
+                return composite.findGenerator(childStrategy, stmtPath, next);
             }
             // Yeah, don't know how to continue here
             return null;
@@ -254,18 +250,17 @@ public abstract class AbstractCompositeGenerator<S extends EffectiveStatement<?,
         // - we propagate those groupings as anchors to any augment statements, which takes out some amount of guesswork
         //   from augment+uses resolution case, as groupings know about their immediate augments as soon as uses linkage
         //   is resolved
-        final List<GroupingGenerator> tmp = new ArrayList<>();
-        for (EffectiveStatement<?, ?> stmt : statement().effectiveSubstatements()) {
-            if (stmt instanceof UsesEffectiveStatement) {
-                final UsesEffectiveStatement uses = (UsesEffectiveStatement) stmt;
-                final GroupingGenerator grouping = context.resolveTreeScoped(GroupingGenerator.class, uses.argument());
+        final var tmp = new ArrayList<GroupingGenerator>();
+        for (var stmt : statement().effectiveSubstatements()) {
+            if (stmt instanceof UsesEffectiveStatement uses) {
+                final var grouping = context.resolveTreeScoped(GroupingGenerator.class, uses.argument());
                 tmp.add(grouping);
 
                 // Trigger resolution of uses/augment statements. This looks like guesswork, but there may be multiple
                 // 'augment' statements in a 'uses' statement and keeping a ListMultimap here seems wasteful.
                 for (Generator gen : this) {
-                    if (gen instanceof UsesAugmentGenerator) {
-                        ((UsesAugmentGenerator) gen).resolveGrouping(uses, grouping);
+                    if (gen instanceof UsesAugmentGenerator usesGen) {
+                        usesGen.resolveGrouping(uses, grouping);
                     }
                 }
             }
@@ -274,12 +269,12 @@ public abstract class AbstractCompositeGenerator<S extends EffectiveStatement<?,
     }
 
     final void startUsesAugmentLinkage(final List<AugmentRequirement> requirements) {
-        for (Generator child : childGenerators) {
-            if (child instanceof UsesAugmentGenerator) {
-                requirements.add(((UsesAugmentGenerator) child).startLinkage());
+        for (var child : childGenerators) {
+            if (child instanceof UsesAugmentGenerator uses) {
+                requirements.add(uses.startLinkage());
             }
-            if (child instanceof AbstractCompositeGenerator) {
-                ((AbstractCompositeGenerator<?, ?>) child).startUsesAugmentLinkage(requirements);
+            if (child instanceof AbstractCompositeGenerator<?, ?> composite) {
+                composite.startUsesAugmentLinkage(requirements);
             }
         }
     }
@@ -322,11 +317,11 @@ public abstract class AbstractCompositeGenerator<S extends EffectiveStatement<?,
                         it.remove();
 
                         // If this is a composite generator we need to process is further
-                        if (child instanceof AbstractCompositeGenerator) {
+                        if (child instanceof AbstractCompositeGenerator<?, ?> composite) {
                             if (unlinkedComposites.isEmpty()) {
                                 unlinkedComposites = new ArrayList<>();
                             }
-                            unlinkedComposites.add((AbstractCompositeGenerator<?, ?>) child);
+                            unlinkedComposites.add(composite);
                         }
                     }
                 }
@@ -393,7 +388,7 @@ public abstract class AbstractCompositeGenerator<S extends EffectiveStatement<?,
 
     @Override
     final AbstractExplicitGenerator<?, ?> findSchemaTreeGenerator(final QName qname) {
-        final AbstractExplicitGenerator<?, ?> found = super.findSchemaTreeGenerator(qname);
+        final var found = super.findSchemaTreeGenerator(qname);
         return found != null ? found : findInferredGenerator(qname);
     }
 
@@ -408,7 +403,7 @@ public abstract class AbstractCompositeGenerator<S extends EffectiveStatement<?,
     }
 
     final @Nullable GroupingGenerator findGroupingForGenerator(final QName qname) {
-        for (GroupingGenerator grouping : groupings) {
+        for (var grouping : groupings) {
             final var gen = grouping.findSchemaTreeGenerator(qname.bindTo(grouping.statement().argument().getModule()));
             if (gen != null) {
                 return grouping;
@@ -444,7 +439,7 @@ public abstract class AbstractCompositeGenerator<S extends EffectiveStatement<?,
      * @return The number of groupings this type uses.
      */
     final int addUsesInterfaces(final GeneratedTypeBuilder builder, final TypeBuilderFactory builderFactory) {
-        for (GroupingGenerator grp : groupings) {
+        for (var grp : groupings) {
             builder.addImplementsType(grp.getGeneratedType(builderFactory));
         }
         return groupings.size();
@@ -455,17 +450,17 @@ public abstract class AbstractCompositeGenerator<S extends EffectiveStatement<?,
     }
 
     final void addGetterMethods(final GeneratedTypeBuilder builder, final TypeBuilderFactory builderFactory) {
-        for (Generator child : this) {
+        for (var child : this) {
             // Only process explicit generators here
-            if (child instanceof AbstractExplicitGenerator) {
-                ((AbstractExplicitGenerator<?, ?>) child).addAsGetterMethod(builder, builderFactory);
+            if (child instanceof AbstractExplicitGenerator<?, ?> explicit) {
+                explicit.addAsGetterMethod(builder, builderFactory);
             }
 
-            final GeneratedType enclosedType = child.enclosedType(builderFactory);
-            if (enclosedType instanceof GeneratedTransferObject) {
-                builder.addEnclosingTransferObject((GeneratedTransferObject) enclosedType);
-            } else if (enclosedType instanceof Enumeration) {
-                builder.addEnumeration((Enumeration) enclosedType);
+            final var enclosedType = child.enclosedType(builderFactory);
+            if (enclosedType instanceof GeneratedTransferObject gto) {
+                builder.addEnclosingTransferObject(gto);
+            } else if (enclosedType instanceof Enumeration enumeration) {
+                builder.addEnumeration(enumeration);
             } else {
                 verify(enclosedType == null, "Unhandled enclosed type %s in %s", enclosedType, child);
             }
@@ -477,70 +472,70 @@ public abstract class AbstractCompositeGenerator<S extends EffectiveStatement<?,
         final var tmpAug = new ArrayList<AbstractAugmentGenerator>();
 
         for (var stmt : statement.effectiveSubstatements()) {
-            if (stmt instanceof ActionEffectiveStatement) {
-                if (!isAugmenting(stmt)) {
-                    tmp.add(new ActionGenerator((ActionEffectiveStatement) stmt, this));
+            if (stmt instanceof ActionEffectiveStatement action) {
+                if (!isAugmenting(action)) {
+                    tmp.add(new ActionGenerator(action, this));
                 }
-            } else if (stmt instanceof AnydataEffectiveStatement) {
-                if (!isAugmenting(stmt)) {
-                    tmp.add(new OpaqueObjectGenerator.Anydata((AnydataEffectiveStatement) stmt, this));
+            } else if (stmt instanceof AnydataEffectiveStatement anydata) {
+                if (!isAugmenting(anydata)) {
+                    tmp.add(new OpaqueObjectGenerator.Anydata(anydata, this));
                 }
-            } else if (stmt instanceof AnyxmlEffectiveStatement) {
-                if (!isAugmenting(stmt)) {
-                    tmp.add(new OpaqueObjectGenerator.Anyxml((AnyxmlEffectiveStatement) stmt, this));
+            } else if (stmt instanceof AnyxmlEffectiveStatement anyxml) {
+                if (!isAugmenting(anyxml)) {
+                    tmp.add(new OpaqueObjectGenerator.Anyxml(anyxml, this));
                 }
-            } else if (stmt instanceof CaseEffectiveStatement) {
-                tmp.add(new CaseGenerator((CaseEffectiveStatement) stmt, this));
-            } else if (stmt instanceof ChoiceEffectiveStatement) {
+            } else if (stmt instanceof CaseEffectiveStatement cast) {
+                tmp.add(new CaseGenerator(cast, this));
+            } else if (stmt instanceof ChoiceEffectiveStatement choice) {
                 // FIXME: use isOriginalDeclaration() ?
-                if (!isAddedByUses(stmt)) {
-                    tmp.add(new ChoiceGenerator((ChoiceEffectiveStatement) stmt, this));
+                if (!isAddedByUses(choice)) {
+                    tmp.add(new ChoiceGenerator(choice, this));
                 }
-            } else if (stmt instanceof ContainerEffectiveStatement) {
-                if (isOriginalDeclaration(stmt)) {
-                    tmp.add(new ContainerGenerator((ContainerEffectiveStatement) stmt, this));
+            } else if (stmt instanceof ContainerEffectiveStatement container) {
+                if (isOriginalDeclaration(container)) {
+                    tmp.add(new ContainerGenerator(container, this));
                 }
-            } else if (stmt instanceof GroupingEffectiveStatement) {
-                tmp.add(new GroupingGenerator((GroupingEffectiveStatement) stmt, this));
-            } else if (stmt instanceof IdentityEffectiveStatement) {
-                tmp.add(new IdentityGenerator((IdentityEffectiveStatement) stmt, this));
-            } else if (stmt instanceof InputEffectiveStatement) {
-                final var cast = (InputEffectiveStatement) stmt;
-                // FIXME: do not generate legacy RPC layout
-                tmp.add(this instanceof RpcGenerator ? new RpcInputGenerator(cast, this)
-                    : new InputGenerator(cast, this));
-            } else if (stmt instanceof LeafEffectiveStatement) {
-                if (!isAugmenting(stmt)) {
-                    tmp.add(new LeafGenerator((LeafEffectiveStatement) stmt, this));
+            } else if (stmt instanceof FeatureEffectiveStatement feature && this instanceof ModuleGenerator parent) {
+                tmp.add(new FeatureGenerator(feature, parent));
+            } else if (stmt instanceof GroupingEffectiveStatement grouping) {
+                tmp.add(new GroupingGenerator(grouping, this));
+            } else if (stmt instanceof IdentityEffectiveStatement identity) {
+                tmp.add(new IdentityGenerator(identity, this));
+            } else if (stmt instanceof InputEffectiveStatement input) {
+                tmp.add(this instanceof RpcGenerator ? new RpcInputGenerator(input, this)
+                    : new InputGenerator(input, this));
+            } else if (stmt instanceof LeafEffectiveStatement leaf) {
+                if (!isAugmenting(leaf)) {
+                    tmp.add(new LeafGenerator(leaf, this));
                 }
-            } else if (stmt instanceof LeafListEffectiveStatement) {
-                if (!isAugmenting(stmt)) {
-                    tmp.add(new LeafListGenerator((LeafListEffectiveStatement) stmt, this));
+            } else if (stmt instanceof LeafListEffectiveStatement leafList) {
+                if (!isAugmenting(leafList)) {
+                    tmp.add(new LeafListGenerator(leafList, this));
                 }
-            } else if (stmt instanceof ListEffectiveStatement) {
-                if (isOriginalDeclaration(stmt)) {
-                    final ListGenerator listGen = new ListGenerator((ListEffectiveStatement) stmt, this);
+            } else if (stmt instanceof ListEffectiveStatement list) {
+                if (isOriginalDeclaration(list)) {
+                    final var listGen = new ListGenerator(list, this);
                     tmp.add(listGen);
 
-                    final KeyGenerator keyGen = listGen.keyGenerator();
+                    final var keyGen = listGen.keyGenerator();
                     if (keyGen != null) {
                         tmp.add(keyGen);
                     }
                 }
-            } else if (stmt instanceof NotificationEffectiveStatement) {
-                if (!isAugmenting(stmt)) {
-                    tmp.add(new NotificationGenerator((NotificationEffectiveStatement) stmt, this));
+            } else if (stmt instanceof NotificationEffectiveStatement notification) {
+                if (!isAugmenting(notification)) {
+                    tmp.add(new NotificationGenerator(notification, this));
+                }
+            } else if (stmt instanceof OutputEffectiveStatement output) {
+                tmp.add(this instanceof RpcGenerator ? new RpcOutputGenerator(output, this)
+                    : new OutputGenerator(output, this));
+            } else if (stmt instanceof RpcEffectiveStatement rpc) {
+                if (this instanceof ModuleGenerator module) {
+                    tmp.add(new RpcGenerator(rpc, module));
                 }
-            } else if (stmt instanceof OutputEffectiveStatement) {
-                final var cast = (OutputEffectiveStatement) stmt;
-                // FIXME: do not generate legacy RPC layout
-                tmp.add(this instanceof RpcGenerator ? new RpcOutputGenerator(cast, this)
-                    : new OutputGenerator(cast, this));
-            } else if (stmt instanceof RpcEffectiveStatement) {
-                tmp.add(new RpcGenerator((RpcEffectiveStatement) stmt, this));
-            } else if (stmt instanceof TypedefEffectiveStatement) {
-                tmp.add(new TypedefGenerator((TypedefEffectiveStatement) stmt, this));
-            } else if (stmt instanceof AugmentEffectiveStatement) {
+            } else if (stmt instanceof TypedefEffectiveStatement typedef) {
+                tmp.add(new TypedefGenerator(typedef, this));
+            } else if (stmt instanceof AugmentEffectiveStatement augment) {
                 // FIXME: MDSAL-695: So here we are ignoring any augment which is not in a module, while the 'uses'
                 //                   processing takes care of the rest. There are two problems here:
                 //
@@ -555,14 +550,13 @@ public abstract class AbstractCompositeGenerator<S extends EffectiveStatement<?,
                 //                   So here is where we should decide how to handle this augment, and make sure we
                 //                   retain information about this being an alias. That will serve as the base for keys
                 //                   in the augment -> original map we provide to BindingRuntimeTypes.
-                if (this instanceof ModuleGenerator) {
-                    tmpAug.add(new ModuleAugmentGenerator((AugmentEffectiveStatement) stmt, this));
+                if (this instanceof ModuleGenerator module) {
+                    tmpAug.add(new ModuleAugmentGenerator(augment, module));
                 }
-            } else if (stmt instanceof UsesEffectiveStatement) {
-                final UsesEffectiveStatement uses = (UsesEffectiveStatement) stmt;
-                for (EffectiveStatement<?, ?> usesSub : uses.effectiveSubstatements()) {
-                    if (usesSub instanceof AugmentEffectiveStatement) {
-                        tmpAug.add(new UsesAugmentGenerator((AugmentEffectiveStatement) usesSub, uses, this));
+            } else if (stmt instanceof UsesEffectiveStatement uses) {
+                for (var usesSub : uses.effectiveSubstatements()) {
+                    if (usesSub instanceof AugmentEffectiveStatement usesAug) {
+                        tmpAug.add(new UsesAugmentGenerator(usesAug, uses, this));
                     }
                 }
             } else {
@@ -578,9 +572,7 @@ public abstract class AbstractCompositeGenerator<S extends EffectiveStatement<?,
         tmp.addAll(tmpAug);
 
         // Compatibility FooService and FooListener interfaces, only generated for modules.
-        if (this instanceof ModuleGenerator) {
-            final ModuleGenerator moduleGen = (ModuleGenerator) this;
-
+        if (this instanceof ModuleGenerator moduleGen) {
             final List<NotificationGenerator> notifs = tmp.stream()
                 .filter(NotificationGenerator.class::isInstance)
                 .map(NotificationGenerator.class::cast)
@@ -604,9 +596,9 @@ public abstract class AbstractCompositeGenerator<S extends EffectiveStatement<?,
     // Utility equivalent of (!isAddedByUses(stmt) && !isAugmenting(stmt)). Takes advantage of relationship between
     // CopyableNode and AddedByUsesAware
     private static boolean isOriginalDeclaration(final EffectiveStatement<?, ?> stmt) {
-        if (stmt instanceof AddedByUsesAware) {
-            if (((AddedByUsesAware) stmt).isAddedByUses()
-                || stmt instanceof CopyableNode && ((CopyableNode) stmt).isAugmenting()) {
+        if (stmt instanceof AddedByUsesAware aware) {
+            if (aware.isAddedByUses()
+                || stmt instanceof CopyableNode copyable && copyable.isAugmenting()) {
                 return false;
             }
         }
@@ -614,10 +606,10 @@ public abstract class AbstractCompositeGenerator<S extends EffectiveStatement<?,
     }
 
     private static boolean isAddedByUses(final EffectiveStatement<?, ?> stmt) {
-        return stmt instanceof AddedByUsesAware && ((AddedByUsesAware) stmt).isAddedByUses();
+        return stmt instanceof AddedByUsesAware aware && aware.isAddedByUses();
     }
 
     private static boolean isAugmenting(final EffectiveStatement<?, ?> stmt) {
-        return stmt instanceof CopyableNode && ((CopyableNode) stmt).isAugmenting();
+        return stmt instanceof CopyableNode copyable && copyable.isAugmenting();
     }
 }