Use TypedSchemaNode 21/53321/4
authorRobert Varga <rovarga@cisco.com>
Wed, 15 Mar 2017 09:44:02 +0000 (10:44 +0100)
committerRobert Varga <rovarga@cisco.com>
Fri, 17 Mar 2017 09:31:02 +0000 (10:31 +0100)
Eliminate special cases for leafs and leaf-lists, as they now
share a common interface.

Change-Id: I0c2e6a3875e6934e94af6552a14546e19ac822dd
Signed-off-by: Robert Varga <rovarga@cisco.com>
yang/yang-data-codec-gson/src/main/java/org/opendaylight/yangtools/yang/data/codec/gson/JSONCodecFactory.java
yang/yang-data-codec-xml/src/main/java/org/opendaylight/yangtools/yang/data/codec/xml/XmlCodecFactory.java

index 53998d82d78bb10d60fb6715d805b4d9a2e23037..1962556feac5ba6a75ba7fe1a1713cf8cd09603a 100644 (file)
@@ -19,10 +19,9 @@ import javax.annotation.Nonnull;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.data.impl.codec.TypeDefinitionAwareCodec;
 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
+import org.opendaylight.yangtools.yang.model.api.TypedSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.type.EmptyTypeDefinition;
 import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
 import org.opendaylight.yangtools.yang.model.api.type.InstanceIdentifierTypeDefinition;
@@ -57,19 +56,12 @@ public final class JSONCodecFactory {
         }
     };
 
-    private final LoadingCache<DataSchemaNode, JSONCodec<?>> codecs =
-            CacheBuilder.newBuilder().softValues().build(new CacheLoader<DataSchemaNode, JSONCodec<?>>() {
+    private final LoadingCache<TypedSchemaNode, JSONCodec<?>> codecs = CacheBuilder.newBuilder().softValues()
+            .build(new CacheLoader<TypedSchemaNode, JSONCodec<?>>() {
         @Override
-        public JSONCodec<?> load(@Nonnull final DataSchemaNode key) throws Exception {
-            final TypeDefinition<?> type;
-            if (key instanceof LeafSchemaNode) {
-                type = ((LeafSchemaNode) key).getType();
-            } else if (key instanceof LeafListSchemaNode) {
-                type = ((LeafListSchemaNode) key).getType();
-            } else {
-                throw new IllegalArgumentException("Not supported node type " + key.getClass().getName());
-            }
-            return createCodec(key,type);
+        public JSONCodec<?> load(@Nonnull final TypedSchemaNode key) throws Exception {
+            final TypeDefinition<?> type = key.getType();
+            return createCodec(key, type);
         }
     });
 
@@ -144,7 +136,8 @@ public final class JSONCodecFactory {
     }
 
     JSONCodec<?> codecFor(final DataSchemaNode schema) {
-        return codecs.getUnchecked(schema);
+        Preconditions.checkArgument(schema instanceof TypedSchemaNode, "Unsupported node type %s", schema.getClass());
+        return codecs.getUnchecked((TypedSchemaNode) schema);
     }
 
     JSONCodec<?> codecFor(final DataSchemaNode schema, final TypeDefinition<?> unionSubType) {
index 79e0d881a4f0eaf8af10440c6144eebf18ad86a2..726f013912bab857120cf4904ab4d3923bf34c54 100644 (file)
@@ -25,10 +25,9 @@ import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.impl.codec.TypeDefinitionAwareCodec;
 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
+import org.opendaylight.yangtools.yang.model.api.TypedSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.type.EmptyTypeDefinition;
 import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
 import org.opendaylight.yangtools.yang.model.api.type.InstanceIdentifierTypeDefinition;
@@ -62,22 +61,13 @@ public final class XmlCodecFactory {
         }
     };
 
-    private final LoadingCache<Entry<DataSchemaNode, NamespaceContext>, XmlCodec<?>> codecs =
-            CacheBuilder.newBuilder().softValues().build(
-                    new CacheLoader<Entry<DataSchemaNode, NamespaceContext>, XmlCodec<?>>() {
+    private final LoadingCache<Entry<TypedSchemaNode, NamespaceContext>, XmlCodec<?>> codecs = CacheBuilder.newBuilder()
+            .softValues().build(new CacheLoader<Entry<TypedSchemaNode, NamespaceContext>, XmlCodec<?>>() {
                 @Override
-                public XmlCodec<?> load(@Nonnull final Entry<DataSchemaNode, NamespaceContext> schemaNodeAndNamespaceCtxPair)
-                        throws Exception {
-                    final DataSchemaNode schemaNode = schemaNodeAndNamespaceCtxPair.getKey();
-                    final TypeDefinition<?> type;
-                    if (schemaNode instanceof LeafSchemaNode) {
-                        type = ((LeafSchemaNode) schemaNode).getType();
-                    } else if (schemaNode instanceof LeafListSchemaNode) {
-                        type = ((LeafListSchemaNode) schemaNode).getType();
-                    } else {
-                        throw new IllegalArgumentException("Not supported node type " + schemaNode.getClass().getName());
-                    }
-                    return createCodec(schemaNode,type, schemaNodeAndNamespaceCtxPair.getValue());
+                public XmlCodec<?> load(@Nonnull final Entry<TypedSchemaNode, NamespaceContext> pair) {
+                    final TypedSchemaNode schemaNode = pair.getKey();
+                    final TypeDefinition<?> type = schemaNode.getType();
+                    return createCodec(schemaNode, type, pair.getValue());
                 }
             });
 
@@ -156,7 +146,8 @@ public final class XmlCodecFactory {
     }
 
     XmlCodec<?> codecFor(final DataSchemaNode schema, final NamespaceContext namespaceContext) {
-        return codecs.getUnchecked(new SimpleImmutableEntry<>(schema, namespaceContext));
+        Preconditions.checkArgument(schema instanceof TypedSchemaNode, "Unsupported node type %s", schema.getClass());
+        return codecs.getUnchecked(new SimpleImmutableEntry<>((TypedSchemaNode)schema, namespaceContext));
     }
 
     XmlCodec<?> codecFor(final DataSchemaNode schema, final TypeDefinition<?> unionSubType,