Annotate AbstractRaftRPC with java.io.Serial
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / main / java / org / opendaylight / controller / cluster / raft / messages / RequestVote.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.messages;
9
10 import java.io.Externalizable;
11 import java.io.IOException;
12 import java.io.ObjectInput;
13 import java.io.ObjectOutput;
14
15 /**
16  * Invoked by candidates to gather votes (§5.2).
17  */
18 public final class RequestVote extends AbstractRaftRPC {
19     @java.io.Serial
20     private static final long serialVersionUID = -6967509186297108657L;
21
22     // candidate requesting vote
23     private final String candidateId;
24
25     // index of candidate’s last log entry (§5.4)
26     private final long lastLogIndex;
27
28     // term of candidate’s last log entry (§5.4)
29     private final long lastLogTerm;
30
31     public RequestVote(final long term, final String candidateId, final long lastLogIndex, final long lastLogTerm) {
32         super(term);
33         this.candidateId = candidateId;
34         this.lastLogIndex = lastLogIndex;
35         this.lastLogTerm = lastLogTerm;
36     }
37
38     public String getCandidateId() {
39         return candidateId;
40     }
41
42     public long getLastLogIndex() {
43         return lastLogIndex;
44     }
45
46     public long getLastLogTerm() {
47         return lastLogTerm;
48     }
49
50     @Override
51     public String toString() {
52         return "RequestVote [term=" + getTerm()
53                 + ", candidateId=" + candidateId
54                 + ", lastLogIndex=" + lastLogIndex
55                 + ", lastLogTerm=" + lastLogTerm
56                 + "]";
57     }
58
59     @Override
60     Object writeReplace() {
61         return new RV(this);
62     }
63
64     @Deprecated(since = "7.0.0", forRemoval = true)
65     private static class Proxy implements Externalizable {
66         @java.io.Serial
67         private static final long serialVersionUID = 1L;
68
69         private RequestVote requestVote;
70
71         // checkstyle flags the public modifier as redundant which really doesn't make sense since it clearly isn't
72         // redundant. It is explicitly needed for Java serialization to be able to create instances via reflection.
73         @SuppressWarnings("checkstyle:RedundantModifier")
74         public Proxy() {
75         }
76
77         @Override
78         public void writeExternal(final ObjectOutput out) {
79             throw new UnsupportedOperationException();
80         }
81
82         @Override
83         public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
84             long term = in.readLong();
85             String candidateId = (String) in.readObject();
86             long lastLogIndex = in.readLong();
87             long lastLogTerm = in.readLong();
88
89             requestVote = new RequestVote(term, candidateId, lastLogIndex, lastLogTerm);
90         }
91
92         @java.io.Serial
93         private Object readResolve() {
94             return requestVote;
95         }
96     }
97 }