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 org.opendaylight.controller.cluster.raft.ReplicatedLogEntry;
17 import org.opendaylight.controller.cluster.raft.protobuff.client.messages.Payload;
20 * A {@link ReplicatedLogEntry} implementation.
22 * @author Thomas Pantelis
24 public class SimpleReplicatedLogEntry implements ReplicatedLogEntry, MigratedSerializable {
25 private static final class Proxy implements Externalizable {
26 private static final long serialVersionUID = 1L;
28 private ReplicatedLogEntry replicatedLogEntry;
30 // checkstyle flags the public modifier as redundant which really doesn't make sense since it clearly isn't
31 // redundant. It is explicitly needed for Java serialization to be able to create instances via reflection.
32 @SuppressWarnings("checkstyle:RedundantModifier")
37 Proxy(final ReplicatedLogEntry replicatedLogEntry) {
38 this.replicatedLogEntry = replicatedLogEntry;
42 public void writeExternal(final ObjectOutput out) throws IOException {
43 out.writeLong(replicatedLogEntry.getIndex());
44 out.writeLong(replicatedLogEntry.getTerm());
45 out.writeObject(replicatedLogEntry.getData());
49 public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
50 replicatedLogEntry = new SimpleReplicatedLogEntry(in.readLong(), in.readLong(), (Payload) in.readObject());
53 private Object readResolve() {
54 return replicatedLogEntry;
58 private static final long serialVersionUID = 1L;
60 private final long index;
61 private final long term;
62 private final Payload payload;
63 private boolean persistencePending;
64 private final boolean migrated;
66 private SimpleReplicatedLogEntry(long index, long term, Payload payload, boolean migrated) {
69 this.payload = Preconditions.checkNotNull(payload);
70 this.migrated = migrated;
74 * Constructs an instance.
76 * @param index the index
77 * @param term the term
78 * @param payload the payload
80 public SimpleReplicatedLogEntry(final long index, final long term, final Payload payload) {
81 this(index, term, payload, false);
85 public static ReplicatedLogEntry createMigrated(final long index, final long term, final Payload payload) {
86 return new SimpleReplicatedLogEntry(index, term, payload, true);
90 public Payload getData() {
95 public long getTerm() {
100 public long getIndex() {
106 return getData().size();
110 public boolean isPersistencePending() {
111 return persistencePending;
115 public void setPersistencePending(boolean pending) {
116 persistencePending = pending;
120 public boolean isMigrated() {
125 public Object writeReplace() {
126 return new Proxy(this);
130 public int hashCode() {
131 final int prime = 31;
133 result = prime * result + payload.hashCode();
134 result = prime * result + (int) (index ^ index >>> 32);
135 result = prime * result + (int) (term ^ term >>> 32);
140 public boolean equals(Object obj) {
145 if (obj == null || getClass() != obj.getClass()) {
149 SimpleReplicatedLogEntry other = (SimpleReplicatedLogEntry) obj;
150 return index == other.index && term == other.term && payload.equals(other.payload);
154 public String toString() {
155 return "SimpleReplicatedLogEntry [index=" + index + ", term=" + term + ", payload=" + payload + "]";