Use direct type checks
authorRobert Varga <rovarga@cisco.com>
Thu, 21 Jul 2016 09:11:42 +0000 (11:11 +0200)
committerAnil Belur <abelur@linuxfoundation.org>
Wed, 19 Jun 2024 00:41:20 +0000 (10:41 +1000)
With the removal of old parser and ExtendedType each type
definition implements proper subtype, hence we do not have
to traverse the type tree up to root.

Change-Id: Ib7a6ffa5f221278547e27e37b19c14a1a3c26d8a
Signed-off-by: Robert Varga <rovarga@cisco.com>
binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/yangtools/binding/data/codec/gen/impl/DataNodeContainerSerializerSource.java
binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/yangtools/binding/data/codec/impl/BindingCodecContext.java
binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/yangtools/binding/data/codec/impl/ValueTypeCodec.java

index 21917eff708d0a84125dfa800d4605f1b5773e75..bc220ff445da95ac93be6eecc2af77ec9166801a 100644 (file)
@@ -109,20 +109,16 @@ abstract class DataNodeContainerSerializerSource extends DataObjectSerializerSou
         final TypeDefinition<?> type ;
         if (node instanceof LeafSchemaNode) {
             type = ((LeafSchemaNode) node).getType();
-        } else if(node instanceof LeafListSchemaNode) {
+        } else if (node instanceof LeafListSchemaNode) {
             type = ((LeafListSchemaNode) node).getType();
         } else {
             type = null;
         }
-        String prefix = "get";
-        if(type != null) {
-            TypeDefinition<?> rootType = type;
-            while (rootType.getBaseType() != null) {
-                rootType = rootType.getBaseType();
-            }
-            if(rootType instanceof BooleanTypeDefinition || rootType instanceof EmptyTypeDefinition) {
-                prefix = "is";
-            }
+        final String prefix;
+        if (type instanceof BooleanTypeDefinition || type instanceof EmptyTypeDefinition) {
+            prefix = "is";
+        } else {
+            prefix = "get";
         }
         return prefix + BindingMapping.getGetterSuffix(node.getQName());
     }
index 3aac56f4c281bb62b9ae03637bf0c221639a6a25..dbf066d44d9dee539658aec91901d03ae452b14e 100644 (file)
@@ -236,11 +236,8 @@ final class BindingCodecContext implements CodecContextFactory, BindingCodecTree
         return getLeafNodesUsingReflection(parentClass, getterToLeafSchema);
     }
 
-    private static String getGetterName(final QName qName, TypeDefinition<?> typeDef) {
+    private static String getGetterName(final QName qName, final TypeDefinition<?> typeDef) {
         final String suffix = BindingMapping.getGetterSuffix(qName);
-        while (typeDef.getBaseType() != null) {
-            typeDef = typeDef.getBaseType();
-        }
         if (typeDef instanceof BooleanTypeDefinition || typeDef instanceof EmptyTypeDefinition) {
             return "is" + suffix;
         }
@@ -309,30 +306,25 @@ final class BindingCodecContext implements CodecContextFactory, BindingCodecTree
         return ValueTypeCodec.NOOP_CODEC;
     }
 
-    private Codec<Object, Object> getCodecForBindingClass(final Class<?> valueType, final TypeDefinition<?> instantiatedType) {
-        @SuppressWarnings("rawtypes")
-        TypeDefinition rootType = instantiatedType;
-        while (rootType.getBaseType() != null) {
-            rootType = rootType.getBaseType();
-        }
-        if (rootType instanceof IdentityrefTypeDefinition) {
+    private Codec<Object, Object> getCodecForBindingClass(final Class<?> valueType, final TypeDefinition<?> typeDef) {
+        if (typeDef instanceof IdentityrefTypeDefinition) {
             return ValueTypeCodec.encapsulatedValueCodecFor(valueType, identityCodec);
-        } else if (rootType instanceof InstanceIdentifierTypeDefinition) {
+        } else if (typeDef instanceof InstanceIdentifierTypeDefinition) {
             return ValueTypeCodec.encapsulatedValueCodecFor(valueType, instanceIdentifierCodec);
-        } else if (rootType instanceof UnionTypeDefinition) {
-            final Callable<UnionTypeCodec> loader = UnionTypeCodec.loader(valueType, (UnionTypeDefinition) rootType, this);
+        } else if (typeDef instanceof UnionTypeDefinition) {
+            final Callable<UnionTypeCodec> loader = UnionTypeCodec.loader(valueType, (UnionTypeDefinition) typeDef, this);
             try {
                 return loader.call();
             } catch (final Exception e) {
                 throw new IllegalStateException("Unable to load codec for " + valueType, e);
             }
-        } else if (rootType instanceof LeafrefTypeDefinition) {
+        } else if (typeDef instanceof LeafrefTypeDefinition) {
             final Entry<GeneratedType, Object> typeWithSchema = context.getTypeWithSchema(valueType);
             final Object schema = typeWithSchema.getValue();
             Preconditions.checkState(schema instanceof TypeDefinition<?>);
             return getCodec(valueType, (TypeDefinition<?>) schema);
         }
-        return ValueTypeCodec.getCodecFor(valueType, instantiatedType);
+        return ValueTypeCodec.getCodecFor(valueType, typeDef);
     }
 
     @Override
index d69970b93fad9af9fc3ce9e7545b31da730c5275..c2b12413e326fed2643aa3bb778bccc1d82cb94b 100644 (file)
@@ -93,14 +93,7 @@ abstract class ValueTypeCodec implements Codec<Object, Object> {
         if (BindingReflections.isBindingClass(typeClz)) {
             return getCachedSchemaUnawareCodec(typeClz, getCodecLoader(typeClz, def));
         }
-        TypeDefinition<?> rootType = def;
-        while (rootType.getBaseType() != null) {
-            rootType = rootType.getBaseType();
-        }
-        if (rootType instanceof EmptyTypeDefinition) {
-            return EMPTY_CODEC;
-        }
-        return NOOP_CODEC;
+        return def instanceof EmptyTypeDefinition ? EMPTY_CODEC : NOOP_CODEC;
     }
 
     private static SchemaUnawareCodec getCachedSchemaUnawareCodec(final Class<?> typeClz, final Callable<? extends SchemaUnawareCodec> loader) {