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 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 java.io.Serial;
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 public final class TransactionIdentifier implements WritableIdentifier {
27 private static final class Proxy implements Externalizable {
29 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 = requireNonNull(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);
59 private Object readResolve() {
60 return new TransactionIdentifier(historyId, transactionId);
65 private static final long serialVersionUID = 1L;
67 private final @NonNull LocalHistoryIdentifier historyId;
68 private final long transactionId;
69 private String shortString;
71 public TransactionIdentifier(final @NonNull LocalHistoryIdentifier historyId, final long transactionId) {
72 this.historyId = requireNonNull(historyId);
73 this.transactionId = transactionId;
76 public static @NonNull TransactionIdentifier readFrom(final DataInput in) throws IOException {
77 final LocalHistoryIdentifier historyId = LocalHistoryIdentifier.readFrom(in);
78 return new TransactionIdentifier(historyId, WritableObjects.readLong(in));
82 public void writeTo(final DataOutput out) throws IOException {
83 historyId.writeTo(out);
84 WritableObjects.writeLong(out, transactionId);
87 public @NonNull LocalHistoryIdentifier getHistoryId() {
91 public long getTransactionId() {
96 public int hashCode() {
97 return historyId.hashCode() * 31 + Long.hashCode(transactionId);
101 public boolean equals(final Object obj) {
102 return this == obj || obj instanceof TransactionIdentifier other && transactionId == other.transactionId
103 && historyId.equals(other.historyId);
106 public String toShortString() {
107 if (shortString == null) {
108 String histStr = historyId.getHistoryId() == 0 ? "" : "-chn-" + historyId.getHistoryId();
109 shortString = historyId.getClientId().getFrontendId().getMemberName().getName() + "-"
110 + historyId.getClientId().getFrontendId().getClientType().getName() + "-fe-"
111 + historyId.getClientId().getGeneration() + histStr + "-txn-" + transactionId
112 + "-" + historyId.getCookie();
119 public String toString() {
120 return toShortString();
124 private Object writeReplace() {
125 return new Proxy(historyId, transactionId);