9b7a2f34566342ecd86a52aec1e96ef783f4b9ef
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / main / java / org / opendaylight / controller / cluster / raft / messages / RequestVoteReply.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
9 package org.opendaylight.controller.cluster.raft.messages;
10
11 import java.io.Externalizable;
12 import java.io.IOException;
13 import java.io.ObjectInput;
14 import java.io.ObjectOutput;
15
16 public final class RequestVoteReply extends AbstractRaftRPC {
17     private static final long serialVersionUID = 8427899326488775660L;
18
19     // true means candidate received vote
20     private final boolean voteGranted;
21
22     public RequestVoteReply(long term, boolean voteGranted) {
23         super(term);
24         this.voteGranted = voteGranted;
25     }
26
27     public boolean isVoteGranted() {
28         return voteGranted;
29     }
30
31     @Override
32     public String toString() {
33         return "RequestVoteReply [term=" + getTerm() + ", voteGranted=" + voteGranted + "]";
34     }
35
36     private Object writeReplace() {
37         return new Proxy(this);
38     }
39
40     private static class Proxy implements Externalizable {
41         private static final long serialVersionUID = 1L;
42
43         private RequestVoteReply requestVoteReply;
44
45         // checkstyle flags the public modifier as redundant which really doesn't make sense since it clearly isn't
46         // redundant. It is explicitly needed for Java serialization to be able to create instances via reflection.
47         @SuppressWarnings("checkstyle:RedundantModifier")
48         public Proxy() {
49         }
50
51         Proxy(RequestVoteReply requestVoteReply) {
52             this.requestVoteReply = requestVoteReply;
53         }
54
55         @Override
56         public void writeExternal(ObjectOutput out) throws IOException {
57             out.writeLong(requestVoteReply.getTerm());
58             out.writeBoolean(requestVoteReply.voteGranted);
59         }
60
61         @Override
62         public void readExternal(ObjectInput in) throws IOException {
63             long term = in.readLong();
64             boolean voteGranted = in.readBoolean();
65
66             requestVoteReply = new RequestVoteReply(term, voteGranted);
67         }
68
69         private Object readResolve() {
70             return requestVoteReply;
71         }
72     }
73 }