Add Payload.serializedSize()
[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 final class SimpleReplicatedLogEntry implements ReplicatedLogEntry, Serializable {
27     private static final class Proxy implements Externalizable {
28         private static final long serialVersionUID = 1L;
29
30         private long index;
31         private long term;
32         private Payload data;
33
34         // checkstyle flags the public modifier as redundant which really doesn't make sense since it clearly isn't
35         // redundant. It is explicitly needed for Java serialization to be able to create instances via reflection.
36         @SuppressWarnings("checkstyle:RedundantModifier")
37         public Proxy() {
38             // For Externalizable
39         }
40
41         Proxy(final SimpleReplicatedLogEntry replicatedLogEntry) {
42             index = replicatedLogEntry.getIndex();
43             term = replicatedLogEntry.getTerm();
44             data = replicatedLogEntry.getData();
45         }
46
47         @Override
48         public void writeExternal(final ObjectOutput out) throws IOException {
49             out.writeLong(index);
50             out.writeLong(term);
51             out.writeObject(data);
52         }
53
54         @Override
55         public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
56             index = in.readLong();
57             term = in.readLong();
58             data = (Payload) in.readObject();
59         }
60
61         private Object readResolve() {
62             return new SimpleReplicatedLogEntry(index, term, data);
63         }
64     }
65
66     private static final long serialVersionUID = 1L;
67     // Estimate to how big the proxy is. Note this includes object stream overhead, so it is a bit conservative
68     private static final int PROXY_SIZE = SerializationUtils.serialize(new Proxy()).length;
69
70     private final long index;
71     private final long term;
72     private final Payload payload;
73     private boolean persistencePending;
74
75     /**
76      * Constructs an instance.
77      *
78      * @param index the index
79      * @param term the term
80      * @param payload the payload
81      */
82     public SimpleReplicatedLogEntry(final long index, final long term, final Payload payload) {
83         this.index = index;
84         this.term = term;
85         this.payload = requireNonNull(payload);
86     }
87
88     @Override
89     public Payload getData() {
90         return payload;
91     }
92
93     @Override
94     public long getTerm() {
95         return term;
96     }
97
98     @Override
99     public long getIndex() {
100         return index;
101     }
102
103     @Override
104     public int size() {
105         return payload.size();
106     }
107
108     @Override
109     public int serializedSize() {
110         return PROXY_SIZE + payload.serializedSize();
111     }
112
113     @Override
114     public boolean isPersistencePending() {
115         return persistencePending;
116     }
117
118     @Override
119     public void setPersistencePending(final boolean pending) {
120         persistencePending = pending;
121     }
122
123     private Object writeReplace() {
124         return new Proxy(this);
125     }
126
127     @Override
128     public int hashCode() {
129         final int prime = 31;
130         int result = 1;
131         result = prime * result + payload.hashCode();
132         result = prime * result + (int) (index ^ index >>> 32);
133         result = prime * result + (int) (term ^ term >>> 32);
134         return result;
135     }
136
137     @Override
138     public boolean equals(final Object obj) {
139         if (this == obj) {
140             return true;
141         }
142
143         if (obj == null || getClass() != obj.getClass()) {
144             return false;
145         }
146
147         SimpleReplicatedLogEntry other = (SimpleReplicatedLogEntry) obj;
148         return index == other.index && term == other.term && payload.equals(other.payload);
149     }
150
151     @Override
152     public String toString() {
153         return "SimpleReplicatedLogEntry [index=" + index + ", term=" + term + ", payload=" + payload + "]";
154     }
155 }