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 sealed class SimpleReplicatedLogEntry implements ReplicatedLogEntry, Serializable {
27 @Deprecated(since = "7.0.0", forRemoval = true)
28 private static final class Legacy extends SimpleReplicatedLogEntry implements LegacySerializable {
30 private static final long serialVersionUID = 1L;
32 Legacy(final long index, final long term, final Payload payload) {
33 super(index, term, payload);
37 @Deprecated(since = "7.0.0", forRemoval = true)
38 private static final class Proxy implements Externalizable {
40 private static final long serialVersionUID = 1L;
46 // checkstyle flags the public modifier as redundant which really doesn't make sense since it clearly isn't
47 // redundant. It is explicitly needed for Java serialization to be able to create instances via reflection.
48 @SuppressWarnings("checkstyle:RedundantModifier")
54 public void writeExternal(final ObjectOutput out) throws IOException {
57 out.writeObject(data);
61 public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
62 index = in.readLong();
64 data = (Payload) in.readObject();
68 private Object readResolve() {
69 return new Legacy(index, term, data);
74 private static final long serialVersionUID = 1L;
75 // Estimate to how big the proxy is. Note this includes object stream overhead, so it is a bit conservative.
76 private static final int PROXY_SIZE = SerializationUtils.serialize(new LE((Void) null)).length;
78 private final long index;
79 private final long term;
80 private final Payload payload;
81 private boolean persistencePending;
84 * Constructs an instance.
86 * @param index the index
87 * @param term the term
88 * @param payload the payload
90 public SimpleReplicatedLogEntry(final long index, final long term, final Payload payload) {
93 this.payload = requireNonNull(payload);
97 public final Payload getData() {
102 public final long getTerm() {
107 public final long getIndex() {
112 public final int size() {
113 return payload.size();
117 public final int serializedSize() {
118 return PROXY_SIZE + payload.serializedSize();
122 public final boolean isPersistencePending() {
123 return persistencePending;
127 public final void setPersistencePending(final boolean pending) {
128 persistencePending = pending;
132 public final Object writeReplace() {
137 public final int hashCode() {
138 final int prime = 31;
140 result = prime * result + payload.hashCode();
141 result = prime * result + (int) (index ^ index >>> 32);
142 result = prime * result + (int) (term ^ term >>> 32);
147 public final boolean equals(final Object obj) {
148 return this == obj || obj instanceof SimpleReplicatedLogEntry other && index == other.index
149 && term == other.term && payload.equals(other.payload);
153 public final String toString() {
154 return "SimpleReplicatedLogEntry [index=" + index + ", term=" + term + ", payload=" + payload + "]";