Bug 7391: Fix out-of-order LeaderStateChange events
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / main / java / org / opendaylight / controller / cluster / raft / ReplicatedLogImplEntry.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;
10
11 import com.google.common.base.Preconditions;
12 import java.io.Serializable;
13 import org.opendaylight.controller.cluster.raft.protobuff.client.messages.Payload;
14
15 /**
16  * A {@link ReplicatedLogEntry} implementation.
17  *
18  * @deprecated Use {@link org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry} instead.
19  */
20 @Deprecated
21 public class ReplicatedLogImplEntry implements ReplicatedLogEntry, Serializable {
22     private static final long serialVersionUID = -9085798014576489130L;
23
24     private final long index;
25     private final long term;
26     private final Payload payload;
27     private transient boolean persistencePending = false;
28
29     /**
30      * Constructs an instance.
31      *
32      * @param index the index
33      * @param term the term
34      * @param payload the payload
35      */
36     public ReplicatedLogImplEntry(final long index, final long term, final Payload payload) {
37         this.index = index;
38         this.term = term;
39         this.payload = Preconditions.checkNotNull(payload);
40     }
41
42     @Override
43     public Payload getData() {
44         return payload;
45     }
46
47     @Override
48     public long getTerm() {
49         return term;
50     }
51
52     @Override
53     public long getIndex() {
54         return index;
55     }
56
57     @Override
58     public int size() {
59         return getData().size();
60     }
61
62     private Object readResolve() {
63         return org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry.createMigrated(
64                 index, term, payload);
65     }
66
67     @Override
68     public boolean isPersistencePending() {
69         return persistencePending;
70     }
71
72     @Override
73     public void setPersistencePending(boolean pending) {
74         persistencePending = pending;
75     }
76
77     @Override
78     public int hashCode() {
79         final int prime = 31;
80         int result = 1;
81         result = prime * result + payload.hashCode();
82         result = prime * result + (int) (index ^ index >>> 32);
83         result = prime * result + (int) (term ^ term >>> 32);
84         return result;
85     }
86
87     @Override
88     public boolean equals(Object obj) {
89         if (this == obj) {
90             return true;
91         }
92
93         if (obj == null) {
94             return false;
95         }
96
97         if (getClass() != obj.getClass()) {
98             return false;
99         }
100
101         ReplicatedLogImplEntry other = (ReplicatedLogImplEntry) obj;
102         if (payload == null) {
103             if (other.payload != null) {
104                 return false;
105             }
106         } else if (!payload.equals(other.payload)) {
107             return false;
108         }
109
110         if (index != other.index) {
111             return false;
112         }
113
114         if (term != other.term) {
115             return false;
116         }
117
118         return true;
119     }
120
121     @Override
122     public String toString() {
123         return "Entry{index=" + index + ", term=" + term + '}';
124     }
125 }