Split out AbstractTypeStatementSupport.resolveTypeReference() 81/90881/4
authorRobert Varga <robert.varga@pantheon.tech>
Thu, 2 Jul 2020 16:22:18 +0000 (18:22 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Thu, 2 Jul 2020 22:31:55 +0000 (00:31 +0200)
The implementation of createEffective() method is quite huge,
split out the switch lookup part out of it, saving a few lines.

JIRA: YANGTOOLS-1065
Change-Id: I97c36b2263a3ee2c4f394f77e3d5651846aa91f3
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
yang/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/stmt/type/AbstractTypeStatementSupport.java

index 0f1f85e410c2755765ba68223c41822f3632a98c..2330fedf670f9a5b13f051e1c24b15ae1d0c5ed7 100644 (file)
@@ -11,6 +11,7 @@ import static com.google.common.base.Preconditions.checkArgument;
 
 import com.google.common.collect.ImmutableMap;
 import java.util.Collection;
+import org.eclipse.jdt.annotation.NonNull;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
@@ -143,54 +144,7 @@ abstract class AbstractTypeStatementSupport
             final StmtContext<String, TypeStatement, EffectiveStatement<String, TypeStatement>> ctx) {
 
         // First look up the proper base type
-        final TypeEffectiveStatement<TypeStatement> typeStmt;
-        switch (ctx.coerceStatementArgument()) {
-            case BINARY:
-                typeStmt = BuiltinEffectiveStatement.BINARY;
-                break;
-            case BOOLEAN:
-                typeStmt = BuiltinEffectiveStatement.BOOLEAN;
-                break;
-            case EMPTY:
-                typeStmt = BuiltinEffectiveStatement.EMPTY;
-                break;
-            case INSTANCE_IDENTIFIER:
-                typeStmt = BuiltinEffectiveStatement.INSTANCE_IDENTIFIER;
-                break;
-            case INT8:
-                typeStmt = BuiltinEffectiveStatement.INT8;
-                break;
-            case INT16:
-                typeStmt = BuiltinEffectiveStatement.INT16;
-                break;
-            case INT32:
-                typeStmt = BuiltinEffectiveStatement.INT32;
-                break;
-            case INT64:
-                typeStmt = BuiltinEffectiveStatement.INT64;
-                break;
-            case STRING:
-                typeStmt = BuiltinEffectiveStatement.STRING;
-                break;
-            case UINT8:
-                typeStmt = BuiltinEffectiveStatement.UINT8;
-                break;
-            case UINT16:
-                typeStmt = BuiltinEffectiveStatement.UINT16;
-                break;
-            case UINT32:
-                typeStmt = BuiltinEffectiveStatement.UINT32;
-                break;
-            case UINT64:
-                typeStmt = BuiltinEffectiveStatement.UINT64;
-                break;
-            default:
-                final QName qname = StmtContextUtils.parseNodeIdentifier(ctx, ctx.getStatementArgument());
-                final StmtContext<?, TypedefStatement, TypedefEffectiveStatement> typedef =
-                        SourceException.throwIfNull(ctx.getFromNamespace(TypeNamespace.class, qname),
-                            ctx.getStatementSourceReference(), "Type '%s' not found", qname);
-                typeStmt = typedef.buildEffective().asTypeEffectiveStatement();
-        }
+        final TypeEffectiveStatement<TypeStatement> typeStmt = resolveType(ctx);
 
         if (ctx.declaredSubstatements().isEmpty() && ctx.effectiveSubstatements().isEmpty()) {
             return typeStmt;
@@ -314,4 +268,49 @@ abstract class AbstractTypeStatementSupport
         final QName qname = path.getLastComponent().bindTo(parentQName.getModule()).intern();
         return parent.createChild(qname);
     }
+
+    /**
+     * Resolve type reference, as pointed to by the context's argument.
+     *
+     * @param ctx Statement context
+     * @return Resolved type
+     * @throws SourceException if the target type cannot be found
+     */
+    private static @NonNull TypeEffectiveStatement<TypeStatement> resolveType(final StmtContext<String, ?, ?> ctx) {
+        final String argument = ctx.coerceStatementArgument();
+        switch (argument) {
+            case BINARY:
+                return BuiltinEffectiveStatement.BINARY;
+            case BOOLEAN:
+                return BuiltinEffectiveStatement.BOOLEAN;
+            case EMPTY:
+                return BuiltinEffectiveStatement.EMPTY;
+            case INSTANCE_IDENTIFIER:
+                return BuiltinEffectiveStatement.INSTANCE_IDENTIFIER;
+            case INT8:
+                return BuiltinEffectiveStatement.INT8;
+            case INT16:
+                return BuiltinEffectiveStatement.INT16;
+            case INT32:
+                return BuiltinEffectiveStatement.INT32;
+            case INT64:
+                return BuiltinEffectiveStatement.INT64;
+            case STRING:
+                return BuiltinEffectiveStatement.STRING;
+            case UINT8:
+                return BuiltinEffectiveStatement.UINT8;
+            case UINT16:
+                return BuiltinEffectiveStatement.UINT16;
+            case UINT32:
+                return BuiltinEffectiveStatement.UINT32;
+            case UINT64:
+                return BuiltinEffectiveStatement.UINT64;
+            default:
+                final QName qname = StmtContextUtils.parseNodeIdentifier(ctx, argument);
+                final StmtContext<?, TypedefStatement, TypedefEffectiveStatement> typedef =
+                        SourceException.throwIfNull(ctx.getFromNamespace(TypeNamespace.class, qname),
+                            ctx.getStatementSourceReference(), "Type '%s' not found", qname);
+                return typedef.buildEffective().asTypeEffectiveStatement();
+        }
+    }
 }
\ No newline at end of file