Remove JournalWriter.getLastEntry()
[controller.git] / opendaylight / md-sal / cds-access-api / src / main / java / org / opendaylight / controller / cluster / access / concepts / LocalHistoryIdentifier.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 com.google.common.base.MoreObjects;
13 import java.io.DataInput;
14 import java.io.DataOutput;
15 import java.io.IOException;
16 import org.eclipse.jdt.annotation.NonNull;
17 import org.opendaylight.yangtools.concepts.WritableIdentifier;
18 import org.opendaylight.yangtools.concepts.WritableObjects;
19
20 /**
21  * Globally-unique identifier of a local history. This identifier is assigned on the frontend and is composed of
22  * - a {@link ClientIdentifier}, which uniquely identifies a single instantiation of a particular frontend
23  * - an unsigned long, which uniquely identifies the history on the backend
24  * - an unsigned long cookie, assigned by the client and meaningless on the backend, which just reflects it back
25  */
26 public final class LocalHistoryIdentifier implements WritableIdentifier {
27     @java.io.Serial
28     private static final long serialVersionUID = 1L;
29
30     private final @NonNull ClientIdentifier clientId;
31     private final long historyId;
32     private final long cookie;
33
34     public LocalHistoryIdentifier(final ClientIdentifier frontendId, final long historyId) {
35         this(frontendId, historyId, 0);
36     }
37
38     public LocalHistoryIdentifier(final ClientIdentifier frontendId, final long historyId, final long cookie) {
39         clientId = requireNonNull(frontendId);
40         this.historyId = historyId;
41         this.cookie = cookie;
42     }
43
44     public static @NonNull LocalHistoryIdentifier readFrom(final DataInput in) throws IOException {
45         final ClientIdentifier clientId = ClientIdentifier.readFrom(in);
46
47         final byte header = WritableObjects.readLongHeader(in);
48         return new LocalHistoryIdentifier(clientId, WritableObjects.readFirstLong(in, header),
49             WritableObjects.readSecondLong(in, header));
50     }
51
52     @Override
53     public void writeTo(final DataOutput out) throws IOException {
54         clientId.writeTo(out);
55         WritableObjects.writeLongs(out, historyId, cookie);
56     }
57
58     public @NonNull ClientIdentifier getClientId() {
59         return clientId;
60     }
61
62     public long getHistoryId() {
63         return historyId;
64     }
65
66     public long getCookie() {
67         return cookie;
68     }
69
70     @Override
71     public int hashCode() {
72         int ret = clientId.hashCode();
73         ret = 31 * ret + Long.hashCode(historyId);
74         ret = 31 * ret + Long.hashCode(cookie);
75         return ret;
76     }
77
78     @Override
79     public boolean equals(final Object obj) {
80         if (this == obj) {
81             return true;
82         }
83         if (!(obj instanceof LocalHistoryIdentifier other)) {
84             return false;
85         }
86
87         return historyId == other.historyId && cookie == other.cookie && clientId.equals(other.clientId);
88     }
89
90     @Override
91     public String toString() {
92         return MoreObjects.toStringHelper(LocalHistoryIdentifier.class).add("client", clientId)
93                 .add("history", Long.toUnsignedString(historyId, 16))
94                 .add("cookie", Long.toUnsignedString(cookie, 16)).toString();
95     }
96
97     @java.io.Serial
98     private Object writeReplace() {
99         return new HI(this);
100     }
101 }