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.Preconditions;
12 import java.io.DataInput;
13 import java.io.DataOutput;
14 import java.io.Externalizable;
15 import java.io.IOException;
16 import java.io.ObjectInput;
17 import java.io.ObjectOutput;
18 import javax.annotation.Nonnull;
19 import org.opendaylight.yangtools.concepts.WritableIdentifier;
20 import org.opendaylight.yangtools.concepts.WritableObjects;
23 * Globally-unique identifier of a transaction.
25 * @author Robert Varga
28 public final class TransactionIdentifier implements WritableIdentifier {
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;
63 private transient String shortString;
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);
107 public String toShortString() {
108 if(shortString == null) {
109 String histStr = historyId.getHistoryId() == 0 ? "" : "-chn-" + historyId.getHistoryId();
110 shortString = historyId.getClientId().getFrontendId().getMemberName().getName() + "-" +
111 historyId.getClientId().getFrontendId().getClientType().getName() + "-fe-" +
112 historyId.getClientId().getGeneration() + histStr + "-txn-" + transactionId;
119 public String toString() {
120 return toShortString();
123 private Object writeReplace() {
124 return new Proxy(historyId, transactionId);