Propagate @Nonnull and @Nullable annotations
[yangtools.git] / yang / yang-data-codec-gson / src / main / java / org / opendaylight / yangtools / yang / data / codec / gson / JSONStringIdentityrefCodec.java
index 66d55237a567c813d31b1d77d7e3505731ac17b8..032553193bcb40d318750b0e203a240cbd5af6bf 100644 (file)
@@ -7,30 +7,38 @@
  */
 package org.opendaylight.yangtools.yang.data.codec.gson;
 
-import com.google.common.base.Preconditions;
-
-import java.net.URI;
-
-import org.opendaylight.yangtools.yang.data.util.AbstractModuleStringIdentityrefCodec;
+import com.google.gson.stream.JsonWriter;
+import java.io.IOException;
+import javax.annotation.Nonnull;
+import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.common.QNameModule;
+import org.opendaylight.yangtools.yang.data.util.ModuleStringIdentityrefCodec;
 import org.opendaylight.yangtools.yang.model.api.Module;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
 
-final class JSONStringIdentityrefCodec extends AbstractModuleStringIdentityrefCodec {
-    private final SchemaContext context;
-
-    JSONStringIdentityrefCodec(final SchemaContext context) {
-        this.context = Preconditions.checkNotNull(context);
+final class JSONStringIdentityrefCodec extends ModuleStringIdentityrefCodec implements JSONCodec<QName> {
+    JSONStringIdentityrefCodec(final SchemaContext context, final QNameModule parentModule) {
+        super(context, parentModule);
     }
 
     @Override
-    protected Module moduleForPrefix(final String prefix) {
-        return context.findModuleByName(prefix, null);
+    protected Module moduleForPrefix(@Nonnull final String prefix) {
+        if (prefix.isEmpty()) {
+            return context.findModuleByNamespaceAndRevision(parentModuleQname.getNamespace(),
+                    parentModuleQname.getRevision());
+        } else {
+            return context.findModuleByName(prefix, null);
+        }
     }
 
+    /**
+     * Serialize QName with specified JsonWriter.
+     *
+     * @param writer JsonWriter
+     * @param value QName
+     */
     @Override
-    protected String prefixForNamespace(final URI namespace) {
-        final Module module = context.findModuleByNamespaceAndRevision(namespace, null);
-        return module == null ? null : module.getName();
+    public void serializeToWriter(JsonWriter writer, QName value) throws IOException {
+        writer.value(serialize(value));
     }
-
 }