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
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.cluster.access.concepts;
9
10 import com.google.common.annotations.Beta;
11 import com.google.common.base.MoreObjects;
12 import com.google.common.base.Preconditions;
13 import java.io.DataInput;
14 import java.io.DataOutput;
15 import java.io.Externalizable;
16 import java.io.IOException;
17 import java.io.ObjectInput;
18 import java.io.ObjectOutput;
19 import javax.annotation.Nonnull;
20 import org.opendaylight.yangtools.concepts.Identifier;
21
22 /**
23  * Globally-unique identifier of a transaction.
24  *
25  * @author Robert Varga
26  */
27 @Beta
28 public final class TransactionIdentifier implements Identifier, WritableObject {
29     private static final class Proxy implements Externalizable {
30         private static final long serialVersionUID = 1L;
31         private LocalHistoryIdentifier historyId;
32         private long transactionId;
33
34         public Proxy() {
35             // For Externalizable
36         }
37
38         Proxy(final LocalHistoryIdentifier historyId, final long transactionId) {
39             this.historyId = Preconditions.checkNotNull(historyId);
40             this.transactionId = transactionId;
41         }
42
43         @Override
44         public void writeExternal(final ObjectOutput out) throws IOException {
45             historyId.writeTo(out);
46             WritableObjects.writeLong(out, transactionId);
47         }
48
49         @Override
50         public void readExternal(final ObjectInput in) throws IOException {
51             historyId = LocalHistoryIdentifier.readFrom(in);
52             transactionId = WritableObjects.readLong(in);
53         }
54
55         private Object readResolve() {
56             return new TransactionIdentifier(historyId, transactionId);
57         }
58     }
59
60     private static final long serialVersionUID = 1L;
61     private final LocalHistoryIdentifier historyId;
62     private final long transactionId;
63
64     public TransactionIdentifier(final @Nonnull LocalHistoryIdentifier historyId, final long transactionId) {
65         this.historyId = Preconditions.checkNotNull(historyId);
66         this.transactionId = transactionId;
67     }
68
69     public static TransactionIdentifier readFrom(final DataInput in) throws IOException {
70         final LocalHistoryIdentifier historyId = LocalHistoryIdentifier.readFrom(in);
71         return new TransactionIdentifier(historyId, WritableObjects.readLong(in));
72     }
73
74     @Override
75     public void writeTo(final DataOutput out) throws IOException {
76         historyId.writeTo(out);
77         WritableObjects.writeLong(out, transactionId);
78     }
79
80     public LocalHistoryIdentifier getHistoryId() {
81         return historyId;
82     }
83
84     public long getTransactionId() {
85         return transactionId;
86     }
87
88     @Override
89     public int hashCode() {
90         return historyId.hashCode() * 31 + Long.hashCode(transactionId);
91     }
92
93     @Override
94     public boolean equals(final Object o) {
95         if (this == o) {
96             return true;
97         }
98         if (!(o instanceof TransactionIdentifier)) {
99             return false;
100         }
101
102         final TransactionIdentifier other = (TransactionIdentifier) o;
103         return transactionId == other.transactionId && historyId.equals(other.historyId);
104     }
105
106     @Override
107     public String toString() {
108         return MoreObjects.toStringHelper(TransactionIdentifier.class).add("history", historyId)
109                 .add("transaction", transactionId).toString();
110     }
111
112     private Object writeReplace() {
113         return new Proxy(historyId, transactionId);
114     }
115 }