Bug 5740: Remove Serializable where not necessary
[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
9 package org.opendaylight.controller.cluster.raft.persisted;
10
11 import com.google.common.base.Preconditions;
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.opendaylight.controller.cluster.raft.ReplicatedLogEntry;
18 import org.opendaylight.controller.cluster.raft.protobuff.client.messages.Payload;
19
20 /**
21  * A {@link ReplicatedLogEntry} implementation.
22  *
23  * @author Thomas Pantelis
24  */
25 public final class SimpleReplicatedLogEntry implements ReplicatedLogEntry, Serializable {
26     private static final class Proxy implements Externalizable {
27         private static final long serialVersionUID = 1L;
28
29         private ReplicatedLogEntry replicatedLogEntry;
30
31         // checkstyle flags the public modifier as redundant which really doesn't make sense since it clearly isn't
32         // redundant. It is explicitly needed for Java serialization to be able to create instances via reflection.
33         @SuppressWarnings("checkstyle:RedundantModifier")
34         public Proxy() {
35             // For Externalizable
36         }
37
38         Proxy(final ReplicatedLogEntry replicatedLogEntry) {
39             this.replicatedLogEntry = replicatedLogEntry;
40         }
41
42         @Override
43         public void writeExternal(final ObjectOutput out) throws IOException {
44             out.writeLong(replicatedLogEntry.getIndex());
45             out.writeLong(replicatedLogEntry.getTerm());
46             out.writeObject(replicatedLogEntry.getData());
47         }
48
49         @Override
50         public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
51             replicatedLogEntry = new SimpleReplicatedLogEntry(in.readLong(), in.readLong(), (Payload) in.readObject());
52         }
53
54         private Object readResolve() {
55             return replicatedLogEntry;
56         }
57     }
58
59     private static final long serialVersionUID = 1L;
60
61     private final long index;
62     private final long term;
63     private final Payload payload;
64     private boolean persistencePending;
65
66     /**
67      * Constructs an instance.
68      *
69      * @param index the index
70      * @param term the term
71      * @param payload the payload
72      */
73     public SimpleReplicatedLogEntry(long index, long term, Payload payload) {
74         this.index = index;
75         this.term = term;
76         this.payload = Preconditions.checkNotNull(payload);
77     }
78
79     @Override
80     public Payload getData() {
81         return payload;
82     }
83
84     @Override
85     public long getTerm() {
86         return term;
87     }
88
89     @Override
90     public long getIndex() {
91         return index;
92     }
93
94     @Override
95     public int size() {
96         return getData().size();
97     }
98
99     @Override
100     public boolean isPersistencePending() {
101         return persistencePending;
102     }
103
104     @Override
105     public void setPersistencePending(boolean pending) {
106         persistencePending = pending;
107     }
108
109     private Object writeReplace() {
110         return new Proxy(this);
111     }
112
113     @Override
114     public int hashCode() {
115         final int prime = 31;
116         int result = 1;
117         result = prime * result + payload.hashCode();
118         result = prime * result + (int) (index ^ index >>> 32);
119         result = prime * result + (int) (term ^ term >>> 32);
120         return result;
121     }
122
123     @Override
124     public boolean equals(Object obj) {
125         if (this == obj) {
126             return true;
127         }
128
129         if (obj == null || getClass() != obj.getClass()) {
130             return false;
131         }
132
133         SimpleReplicatedLogEntry other = (SimpleReplicatedLogEntry) obj;
134         return index == other.index && term == other.term && payload.equals(other.payload);
135     }
136
137     @Override
138     public String toString() {
139         return "SimpleReplicatedLogEntry [index=" + index + ", term=" + term + ", payload=" + payload + "]";
140     }
141 }