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;
43 public void writeExternal(final ObjectOutput out) throws IOException {
44 out.writeLong(replicatedLogEntry.getIndex());
45 out.writeLong(replicatedLogEntry.getTerm());
46 out.writeObject(replicatedLogEntry.getData());
50 public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
51 replicatedLogEntry = new SimpleReplicatedLogEntry(in.readLong(), in.readLong(), (Payload) in.readObject());
54 private Object readResolve() {
55 return replicatedLogEntry;
59 private static final long serialVersionUID = 1L;
61 private final long index;
62 private final long term;
63 private final Payload payload;
64 private boolean persistencePending;
67 * Constructs an instance.
69 * @param index the index
70 * @param term the term
71 * @param payload the payload
73 public SimpleReplicatedLogEntry(long index, long term, Payload payload) {
76 this.payload = Preconditions.checkNotNull(payload);
80 public Payload getData() {
85 public long getTerm() {
90 public long getIndex() {
96 return getData().size();
100 public boolean isPersistencePending() {
101 return persistencePending;
105 public void setPersistencePending(boolean pending) {
106 persistencePending = pending;
109 private Object writeReplace() {
110 return new Proxy(this);
114 public int hashCode() {
115 final int prime = 31;
117 result = prime * result + payload.hashCode();
118 result = prime * result + (int) (index ^ index >>> 32);
119 result = prime * result + (int) (term ^ term >>> 32);
124 public boolean equals(Object obj) {
129 if (obj == null || getClass() != obj.getClass()) {
133 SimpleReplicatedLogEntry other = (SimpleReplicatedLogEntry) obj;
134 return index == other.index && term == other.term && payload.equals(other.payload);
138 public String toString() {
139 return "SimpleReplicatedLogEntry [index=" + index + ", term=" + term + ", payload=" + payload + "]";