Split STATIC_CODECS into per-type caches
[mdsal.git] / binding / mdsal-binding-dom-codec / src / main / java / org / opendaylight / mdsal / binding / dom / codec / impl / ValueTypeCodec.java
index 2b8b69d08ece0d2c1a2d6f7469516b30fa006d7f..253fffae4041fbd9dde50001f69ce8be55f6b880 100644 (file)
@@ -7,40 +7,34 @@
  */
 package org.opendaylight.mdsal.binding.dom.codec.impl;
 
-import com.google.common.cache.Cache;
-import com.google.common.cache.CacheBuilder;
-import java.util.concurrent.Callable;
+import static java.util.Objects.requireNonNull;
+
 import java.util.concurrent.ExecutionException;
 import org.opendaylight.mdsal.binding.spec.reflect.BindingReflections;
-import org.opendaylight.yangtools.concepts.Codec;
-import org.opendaylight.yangtools.yang.common.Empty;
+import org.opendaylight.yangtools.concepts.IllegalArgumentCodec;
 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
-import org.opendaylight.yangtools.yang.model.api.type.EmptyTypeDefinition;
 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
 
 /**
  * Value codec, which serializes / deserializes values from DOM simple values.
  */
-abstract class ValueTypeCodec implements Codec<Object, Object> {
-
-    private static final Cache<Class<?>, SchemaUnawareCodec> STATIC_CODECS = CacheBuilder.newBuilder().weakKeys()
-            .build();
-
+// FIXME: IllegalArgumentCodec is perhaps not appropriate here due to null behavior
+abstract class ValueTypeCodec implements IllegalArgumentCodec<Object, Object> {
     /**
      * Marker interface for codecs, which functionality will not be affected by schema change (introduction of new YANG
      * modules) they may have one static instance generated when first time needed.
      */
-    interface SchemaUnawareCodec extends Codec<Object,Object> {
+    // FIXME: IllegalArgumentCodec is perhaps not appropriate here due to null behavior
+    interface SchemaUnawareCodec extends IllegalArgumentCodec<Object, Object> {
 
     }
 
     /**
      * No-op Codec, Java YANG Binding uses same types as NormalizedNode model for base YANG types, representing numbers,
-     * binary and strings.
+     * binary, strings and empty.
      */
     public static final SchemaUnawareCodec NOOP_CODEC = new SchemaUnawareCodec() {
-
         @Override
         public Object serialize(final Object input) {
             return input;
@@ -52,63 +46,41 @@ abstract class ValueTypeCodec implements Codec<Object, Object> {
         }
     };
 
-    public static final SchemaUnawareCodec EMPTY_CODEC = new SchemaUnawareCodec() {
-
-        @Override
-        public Object serialize(final Object input) {
-            // Empty type has Empty value in NormalizedNode and Composite Node representation
-            return Empty.getInstance();
-        }
-
-        @Override
-        public Object deserialize(final Object input) {
-            /* Empty type has boolean.TRUE representation in Binding-aware world otherwise it is null / false.
-             * So when codec is triggered, empty leaf is present, that means we are safe to return true.
-             */
-            return Boolean.TRUE;
-        }
-    };
-
-    private static final Callable<? extends SchemaUnawareCodec> EMPTY_LOADER = () -> EMPTY_CODEC;
-
-
     public static SchemaUnawareCodec getCodecFor(final Class<?> typeClz, final TypeDefinition<?> def) {
         if (BindingReflections.isBindingClass(typeClz)) {
-            return getCachedSchemaUnawareCodec(typeClz, getCodecLoader(typeClz, def));
+            return getCachedSchemaUnawareCodec(typeClz, def);
         }
-        return def instanceof EmptyTypeDefinition ? EMPTY_CODEC : NOOP_CODEC;
+        return NOOP_CODEC;
     }
 
-    private static SchemaUnawareCodec getCachedSchemaUnawareCodec(final Class<?> typeClz,
-            final Callable<? extends SchemaUnawareCodec> loader) {
+    private static SchemaUnawareCodec getCachedSchemaUnawareCodec(final Class<?> typeClz, final TypeDefinition<?> def) {
+        // FIXME: extract this only when really needed
+        var rootType = requireNonNull(def);
+        while (true) {
+            final var base = rootType.getBaseType();
+            if (base != null) {
+                rootType = base;
+            } else {
+                break;
+            }
+        }
+
         try {
-            return STATIC_CODECS.get(typeClz, loader);
+            if (rootType instanceof EnumTypeDefinition) {
+                return EnumerationCodec.of(typeClz, (EnumTypeDefinition) rootType);
+            } else if (rootType instanceof BitsTypeDefinition) {
+                return BitsCodec.of(typeClz, (BitsTypeDefinition) rootType);
+            } else {
+                return EncapsulatedValueCodec.of(typeClz, def);
+            }
         } catch (ExecutionException e) {
             throw new IllegalStateException(e);
         }
     }
 
-    private static Callable<? extends SchemaUnawareCodec> getCodecLoader(final Class<?> typeClz,
-            final TypeDefinition<?> def) {
-
-        TypeDefinition<?> rootType = def;
-        while (rootType.getBaseType() != null) {
-            rootType = rootType.getBaseType();
-        }
-        if (rootType instanceof EnumTypeDefinition) {
-            return EnumerationCodec.loader(typeClz, (EnumTypeDefinition) rootType);
-        } else if (rootType instanceof BitsTypeDefinition) {
-            return BitsCodec.loader(typeClz, (BitsTypeDefinition) rootType);
-        }
-        return EncapsulatedValueCodec.loader(typeClz, def);
-    }
-
     @SuppressWarnings("rawtypes")
     static ValueTypeCodec encapsulatedValueCodecFor(final Class<?> typeClz, final TypeDefinition<?> typeDef,
-             final Codec delegate) {
-        SchemaUnawareCodec extractor = getCachedSchemaUnawareCodec(typeClz,
-            EncapsulatedValueCodec.loader(typeClz, typeDef));
-        return new CompositeValueCodec(extractor, delegate);
+             final IllegalArgumentCodec delegate) {
+        return new CompositeValueCodec(getCachedSchemaUnawareCodec(typeClz, typeDef), delegate);
     }
-
 }