2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.cluster.raft.persisted;
10 import static java.util.Objects.requireNonNull;
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;
22 * A {@link ReplicatedLogEntry} implementation.
24 * @author Thomas Pantelis
26 public final class SimpleReplicatedLogEntry implements ReplicatedLogEntry, Serializable {
27 private static final class Proxy implements Externalizable {
28 private static final long serialVersionUID = 1L;
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")
41 Proxy(final SimpleReplicatedLogEntry replicatedLogEntry) {
42 index = replicatedLogEntry.getIndex();
43 term = replicatedLogEntry.getTerm();
44 data = replicatedLogEntry.getData();
48 public void writeExternal(final ObjectOutput out) throws IOException {
51 out.writeObject(data);
55 public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
56 index = in.readLong();
58 data = (Payload) in.readObject();
61 private Object readResolve() {
62 return new SimpleReplicatedLogEntry(index, term, data);
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;
70 private final long index;
71 private final long term;
72 private final Payload payload;
73 private boolean persistencePending;
76 * Constructs an instance.
78 * @param index the index
79 * @param term the term
80 * @param payload the payload
82 public SimpleReplicatedLogEntry(final long index, final long term, final Payload payload) {
85 this.payload = requireNonNull(payload);
89 public Payload getData() {
94 public long getTerm() {
99 public long getIndex() {
105 return payload.size();
109 public int serializedSize() {
110 return PROXY_SIZE + payload.serializedSize();
114 public boolean isPersistencePending() {
115 return persistencePending;
119 public void setPersistencePending(final boolean pending) {
120 persistencePending = pending;
123 private Object writeReplace() {
124 return new Proxy(this);
128 public int hashCode() {
129 final int prime = 31;
131 result = prime * result + payload.hashCode();
132 result = prime * result + (int) (index ^ index >>> 32);
133 result = prime * result + (int) (term ^ term >>> 32);
138 public boolean equals(final Object obj) {
143 if (obj == null || getClass() != obj.getClass()) {
147 SimpleReplicatedLogEntry other = (SimpleReplicatedLogEntry) obj;
148 return index == other.index && term == other.term && payload.equals(other.payload);
152 public String toString() {
153 return "SimpleReplicatedLogEntry [index=" + index + ", term=" + term + ", payload=" + payload + "]";