Make QNameModule use externalizable proxy pattern 61/81061/2
authorRobert Varga <robert.varga@pantheon.tech>
Thu, 21 Mar 2019 09:00:07 +0000 (10:00 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Thu, 21 Mar 2019 09:18:49 +0000 (10:18 +0100)
This disconnects the serialized form from the object itself,
allowing the two of them to evolve independently.

Change-Id: I5688c2c930282307f2cf5bf9ebd23295fb314c63
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
yang/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/NSv1.java [new file with mode: 0644]
yang/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/QNameModule.java

diff --git a/yang/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/NSv1.java b/yang/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/NSv1.java
new file mode 100644 (file)
index 0000000..99a7e80
--- /dev/null
@@ -0,0 +1,48 @@
+/*
+ * Copyright (c) 2019 PANTHEON.tech, s.r.o. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.yangtools.yang.common;
+
+import static com.google.common.base.Verify.verifyNotNull;
+import static java.util.Objects.requireNonNull;
+
+import java.io.Externalizable;
+import java.io.IOException;
+import java.io.ObjectInput;
+import java.io.ObjectOutput;
+
+/**
+ * Externalizable proxy for {@link QNameModule}.
+ */
+final class NSv1 implements Externalizable {
+    private static final long serialVersionUID = 1L;
+
+    private QNameModule namespace;
+
+    @SuppressWarnings("checkstyle:redundantModifier")
+    public NSv1() {
+        // For Externalizable
+    }
+
+    NSv1(final QNameModule qname) {
+        this.namespace = requireNonNull(qname);
+    }
+
+    @Override
+    public void writeExternal(final ObjectOutput out) throws IOException {
+        namespace.writeTo(out);
+    }
+
+    @Override
+    public void readExternal(final ObjectInput in) throws IOException {
+        namespace = QNameModule.readFrom(in);
+    }
+
+    Object readResolve() {
+        return verifyNotNull(namespace);
+    }
+}
index c18687c3848d65b911f5c99b3618f56ad70c2719..5da98d990e356e46df2e318eecbae50fee7e77c0 100644 (file)
@@ -182,4 +182,8 @@ public final class QNameModule implements Comparable<QNameModule>, Immutable, Se
         return MoreObjects.toStringHelper(QNameModule.class).omitNullValues().add("ns", namespace)
             .add("rev", revision).toString();
     }
+
+    Object writeReplace() {
+        return new NSv1(this);
+    }
 }