BUG-5280: optimize identifier serialization format
[controller.git] / opendaylight / md-sal / cds-access-api / src / main / java / org / opendaylight / controller / cluster / access / concepts / LocalHistoryIdentifier.java
index 330362e6aeccf11df77e798d1210c0d30f8c3fa2..4eb26270c4e078b099f32b9f0d2e721e5e03e810 100644 (file)
@@ -9,6 +9,8 @@ package org.opendaylight.controller.cluster.access.concepts;
 
 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;
@@ -18,53 +20,61 @@ import org.opendaylight.yangtools.concepts.Identifier;
 /**
  * Globally-unique identifier of a local history.
  *
- * @param <T> Frontend type
- *
  * @author Robert Varga
  */
-public final class LocalHistoryIdentifier<T extends FrontendType> implements Identifier {
-    private static final class Proxy<T extends FrontendType> implements Externalizable {
+public final class LocalHistoryIdentifier implements Identifier, WritableObject {
+    private static final class Proxy implements Externalizable {
         private static final long serialVersionUID = 1L;
-        private ClientIdentifier<T> clientId;
+        private ClientIdentifier clientId;
         private long historyId;
 
         public Proxy() {
             // For Externalizable
         }
 
-        Proxy(final ClientIdentifier<T> frontendId, final long historyId) {
+        Proxy(final ClientIdentifier frontendId, final long historyId) {
             this.clientId = Preconditions.checkNotNull(frontendId);
             this.historyId = historyId;
         }
 
         @Override
         public void writeExternal(final ObjectOutput out) throws IOException {
-            out.writeObject(clientId);
-            out.writeLong(historyId);
+            clientId.writeTo(out);
+            WritableObjects.writeLong(out, historyId);
         }
 
-        @SuppressWarnings("unchecked")
         @Override
         public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
-            clientId = (ClientIdentifier<T>) in.readObject();
-            historyId = in.readLong();
+            clientId = ClientIdentifier.readFrom(in);
+            historyId = WritableObjects.readLong(in);
         }
 
         private Object readResolve() {
-            return new LocalHistoryIdentifier<>(clientId, historyId);
+            return new LocalHistoryIdentifier(clientId, historyId);
         }
     }
 
     private static final long serialVersionUID = 1L;
-    private final ClientIdentifier<T> clientId;
+    private final ClientIdentifier clientId;
     private final long historyId;
 
-    public LocalHistoryIdentifier(final ClientIdentifier<T> frontendId, final long historyId) {
+    public LocalHistoryIdentifier(final ClientIdentifier frontendId, final long historyId) {
         this.clientId = Preconditions.checkNotNull(frontendId);
         this.historyId = historyId;
     }
 
-    public ClientIdentifier<T> getClienId() {
+    public static LocalHistoryIdentifier readFrom(final DataInput in) throws IOException {
+        final ClientIdentifier clientId = ClientIdentifier.readFrom(in);
+        return new LocalHistoryIdentifier(clientId, WritableObjects.readLong(in));
+    }
+
+    @Override
+    public void writeTo(final DataOutput out) throws IOException {
+        clientId.writeTo(out);
+        WritableObjects.writeLong(out, historyId);
+    }
+
+    public ClientIdentifier getClientId() {
         return clientId;
     }
 
@@ -86,7 +96,7 @@ public final class LocalHistoryIdentifier<T extends FrontendType> implements Ide
             return false;
         }
 
-        final LocalHistoryIdentifier<?> other = (LocalHistoryIdentifier<?>) o;
+        final LocalHistoryIdentifier other = (LocalHistoryIdentifier) o;
         return historyId == other.historyId && clientId.equals(other.clientId);
     }
 
@@ -97,6 +107,6 @@ public final class LocalHistoryIdentifier<T extends FrontendType> implements Ide
     }
 
     private Object writeReplace() {
-        return new Proxy<>(clientId, historyId);
+        return new Proxy(clientId, historyId);
     }
 }