Remove JournalWriter.getLastEntry()
[controller.git] / opendaylight / md-sal / cds-access-api / src / main / java / org / opendaylight / controller / cluster / access / concepts / TransactionIdentifier.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.cluster.access.concepts;
9
10 import static java.util.Objects.requireNonNull;
11
12 import java.io.DataInput;
13 import java.io.DataOutput;
14 import java.io.IOException;
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.opendaylight.yangtools.concepts.WritableIdentifier;
17 import org.opendaylight.yangtools.concepts.WritableObjects;
18
19 /**
20  * Globally-unique identifier of a transaction.
21  */
22 public final class TransactionIdentifier implements WritableIdentifier {
23     @java.io.Serial
24     private static final long serialVersionUID = 1L;
25
26     private final @NonNull LocalHistoryIdentifier historyId;
27     private final long transactionId;
28     private String shortString;
29
30     public TransactionIdentifier(final @NonNull LocalHistoryIdentifier historyId, final long transactionId) {
31         this.historyId = requireNonNull(historyId);
32         this.transactionId = transactionId;
33     }
34
35     public static @NonNull TransactionIdentifier readFrom(final DataInput in) throws IOException {
36         final LocalHistoryIdentifier historyId = LocalHistoryIdentifier.readFrom(in);
37         return new TransactionIdentifier(historyId, WritableObjects.readLong(in));
38     }
39
40     @Override
41     public void writeTo(final DataOutput out) throws IOException {
42         historyId.writeTo(out);
43         WritableObjects.writeLong(out, transactionId);
44     }
45
46     public @NonNull LocalHistoryIdentifier getHistoryId() {
47         return historyId;
48     }
49
50     public long getTransactionId() {
51         return transactionId;
52     }
53
54     @Override
55     public int hashCode() {
56         return historyId.hashCode() * 31 + Long.hashCode(transactionId);
57     }
58
59     @Override
60     public boolean equals(final Object obj) {
61         return this == obj || obj instanceof TransactionIdentifier other && transactionId == other.transactionId
62             && historyId.equals(other.historyId);
63     }
64
65     public String toShortString() {
66         if (shortString == null) {
67             String histStr = historyId.getHistoryId() == 0 ? "" : "-chn-" + historyId.getHistoryId();
68             shortString = historyId.getClientId().getFrontendId().getMemberName().getName() + "-"
69                     + historyId.getClientId().getFrontendId().getClientType().getName() + "-fe-"
70                     + historyId.getClientId().getGeneration() + histStr + "-txn-" + transactionId
71                     + "-" + historyId.getCookie();
72         }
73
74         return shortString;
75     }
76
77     @Override
78     public String toString() {
79         return toShortString();
80     }
81
82     @java.io.Serial
83     private Object writeReplace() {
84         return new TI(this);
85     }
86 }