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;
34 // checkstyle flags the public modifier as redundant however it is explicitly needed for Java serialization to
35 // be able to create instances via reflection.
36 @SuppressWarnings("checkstyle:RedundantModifier")
41 Proxy(final LocalHistoryIdentifier historyId, final long transactionId) {
42 this.historyId = Preconditions.checkNotNull(historyId);
43 this.transactionId = transactionId;
47 public void writeExternal(final ObjectOutput out) throws IOException {
48 historyId.writeTo(out);
49 WritableObjects.writeLong(out, transactionId);
53 public void readExternal(final ObjectInput in) throws IOException {
54 historyId = LocalHistoryIdentifier.readFrom(in);
55 transactionId = WritableObjects.readLong(in);
58 private Object readResolve() {
59 return new TransactionIdentifier(historyId, transactionId);
63 private static final long serialVersionUID = 1L;
64 private final LocalHistoryIdentifier historyId;
65 private final long transactionId;
66 private String shortString;
68 public TransactionIdentifier(@Nonnull final LocalHistoryIdentifier historyId, final long transactionId) {
69 this.historyId = Preconditions.checkNotNull(historyId);
70 this.transactionId = transactionId;
73 public static TransactionIdentifier readFrom(final DataInput in) throws IOException {
74 final LocalHistoryIdentifier historyId = LocalHistoryIdentifier.readFrom(in);
75 return new TransactionIdentifier(historyId, WritableObjects.readLong(in));
79 public void writeTo(final DataOutput out) throws IOException {
80 historyId.writeTo(out);
81 WritableObjects.writeLong(out, transactionId);
84 public LocalHistoryIdentifier getHistoryId() {
88 public long getTransactionId() {
93 public int hashCode() {
94 return historyId.hashCode() * 31 + Long.hashCode(transactionId);
98 public boolean equals(final Object obj) {
102 if (!(obj instanceof TransactionIdentifier)) {
106 final TransactionIdentifier other = (TransactionIdentifier) obj;
107 return transactionId == other.transactionId && historyId.equals(other.historyId);
110 public String toShortString() {
111 if (shortString == null) {
112 String histStr = historyId.getHistoryId() == 0 ? "" : "-chn-" + historyId.getHistoryId();
113 shortString = historyId.getClientId().getFrontendId().getMemberName().getName() + "-"
114 + historyId.getClientId().getFrontendId().getClientType().getName() + "-fe-"
115 + historyId.getClientId().getGeneration() + histStr + "-txn-" + transactionId;
122 public String toString() {
123 return toShortString();
126 private Object writeReplace() {
127 return new Proxy(historyId, transactionId);