Do not box result twice
[mdsal.git] / binding / mdsal-binding-dom-codec / src / main / java / org / opendaylight / mdsal / binding / dom / codec / impl / LeafNodeCodecContext.java
index c075713c8a5551541c72583526f59d9c3e9d7db1..385c6095f0e1b8e2b40afdd1ebad887909d56af8 100644 (file)
@@ -7,15 +7,16 @@
  */
 package org.opendaylight.mdsal.binding.dom.codec.impl;
 
-import com.google.common.base.Optional;
-import com.google.common.base.Preconditions;
+import static com.google.common.base.Preconditions.checkArgument;
+import static java.util.Objects.requireNonNull;
+
 import com.google.common.collect.ImmutableCollection;
 import java.lang.reflect.Method;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.List;
+import java.util.Optional;
 import java.util.Set;
-import javax.annotation.Nullable;
 import org.opendaylight.mdsal.binding.dom.codec.api.BindingCodecTreeNode;
 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeCachingCodec;
 import org.opendaylight.yangtools.concepts.Codec;
@@ -49,9 +50,9 @@ final class LeafNodeCodecContext<D extends DataObject> extends NodeCodecContext<
     LeafNodeCodecContext(final DataSchemaNode schema, final Codec<Object, Object> codec, final Method getter,
                 final SchemaContext schemaContext) {
         this.yangIdentifier = new YangInstanceIdentifier.NodeIdentifier(schema.getQName());
-        this.valueCodec = Preconditions.checkNotNull(codec);
+        this.valueCodec = requireNonNull(codec);
         this.getter = getter;
-        this.schema = Preconditions.checkNotNull(schema);
+        this.schema = requireNonNull(schema);
 
         this.defaultObject = createDefaultObject(schema, valueCodec, schemaContext);
     }
@@ -59,23 +60,23 @@ final class LeafNodeCodecContext<D extends DataObject> extends NodeCodecContext<
     private static Object createDefaultObject(final DataSchemaNode schema, final Codec<Object, Object> codec,
                                               final SchemaContext schemaContext) {
         if (schema instanceof LeafSchemaNode) {
-            Object defaultValue = ((LeafSchemaNode) schema).getDefault();
+            Optional<? extends Object> defaultValue = ((LeafSchemaNode) schema).getType().getDefaultValue();
             TypeDefinition<?> type = ((LeafSchemaNode) schema).getType();
-            if (defaultValue != null) {
+            if (defaultValue.isPresent()) {
                 if (type instanceof IdentityrefTypeDefinition) {
-                    return qnameDomValueFromString(codec, schema, (String) defaultValue, schemaContext);
+                    return qnameDomValueFromString(codec, schema, (String) defaultValue.get(), schemaContext);
                 }
-                return domValueFromString(codec, type, defaultValue);
+                return domValueFromString(codec, type, defaultValue.get());
             }
 
-            while (type.getBaseType() != null && type.getDefaultValue() == null) {
+            while (type.getBaseType() != null && !type.getDefaultValue().isPresent()) {
                 type = type.getBaseType();
             }
 
             defaultValue = type.getDefaultValue();
-            if (defaultValue != null) {
+            if (defaultValue.isPresent()) {
                 if (type instanceof IdentityrefTypeDefinition) {
-                    return qnameDomValueFromString(codec, schema, (String) defaultValue, schemaContext);
+                    return qnameDomValueFromString(codec, schema, (String) defaultValue.get(), schemaContext);
                 }
                 return domValueFromString(codec, type, defaultValue);
             }
@@ -90,9 +91,7 @@ final class LeafNodeCodecContext<D extends DataObject> extends NodeCodecContext<
         if (prefixEndIndex != -1) {
             String defaultValuePrefix = defaultValue.substring(0, prefixEndIndex);
 
-            Module module = schemaContext.findModuleByNamespaceAndRevision(schema.getQName().getNamespace(),
-                    schema.getQName().getRevision());
-
+            Module module = schemaContext.findModule(schema.getQName().getModule()).get();
             if (module.getPrefix().equals(defaultValuePrefix)) {
                 qname = QName.create(module.getQNameModule(), defaultValue.substring(prefixEndIndex + 1));
                 return codec.deserialize(qname);
@@ -101,8 +100,8 @@ final class LeafNodeCodecContext<D extends DataObject> extends NodeCodecContext<
             Set<ModuleImport> imports = module.getImports();
             for (ModuleImport moduleImport : imports) {
                 if (moduleImport.getPrefix().equals(defaultValuePrefix)) {
-                    Module importedModule = schemaContext.findModuleByName(moduleImport.getModuleName(),
-                        moduleImport.getRevision());
+                    Module importedModule = schemaContext.findModule(moduleImport.getModuleName(),
+                        moduleImport.getRevision()).get();
                     qname = QName.create(importedModule.getQNameModule(), defaultValue.substring(prefixEndIndex + 1));
                     return codec.deserialize(qname);
                 }
@@ -211,7 +210,7 @@ final class LeafNodeCodecContext<D extends DataObject> extends NodeCodecContext<
 
     @Override
     public InstanceIdentifier.PathArgument deserializePathArgument(final YangInstanceIdentifier.PathArgument arg) {
-        Preconditions.checkArgument(getDomPathArgument().equals(arg));
+        checkArgument(getDomPathArgument().equals(arg));
         return null;
     }
 
@@ -225,12 +224,13 @@ final class LeafNodeCodecContext<D extends DataObject> extends NodeCodecContext<
         return schema;
     }
 
-    /**
-     * Return the default value object.
-     *
-     * @return The default value object, or null if the default value is not defined.
-     */
-    @Nullable Object defaultObject() {
+    @Override
+    public ChildAddressabilitySummary getChildAddressabilitySummary() {
+        return ChildAddressabilitySummary.UNADDRESSABLE;
+    }
+
+    @Override
+    Object defaultObject() {
         return defaultObject;
     }
-}
\ No newline at end of file
+}