d075a417017a6066e08d62306f68c85e52508e89
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / main / java / org / opendaylight / controller / cluster / raft / persisted / SimpleReplicatedLogEntry.java
1 /*
2  * Copyright (c) 2014 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.raft.persisted;
9
10 import static java.util.Objects.requireNonNull;
11
12 import java.io.Externalizable;
13 import java.io.IOException;
14 import java.io.ObjectInput;
15 import java.io.ObjectOutput;
16 import java.io.Serializable;
17 import org.apache.commons.lang3.SerializationUtils;
18 import org.opendaylight.controller.cluster.raft.ReplicatedLogEntry;
19 import org.opendaylight.controller.cluster.raft.messages.Payload;
20
21 /**
22  * A {@link ReplicatedLogEntry} implementation.
23  *
24  * @author Thomas Pantelis
25  */
26 public sealed class SimpleReplicatedLogEntry implements ReplicatedLogEntry, Serializable {
27     @Deprecated(since = "7.0.0", forRemoval = true)
28     private static final class Legacy extends SimpleReplicatedLogEntry implements LegacySerializable {
29         @java.io.Serial
30         private static final long serialVersionUID = 1L;
31
32         Legacy(final long index, final long term, final Payload payload) {
33             super(index, term, payload);
34         }
35     }
36
37     @Deprecated(since = "7.0.0", forRemoval = true)
38     private static final class Proxy implements Externalizable {
39         @java.io.Serial
40         private static final long serialVersionUID = 1L;
41
42         private long index;
43         private long term;
44         private Payload data;
45
46         // checkstyle flags the public modifier as redundant which really doesn't make sense since it clearly isn't
47         // redundant. It is explicitly needed for Java serialization to be able to create instances via reflection.
48         @SuppressWarnings("checkstyle:RedundantModifier")
49         public Proxy() {
50             // For Externalizable
51         }
52
53         Proxy(final SimpleReplicatedLogEntry replicatedLogEntry) {
54             index = replicatedLogEntry.getIndex();
55             term = replicatedLogEntry.getTerm();
56             data = replicatedLogEntry.getData();
57         }
58
59         @Override
60         public void writeExternal(final ObjectOutput out) throws IOException {
61             out.writeLong(index);
62             out.writeLong(term);
63             out.writeObject(data);
64         }
65
66         @Override
67         public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
68             index = in.readLong();
69             term = in.readLong();
70             data = (Payload) in.readObject();
71         }
72
73         @java.io.Serial
74         private Object readResolve() {
75             return new Legacy(index, term, data);
76         }
77     }
78
79     @java.io.Serial
80     private static final long serialVersionUID = 1L;
81     // Estimate to how big the proxy is. Note this includes object stream overhead, so it is a bit conservative.
82     private static final int PROXY_SIZE = SerializationUtils.serialize(new LE((Void) null)).length;
83
84     private final long index;
85     private final long term;
86     private final Payload payload;
87     private boolean persistencePending;
88
89     /**
90      * Constructs an instance.
91      *
92      * @param index the index
93      * @param term the term
94      * @param payload the payload
95      */
96     public SimpleReplicatedLogEntry(final long index, final long term, final Payload payload) {
97         this.index = index;
98         this.term = term;
99         this.payload = requireNonNull(payload);
100     }
101
102     @Override
103     public final Payload getData() {
104         return payload;
105     }
106
107     @Override
108     public final long getTerm() {
109         return term;
110     }
111
112     @Override
113     public final long getIndex() {
114         return index;
115     }
116
117     @Override
118     public final int size() {
119         return payload.size();
120     }
121
122     @Override
123     public final int serializedSize() {
124         return PROXY_SIZE + payload.serializedSize();
125     }
126
127     @Override
128     public final boolean isPersistencePending() {
129         return persistencePending;
130     }
131
132     @Override
133     public final void setPersistencePending(final boolean pending) {
134         persistencePending = pending;
135     }
136
137     @java.io.Serial
138     public final Object writeReplace() {
139         return new LE(this);
140     }
141
142     @Override
143     public final int hashCode() {
144         final int prime = 31;
145         int result = 1;
146         result = prime * result + payload.hashCode();
147         result = prime * result + (int) (index ^ index >>> 32);
148         result = prime * result + (int) (term ^ term >>> 32);
149         return result;
150     }
151
152     @Override
153     public final boolean equals(final Object obj) {
154         return this == obj || obj instanceof SimpleReplicatedLogEntry other && index == other.index
155             && term == other.term && payload.equals(other.payload);
156     }
157
158     @Override
159     public final String toString() {
160         return "SimpleReplicatedLogEntry [index=" + index + ", term=" + term + ", payload=" + payload + "]";
161     }
162 }