5edd2ed5358dd19a7137d522cdbb67a6989c5717
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / main / java / org / opendaylight / controller / cluster / raft / persisted / SimpleReplicatedLogEntry.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.cluster.raft.persisted;
10
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;
18
19 /**
20  * A {@link ReplicatedLogEntry} implementation.
21  *
22  * @author Thomas Pantelis
23  */
24 public final class SimpleReplicatedLogEntry implements ReplicatedLogEntry, MigratedSerializable {
25     private static final class Proxy implements Externalizable {
26         private static final long serialVersionUID = 1L;
27
28         private ReplicatedLogEntry replicatedLogEntry;
29
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")
33         public Proxy() {
34             // For Externalizable
35         }
36
37         Proxy(final ReplicatedLogEntry replicatedLogEntry) {
38             this.replicatedLogEntry = replicatedLogEntry;
39         }
40
41         @Override
42         public void writeExternal(final ObjectOutput out) throws IOException {
43             out.writeLong(replicatedLogEntry.getIndex());
44             out.writeLong(replicatedLogEntry.getTerm());
45             out.writeObject(replicatedLogEntry.getData());
46         }
47
48         @Override
49         public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
50             replicatedLogEntry = new SimpleReplicatedLogEntry(in.readLong(), in.readLong(), (Payload) in.readObject());
51         }
52
53         private Object readResolve() {
54             return replicatedLogEntry;
55         }
56     }
57
58     private static final long serialVersionUID = 1L;
59
60     private final long index;
61     private final long term;
62     private final Payload payload;
63     private boolean persistencePending;
64     private final boolean migrated;
65
66     private SimpleReplicatedLogEntry(long index, long term, Payload payload, boolean migrated) {
67         this.index = index;
68         this.term = term;
69         this.payload = Preconditions.checkNotNull(payload);
70         this.migrated = migrated;
71     }
72
73     /**
74      * Constructs an instance.
75      *
76      * @param index the index
77      * @param term the term
78      * @param payload the payload
79      */
80     public SimpleReplicatedLogEntry(final long index, final long term, final Payload payload) {
81         this(index, term, payload, false);
82     }
83
84     @Deprecated
85     public static ReplicatedLogEntry createMigrated(final long index, final long term, final Payload payload) {
86         return new SimpleReplicatedLogEntry(index, term, payload, true);
87     }
88
89     @Override
90     public Payload getData() {
91         return payload;
92     }
93
94     @Override
95     public long getTerm() {
96         return term;
97     }
98
99     @Override
100     public long getIndex() {
101         return index;
102     }
103
104     @Override
105     public int size() {
106         return getData().size();
107     }
108
109     @Override
110     public boolean isPersistencePending() {
111         return persistencePending;
112     }
113
114     @Override
115     public void setPersistencePending(boolean pending) {
116         persistencePending = pending;
117     }
118
119     @Override
120     public boolean isMigrated() {
121         return migrated;
122     }
123
124     @Override
125     public Object writeReplace() {
126         return new Proxy(this);
127     }
128
129     @Override
130     public int hashCode() {
131         final int prime = 31;
132         int result = 1;
133         result = prime * result + payload.hashCode();
134         result = prime * result + (int) (index ^ index >>> 32);
135         result = prime * result + (int) (term ^ term >>> 32);
136         return result;
137     }
138
139     @Override
140     public boolean equals(Object obj) {
141         if (this == obj) {
142             return true;
143         }
144
145         if (obj == null || getClass() != obj.getClass()) {
146             return false;
147         }
148
149         SimpleReplicatedLogEntry other = (SimpleReplicatedLogEntry) obj;
150         return index == other.index && term == other.term && payload.equals(other.payload);
151     }
152
153     @Override
154     public String toString() {
155         return "SimpleReplicatedLogEntry [index=" + index + ", term=" + term + ", payload=" + payload + "]";
156     }
157 }