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.WritableIdentifier;
21 import org.opendaylight.yangtools.concepts.WritableObjects;
24 * Globally-unique identifier of a transaction.
26 * @author Robert Varga
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;
39 Proxy(final LocalHistoryIdentifier historyId, final long transactionId) {
40 this.historyId = Preconditions.checkNotNull(historyId);
41 this.transactionId = transactionId;
45 public void writeExternal(final ObjectOutput out) throws IOException {
46 historyId.writeTo(out);
47 WritableObjects.writeLong(out, transactionId);
51 public void readExternal(final ObjectInput in) throws IOException {
52 historyId = LocalHistoryIdentifier.readFrom(in);
53 transactionId = WritableObjects.readLong(in);
56 private Object readResolve() {
57 return new TransactionIdentifier(historyId, transactionId);
61 private static final long serialVersionUID = 1L;
62 private final LocalHistoryIdentifier historyId;
63 private final long transactionId;
65 public TransactionIdentifier(final @Nonnull LocalHistoryIdentifier historyId, final long transactionId) {
66 this.historyId = Preconditions.checkNotNull(historyId);
67 this.transactionId = transactionId;
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));
76 public void writeTo(final DataOutput out) throws IOException {
77 historyId.writeTo(out);
78 WritableObjects.writeLong(out, transactionId);
81 public LocalHistoryIdentifier getHistoryId() {
85 public long getTransactionId() {
90 public int hashCode() {
91 return historyId.hashCode() * 31 + Long.hashCode(transactionId);
95 public boolean equals(final Object o) {
99 if (!(o instanceof TransactionIdentifier)) {
103 final TransactionIdentifier other = (TransactionIdentifier) o;
104 return transactionId == other.transactionId && historyId.equals(other.historyId);
108 public String toString() {
109 return MoreObjects.toStringHelper(TransactionIdentifier.class).add("history", historyId)
110 .add("transaction", transactionId).toString();
113 private Object writeReplace() {
114 return new Proxy(historyId, transactionId);