2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.cluster.access.concepts;
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;
23 * Globally-unique identifier of a transaction.
25 * @author Robert Varga
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;
38 Proxy(final LocalHistoryIdentifier historyId, final long transactionId) {
39 this.historyId = Preconditions.checkNotNull(historyId);
40 this.transactionId = transactionId;
44 public void writeExternal(final ObjectOutput out) throws IOException {
45 historyId.writeTo(out);
46 WritableObjects.writeLong(out, transactionId);
50 public void readExternal(final ObjectInput in) throws IOException {
51 historyId = LocalHistoryIdentifier.readFrom(in);
52 transactionId = WritableObjects.readLong(in);
55 private Object readResolve() {
56 return new TransactionIdentifier(historyId, transactionId);
60 private static final long serialVersionUID = 1L;
61 private final LocalHistoryIdentifier historyId;
62 private final long transactionId;
64 public TransactionIdentifier(final @Nonnull LocalHistoryIdentifier historyId, final long transactionId) {
65 this.historyId = Preconditions.checkNotNull(historyId);
66 this.transactionId = transactionId;
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));
75 public void writeTo(final DataOutput out) throws IOException {
76 historyId.writeTo(out);
77 WritableObjects.writeLong(out, transactionId);
80 public LocalHistoryIdentifier getHistoryId() {
84 public long getTransactionId() {
89 public int hashCode() {
90 return historyId.hashCode() * 31 + Long.hashCode(transactionId);
94 public boolean equals(final Object o) {
98 if (!(o instanceof TransactionIdentifier)) {
102 final TransactionIdentifier other = (TransactionIdentifier) o;
103 return transactionId == other.transactionId && historyId.equals(other.historyId);
107 public String toString() {
108 return MoreObjects.toStringHelper(TransactionIdentifier.class).add("history", historyId)
109 .add("transaction", transactionId).toString();
112 private Object writeReplace() {
113 return new Proxy(historyId, transactionId);