Bug 5419: Persist log entries asycnhronously
[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 public class ReplicatedLogImplEntry implements ReplicatedLogEntry, Serializable {
19     private static final long serialVersionUID = -9085798014576489130L;
20
21     private final long index;
22     private final long term;
23     private final Payload payload;
24     private transient boolean persistencePending = false;
25
26     /**
27      * Constructs an instance.
28      *
29      * @param index the index
30      * @param term the term
31      * @param payload the payload
32      */
33     public ReplicatedLogImplEntry(final long index, final long term, final Payload payload) {
34         this.index = index;
35         this.term = term;
36         this.payload = Preconditions.checkNotNull(payload);
37     }
38
39     @Override
40     public Payload getData() {
41         return payload;
42     }
43
44     @Override
45     public long getTerm() {
46         return term;
47     }
48
49     @Override
50     public long getIndex() {
51         return index;
52     }
53
54     @Override
55     public int size() {
56         return getData().size();
57     }
58
59     @Override
60     public boolean isPersistencePending() {
61         return persistencePending;
62     }
63
64     @Override
65     public void setPersistencePending(boolean pending) {
66         persistencePending = pending;
67     }
68
69     @Override
70     public int hashCode() {
71         final int prime = 31;
72         int result = 1;
73         result = prime * result + payload.hashCode();
74         result = prime * result + (int) (index ^ index >>> 32);
75         result = prime * result + (int) (term ^ term >>> 32);
76         return result;
77     }
78
79     @Override
80     public boolean equals(Object obj) {
81         if (this == obj) {
82             return true;
83         }
84
85         if (obj == null) {
86             return false;
87         }
88
89         if (getClass() != obj.getClass()) {
90             return false;
91         }
92
93         ReplicatedLogImplEntry other = (ReplicatedLogImplEntry) obj;
94         if (payload == null) {
95             if (other.payload != null) {
96                 return false;
97             }
98         } else if (!payload.equals(other.payload)) {
99             return false;
100         }
101
102         if (index != other.index) {
103             return false;
104         }
105
106         if (term != other.term) {
107             return false;
108         }
109
110         return true;
111     }
112
113     @Override
114     public String toString() {
115         return "Entry{index=" + index + ", term=" + term + '}';
116     }
117 }