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
9 package org.opendaylight.controller.cluster.raft.persisted;
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;
21 * A {@link ReplicatedLogEntry} implementation.
23 * @author Thomas Pantelis
25 public final class SimpleReplicatedLogEntry implements ReplicatedLogEntry, Serializable {
26 private static final class Proxy implements Externalizable {
27 private static final long serialVersionUID = 1L;
29 private ReplicatedLogEntry replicatedLogEntry;
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")
38 Proxy(final ReplicatedLogEntry replicatedLogEntry) {
39 this.replicatedLogEntry = replicatedLogEntry;
42 static int estimatedSerializedSize(ReplicatedLogEntry replicatedLogEntry) {
43 return 8 /* index */ + 8 /* term */ + replicatedLogEntry.getData().size()
44 + 400 /* estimated extra padding for class info */;
48 public void writeExternal(final ObjectOutput out) throws IOException {
49 out.writeLong(replicatedLogEntry.getIndex());
50 out.writeLong(replicatedLogEntry.getTerm());
51 out.writeObject(replicatedLogEntry.getData());
55 public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
56 replicatedLogEntry = new SimpleReplicatedLogEntry(in.readLong(), in.readLong(), (Payload) in.readObject());
59 private Object readResolve() {
60 return replicatedLogEntry;
64 private static final long serialVersionUID = 1L;
66 private final long index;
67 private final long term;
68 private final Payload payload;
69 private boolean persistencePending;
72 * Constructs an instance.
74 * @param index the index
75 * @param term the term
76 * @param payload the payload
78 public SimpleReplicatedLogEntry(long index, long term, Payload payload) {
81 this.payload = Preconditions.checkNotNull(payload);
85 public Payload getData() {
90 public long getTerm() {
95 public long getIndex() {
101 return getData().size();
105 public boolean isPersistencePending() {
106 return persistencePending;
110 public void setPersistencePending(boolean pending) {
111 persistencePending = pending;
114 private Object writeReplace() {
115 return new Proxy(this);
118 public int estimatedSerializedSize() {
119 return Proxy.estimatedSerializedSize(this);
123 public int hashCode() {
124 final int prime = 31;
126 result = prime * result + payload.hashCode();
127 result = prime * result + (int) (index ^ index >>> 32);
128 result = prime * result + (int) (term ^ term >>> 32);
133 public boolean equals(Object obj) {
138 if (obj == null || getClass() != obj.getClass()) {
142 SimpleReplicatedLogEntry other = (SimpleReplicatedLogEntry) obj;
143 return index == other.index && term == other.term && payload.equals(other.payload);
147 public String toString() {
148 return "SimpleReplicatedLogEntry [index=" + index + ", term=" + term + ", payload=" + payload + "]";