BUG-5280: add cds-access-api identifiers
[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.Externalizable;
14 import java.io.IOException;
15 import java.io.ObjectInput;
16 import java.io.ObjectOutput;
17 import javax.annotation.Nonnull;
18 import org.opendaylight.yangtools.concepts.Identifier;
19
20 /**
21  * Globally-unique identifier of a transaction.
22  *
23  * @param <T> Frontend type
24  *
25  * @author Robert Varga
26  */
27 @Beta
28 public final class TransactionIdentifier<T extends FrontendType> implements Identifier {
29     private static final class Proxy<T extends FrontendType> implements Externalizable {
30         private static final long serialVersionUID = 1L;
31         private LocalHistoryIdentifier<T> historyId;
32         private long transactionId;
33
34         public Proxy() {
35             // For Externalizable
36         }
37
38         Proxy(final LocalHistoryIdentifier<T> 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             out.writeObject(historyId);
46             out.writeLong(transactionId);
47         }
48
49         @SuppressWarnings("unchecked")
50         @Override
51         public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
52             historyId = (LocalHistoryIdentifier<T>) in.readObject();
53             transactionId = in.readLong();
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<T> historyId;
63     private final long transactionId;
64
65     public TransactionIdentifier(final @Nonnull LocalHistoryIdentifier<T> historyId, final long transactionId) {
66         this.historyId = Preconditions.checkNotNull(historyId);
67         this.transactionId = transactionId;
68     }
69
70     public LocalHistoryIdentifier<T> getHistoryId() {
71         return historyId;
72     }
73
74     public long getTransactionId() {
75         return transactionId;
76     }
77
78     @Override
79     public int hashCode() {
80         return historyId.hashCode() * 31 + Long.hashCode(transactionId);
81     }
82
83     @Override
84     public boolean equals(final Object o) {
85         if (this == o) {
86             return true;
87         }
88         if (!(o instanceof TransactionIdentifier)) {
89             return false;
90         }
91
92         final TransactionIdentifier<?> other = (TransactionIdentifier<?>) o;
93         return transactionId == other.transactionId && historyId.equals(other.historyId);
94     }
95
96     @Override
97     public String toString() {
98         return MoreObjects.toStringHelper(TransactionIdentifier.class).add("history", historyId)
99                 .add("transaction", transactionId).toString();
100     }
101
102     private Object writeReplace() {
103         return new Proxy<>(historyId, transactionId);
104     }
105 }