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 static java.util.Objects.requireNonNull;
12 import com.google.common.annotations.Beta;
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 org.eclipse.jdt.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;
35 // checkstyle flags the public modifier as redundant however it is explicitly needed for Java serialization to
36 // be able to create instances via reflection.
37 @SuppressWarnings("checkstyle:RedundantModifier")
42 Proxy(final LocalHistoryIdentifier historyId, final long transactionId) {
43 this.historyId = requireNonNull(historyId);
44 this.transactionId = transactionId;
48 public void writeExternal(final ObjectOutput out) throws IOException {
49 historyId.writeTo(out);
50 WritableObjects.writeLong(out, transactionId);
54 public void readExternal(final ObjectInput in) throws IOException {
55 historyId = LocalHistoryIdentifier.readFrom(in);
56 transactionId = WritableObjects.readLong(in);
59 private Object readResolve() {
60 return new TransactionIdentifier(historyId, transactionId);
64 private static final long serialVersionUID = 1L;
65 private final LocalHistoryIdentifier historyId;
66 private final long transactionId;
67 private String shortString;
69 public TransactionIdentifier(final @NonNull LocalHistoryIdentifier historyId, final long transactionId) {
70 this.historyId = requireNonNull(historyId);
71 this.transactionId = transactionId;
74 public static TransactionIdentifier readFrom(final DataInput in) throws IOException {
75 final LocalHistoryIdentifier historyId = LocalHistoryIdentifier.readFrom(in);
76 return new TransactionIdentifier(historyId, WritableObjects.readLong(in));
80 public void writeTo(final DataOutput out) throws IOException {
81 historyId.writeTo(out);
82 WritableObjects.writeLong(out, transactionId);
85 public LocalHistoryIdentifier getHistoryId() {
89 public long getTransactionId() {
94 public int hashCode() {
95 return historyId.hashCode() * 31 + Long.hashCode(transactionId);
99 public boolean equals(final Object obj) {
103 if (!(obj instanceof TransactionIdentifier)) {
107 final TransactionIdentifier other = (TransactionIdentifier) obj;
108 return transactionId == other.transactionId && historyId.equals(other.historyId);
111 public String toShortString() {
112 if (shortString == null) {
113 String histStr = historyId.getHistoryId() == 0 ? "" : "-chn-" + historyId.getHistoryId();
114 shortString = historyId.getClientId().getFrontendId().getMemberName().getName() + "-"
115 + historyId.getClientId().getFrontendId().getClientType().getName() + "-fe-"
116 + historyId.getClientId().getGeneration() + histStr + "-txn-" + transactionId
117 + "-" + historyId.getCookie();
124 public String toString() {
125 return toShortString();
128 private Object writeReplace() {
129 return new Proxy(historyId, transactionId);