BUG-5280: optimize identifier serialization format
[controller.git] / opendaylight / md-sal / cds-access-api / src / main / java / org / opendaylight / controller / cluster / access / concepts / TransactionIdentifier.java
index 2aaf8264aacadf2084f176b9d64b536d7cc76aae..7d058ef9ffbb328c32f618399f75778ad3d4aad4 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;
@@ -20,54 +22,62 @@ import org.opendaylight.yangtools.concepts.Identifier;
 /**
  * Globally-unique identifier of a transaction.
  *
- * @param <T> Frontend type
- *
  * @author Robert Varga
  */
 @Beta
-public final class TransactionIdentifier<T extends FrontendType> implements Identifier {
-    private static final class Proxy<T extends FrontendType> implements Externalizable {
+public final class TransactionIdentifier implements Identifier, WritableObject {
+    private static final class Proxy implements Externalizable {
         private static final long serialVersionUID = 1L;
-        private LocalHistoryIdentifier<T> historyId;
+        private LocalHistoryIdentifier historyId;
         private long transactionId;
 
         public Proxy() {
             // For Externalizable
         }
 
-        Proxy(final LocalHistoryIdentifier<T> historyId, final long transactionId) {
+        Proxy(final LocalHistoryIdentifier historyId, final long transactionId) {
             this.historyId = Preconditions.checkNotNull(historyId);
             this.transactionId = transactionId;
         }
 
         @Override
         public void writeExternal(final ObjectOutput out) throws IOException {
-            out.writeObject(historyId);
-            out.writeLong(transactionId);
+            historyId.writeTo(out);
+            WritableObjects.writeLong(out, transactionId);
         }
 
-        @SuppressWarnings("unchecked")
         @Override
-        public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
-            historyId = (LocalHistoryIdentifier<T>) in.readObject();
-            transactionId = in.readLong();
+        public void readExternal(final ObjectInput in) throws IOException {
+            historyId = LocalHistoryIdentifier.readFrom(in);
+            transactionId = WritableObjects.readLong(in);
         }
 
         private Object readResolve() {
-            return new TransactionIdentifier<>(historyId, transactionId);
+            return new TransactionIdentifier(historyId, transactionId);
         }
     }
 
     private static final long serialVersionUID = 1L;
-    private final LocalHistoryIdentifier<T> historyId;
+    private final LocalHistoryIdentifier historyId;
     private final long transactionId;
 
-    public TransactionIdentifier(final @Nonnull LocalHistoryIdentifier<T> historyId, final long transactionId) {
+    public TransactionIdentifier(final @Nonnull LocalHistoryIdentifier historyId, final long transactionId) {
         this.historyId = Preconditions.checkNotNull(historyId);
         this.transactionId = transactionId;
     }
 
-    public LocalHistoryIdentifier<T> getHistoryId() {
+    public static TransactionIdentifier readFrom(final DataInput in) throws IOException {
+        final LocalHistoryIdentifier historyId = LocalHistoryIdentifier.readFrom(in);
+        return new TransactionIdentifier(historyId, WritableObjects.readLong(in));
+    }
+
+    @Override
+    public void writeTo(final DataOutput out) throws IOException {
+        historyId.writeTo(out);
+        WritableObjects.writeLong(out, transactionId);
+    }
+
+    public LocalHistoryIdentifier getHistoryId() {
         return historyId;
     }
 
@@ -89,7 +99,7 @@ public final class TransactionIdentifier<T extends FrontendType> implements Iden
             return false;
         }
 
-        final TransactionIdentifier<?> other = (TransactionIdentifier<?>) o;
+        final TransactionIdentifier other = (TransactionIdentifier) o;
         return transactionId == other.transactionId && historyId.equals(other.historyId);
     }
 
@@ -100,6 +110,6 @@ public final class TransactionIdentifier<T extends FrontendType> implements Iden
     }
 
     private Object writeReplace() {
-        return new Proxy<>(historyId, transactionId);
+        return new Proxy(historyId, transactionId);
     }
 }