Simplify YangSourceDefinition 82/95982/1
authorRobert Varga <robert.varga@pantheon.tech>
Tue, 4 May 2021 10:32:18 +0000 (12:32 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Tue, 4 May 2021 10:51:22 +0000 (12:51 +0200)
We can use asEffectiveStatement() instead of guessing, make the code
flow more obvious.

Change-Id: Ie249bcc05820f7670fab86e9b0e4cfd1dbfa43ac
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
binding/mdsal-binding-generator-api/src/main/java/org/opendaylight/mdsal/binding/model/api/YangSourceDefinition.java

index 011d66d1546f538ee47535120f0c00984c4d38d4..943f170e4f468459bbbed41798667a7235a7de8b 100644 (file)
@@ -27,8 +27,6 @@ import org.opendaylight.yangtools.yang.model.api.stmt.ModuleStatement;
 
 /**
  * DTO capturing the YANG source definition which lead to a {@link GeneratedType} being emitted.
- *
- * @author Robert Varga
  */
 @Beta
 @NonNullByDefault
@@ -79,24 +77,23 @@ public abstract class YangSourceDefinition {
     }
 
     public static Optional<YangSourceDefinition> of(final Module module) {
-        if (module instanceof ModuleEffectiveStatement) {
-            final ModuleEffectiveStatement effective = (ModuleEffectiveStatement) module;
-            final ModuleStatement declared = effective.getDeclared();
-            if (declared != null) {
-                return Optional.of(new Single(effective, module));
-            }
+        final ModuleEffectiveStatement effective = module.asEffectiveStatement();
+        final ModuleStatement declared = effective.getDeclared();
+        if (declared != null) {
+            return Optional.of(new Single(effective, module));
         }
         return Optional.empty();
     }
 
     public static Optional<YangSourceDefinition> of(final Module module, final SchemaNode node) {
-        if (module instanceof ModuleEffectiveStatement) {
-            final ModuleEffectiveStatement effective = (ModuleEffectiveStatement) module;
-            if (node instanceof EffectiveStatement) {
-                final DeclaredStatement<?> declared = ((EffectiveStatement<?, ?>) node).getDeclared();
-                if (declared != null) {
-                    return Optional.of(new Single(effective, node));
-                }
+        return of(module.asEffectiveStatement(), node);
+    }
+
+    public static Optional<YangSourceDefinition> of(final ModuleEffectiveStatement module, final SchemaNode node) {
+        if (node instanceof EffectiveStatement) {
+            final DeclaredStatement<?> declared = ((EffectiveStatement<?, ?>) node).getDeclared();
+            if (declared != null) {
+                return Optional.of(new Single(module, node));
             }
         }
         return Optional.empty();
@@ -105,13 +102,10 @@ public abstract class YangSourceDefinition {
     public static Optional<YangSourceDefinition> of(final Module module, final Collection<? extends SchemaNode> nodes) {
         checkArgument(!nodes.isEmpty());
 
-        if (module instanceof ModuleEffectiveStatement) {
-            final ModuleEffectiveStatement effective = (ModuleEffectiveStatement) module;
-            final boolean anyDeclared = nodes.stream().anyMatch(node ->
-                node instanceof EffectiveStatement && ((EffectiveStatement<?, ?>) node).getDeclared() != null);
-            if (anyDeclared) {
-                return Optional.of(new Multiple(effective, nodes));
-            }
+        final boolean anyDeclared = nodes.stream().anyMatch(
+            node -> node instanceof EffectiveStatement && ((EffectiveStatement<?, ?>) node).getDeclared() != null);
+        if (anyDeclared) {
+            return Optional.of(new Multiple(module.asEffectiveStatement(), nodes));
         }
         return Optional.empty();
     }