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);
58 private Object readResolve() {
59 return new TransactionIdentifier(historyId, transactionId);
64 private static final long serialVersionUID = 1L;
66 private final @NonNull LocalHistoryIdentifier historyId;
67 private final long transactionId;
68 private String shortString;
70 public TransactionIdentifier(final @NonNull LocalHistoryIdentifier historyId, final long transactionId) {
71 this.historyId = requireNonNull(historyId);
72 this.transactionId = transactionId;
75 public static @NonNull TransactionIdentifier readFrom(final DataInput in) throws IOException {
76 final LocalHistoryIdentifier historyId = LocalHistoryIdentifier.readFrom(in);
77 return new TransactionIdentifier(historyId, WritableObjects.readLong(in));
81 public void writeTo(final DataOutput out) throws IOException {
82 historyId.writeTo(out);
83 WritableObjects.writeLong(out, transactionId);
86 public @NonNull LocalHistoryIdentifier getHistoryId() {
90 public long getTransactionId() {
95 public int hashCode() {
96 return historyId.hashCode() * 31 + Long.hashCode(transactionId);
100 public boolean equals(final Object obj) {
101 return this == obj || obj instanceof TransactionIdentifier other && transactionId == other.transactionId
102 && historyId.equals(other.historyId);
105 public String toShortString() {
106 if (shortString == null) {
107 String histStr = historyId.getHistoryId() == 0 ? "" : "-chn-" + historyId.getHistoryId();
108 shortString = historyId.getClientId().getFrontendId().getMemberName().getName() + "-"
109 + historyId.getClientId().getFrontendId().getClientType().getName() + "-fe-"
110 + historyId.getClientId().getGeneration() + histStr + "-txn-" + transactionId
111 + "-" + historyId.getCookie();
118 public String toString() {
119 return toShortString();
122 private Object writeReplace() {
123 return new Proxy(historyId, transactionId);