BUG-5280: remove WritableObjects
[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.WritableIdentifier;
21 import org.opendaylight.yangtools.concepts.WritableObjects;
22
23 /**
24  * Globally-unique identifier of a transaction.
25  *
26  * @author Robert Varga
27  */
28 @Beta
29 public final class TransactionIdentifier implements WritableIdentifier {
30     private static final class Proxy implements Externalizable {
31         private static final long serialVersionUID = 1L;
32         private LocalHistoryIdentifier historyId;
33         private long transactionId;
34
35         public Proxy() {
36             // For Externalizable
37         }
38
39         Proxy(final LocalHistoryIdentifier historyId, final long transactionId) {
40             this.historyId = Preconditions.checkNotNull(historyId);
41             this.transactionId = transactionId;
42         }
43
44         @Override
45         public void writeExternal(final ObjectOutput out) throws IOException {
46             historyId.writeTo(out);
47             WritableObjects.writeLong(out, transactionId);
48         }
49
50         @Override
51         public void readExternal(final ObjectInput in) throws IOException {
52             historyId = LocalHistoryIdentifier.readFrom(in);
53             transactionId = WritableObjects.readLong(in);
54         }
55
56         private Object readResolve() {
57             return new TransactionIdentifier(historyId, transactionId);
58         }
59     }
60
61     private static final long serialVersionUID = 1L;
62     private final LocalHistoryIdentifier historyId;
63     private final long transactionId;
64
65     public TransactionIdentifier(final @Nonnull LocalHistoryIdentifier historyId, final long transactionId) {
66         this.historyId = Preconditions.checkNotNull(historyId);
67         this.transactionId = transactionId;
68     }
69
70     public static TransactionIdentifier readFrom(final DataInput in) throws IOException {
71         final LocalHistoryIdentifier historyId = LocalHistoryIdentifier.readFrom(in);
72         return new TransactionIdentifier(historyId, WritableObjects.readLong(in));
73     }
74
75     @Override
76     public void writeTo(final DataOutput out) throws IOException {
77         historyId.writeTo(out);
78         WritableObjects.writeLong(out, transactionId);
79     }
80
81     public LocalHistoryIdentifier getHistoryId() {
82         return historyId;
83     }
84
85     public long getTransactionId() {
86         return transactionId;
87     }
88
89     @Override
90     public int hashCode() {
91         return historyId.hashCode() * 31 + Long.hashCode(transactionId);
92     }
93
94     @Override
95     public boolean equals(final Object o) {
96         if (this == o) {
97             return true;
98         }
99         if (!(o instanceof TransactionIdentifier)) {
100             return false;
101         }
102
103         final TransactionIdentifier other = (TransactionIdentifier) o;
104         return transactionId == other.transactionId && historyId.equals(other.historyId);
105     }
106
107     @Override
108     public String toString() {
109         return MoreObjects.toStringHelper(TransactionIdentifier.class).add("history", historyId)
110                 .add("transaction", transactionId).toString();
111     }
112
113     private Object writeReplace() {
114         return new Proxy(historyId, transactionId);
115     }
116 }