Merge codec contexts caches
[mdsal.git] / binding / mdsal-binding-dom-codec / src / main / java / org / opendaylight / mdsal / binding / dom / codec / impl / ValueContext.java
index bc621f1212400cc0285e0a971da5a9f7ddf028cc..c1dc0baa5df5db4dc0955eccc968be4eeb3571ed 100644 (file)
@@ -7,25 +7,27 @@
  */
 package org.opendaylight.mdsal.binding.dom.codec.impl;
 
-import com.google.common.base.Preconditions;
+import static com.google.common.base.Preconditions.checkArgument;
+import static com.google.common.base.Verify.verifyNotNull;
+
 import com.google.common.base.Throwables;
 import java.lang.invoke.MethodHandle;
 import java.lang.invoke.MethodHandles;
 import java.lang.invoke.MethodType;
-import org.opendaylight.yangtools.concepts.Codec;
+import org.opendaylight.yangtools.concepts.IllegalArgumentCodec;
 
 final class ValueContext {
     private static final MethodType OBJECT_METHOD = MethodType.methodType(Object.class, Object.class);
-    private final Codec<Object, Object> codec;
+    private final IllegalArgumentCodec<Object, Object> codec;
     private final MethodHandle getter;
     private final Class<?> identifier;
     private final String getterName;
 
-    ValueContext(final Class<?> identifier, final LeafNodeCodecContext<?> leaf) {
-        getterName = leaf.getGetter().getName();
+    ValueContext(final Class<?> identifier, final ValueNodeCodecContext leaf) {
+        getterName = leaf.getGetterName();
         try {
             getter = MethodHandles.publicLookup().unreflect(identifier.getMethod(getterName)).asType(OBJECT_METHOD);
-        } catch (IllegalAccessException | NoSuchMethodException | SecurityException e) {
+        } catch (IllegalAccessException | NoSuchMethodException e) {
             throw new IllegalStateException(String.format("Cannot find method %s in class %s", getterName, identifier),
                 e);
         }
@@ -43,14 +45,14 @@ final class ValueContext {
             throw new IllegalStateException(e);
         }
 
-        Preconditions.checkArgument(value != null,
-                "All keys must be specified for %s. Missing key is %s. Supplied key is %s",
+        checkArgument(value != null, "All keys must be specified for %s. Missing key is %s. Supplied key is %s",
                 identifier, getterName, obj);
         return codec.serialize(value);
     }
 
     Object deserialize(final Object obj) {
-        return codec.deserialize(obj);
+        checkArgument(obj != null, "Attempted to serialize null for %s component of %s", getterName, identifier);
+        return verifyNotNull(codec.deserialize(obj), "Codec for %s of %s returned null for %s", getterName, identifier,
+            obj);
     }
-
-}
\ No newline at end of file
+}