BUG-5280: optimize identifier serialization format
[controller.git] / opendaylight / md-sal / cds-access-api / src / main / java / org / opendaylight / controller / cluster / access / concepts / FrontendIdentifier.java
index bd16e4b28ec67c611eb573ac3205a18e126b8f66..54ed314b66504995604898b03b5dbde22d7e30ea 100644 (file)
@@ -10,6 +10,8 @@ package org.opendaylight.controller.cluster.access.concepts;
 import com.google.common.annotations.Beta;
 import com.google.common.base.MoreObjects;
 import com.google.common.base.Preconditions;
+import java.io.DataInput;
+import java.io.DataOutput;
 import java.io.Externalizable;
 import java.io.IOException;
 import java.io.ObjectInput;
@@ -23,53 +25,64 @@ import org.opendaylight.yangtools.concepts.Identifier;
  * @author Robert Varga
  */
 @Beta
-public final class FrontendIdentifier<T extends FrontendType> implements Identifier {
-    private static final class Proxy<T extends FrontendType> implements Externalizable {
+public final class FrontendIdentifier implements Identifier, WritableObject {
+    private static final class Proxy implements Externalizable {
         private static final long serialVersionUID = 1L;
         private MemberName memberName;
-        private T clientType;
+        private FrontendType clientType;
 
         public Proxy() {
             // Needed for Externalizable
         }
 
-        Proxy(final MemberName memberName, final T clientType) {
+        Proxy(final MemberName memberName, final FrontendType clientType) {
             this.memberName = Preconditions.checkNotNull(memberName);
             this.clientType = Preconditions.checkNotNull(clientType);
         }
 
         @Override
-        public void writeExternal(ObjectOutput out) throws IOException {
-            out.writeObject(memberName);
-            out.writeObject(clientType);
+        public void writeExternal(final ObjectOutput out) throws IOException {
+            memberName.writeTo(out);
+            clientType.writeTo(out);
         }
 
-        @SuppressWarnings("unchecked")
         @Override
-        public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
-            memberName = (MemberName) in.readObject();
-            clientType = (T) in.readObject();
+        public void readExternal(final ObjectInput in) throws IOException {
+            memberName = MemberName.readFrom(in);
+            clientType = FrontendType.readFrom(in);
         }
 
         private Object readResolve() {
-            return new FrontendIdentifier<>(memberName, clientType);
+            return new FrontendIdentifier(memberName, clientType);
         }
     }
 
     private static final long serialVersionUID = 1L;
     private final MemberName memberName;
-    private final T clientType;
+    private final FrontendType clientType;
 
-    FrontendIdentifier(final MemberName memberName, final T clientType) {
+    FrontendIdentifier(final MemberName memberName, final FrontendType clientType) {
         this.clientType = Preconditions.checkNotNull(clientType);
         this.memberName = Preconditions.checkNotNull(memberName);
     }
 
-    public static <T extends FrontendType> FrontendIdentifier<T> create(MemberName memberName, final T clientType) {
-        return new FrontendIdentifier<>(memberName, clientType);
+    public static FrontendIdentifier create(final MemberName memberName, final FrontendType clientType) {
+        return new FrontendIdentifier(memberName, clientType);
     }
 
-    public T getClientType() {
+    public static FrontendIdentifier readFrom(final DataInput in) throws IOException {
+        final MemberName memberName = MemberName.readFrom(in);
+        final FrontendType clientType = FrontendType.readFrom(in);
+        return new FrontendIdentifier(memberName, clientType);
+    }
+
+    @Override
+    public void writeTo(final DataOutput out) throws IOException {
+        memberName.writeTo(out);
+        clientType.writeTo(out);
+    }
+
+    public FrontendType getClientType() {
         return clientType;
     }
 
@@ -91,7 +104,7 @@ public final class FrontendIdentifier<T extends FrontendType> implements Identif
             return false;
         }
 
-        final FrontendIdentifier<?> other = (FrontendIdentifier<?>) o;
+        final FrontendIdentifier other = (FrontendIdentifier) o;
         return memberName.equals(other.memberName) && clientType.equals(other.clientType);
     }
 
@@ -102,6 +115,6 @@ public final class FrontendIdentifier<T extends FrontendType> implements Identif
     }
 
     private Object writeReplace() {
-        return new Proxy<>(memberName, clientType);
+        return new Proxy(memberName, clientType);
     }
 }