blob: 3175963a90c4aff95925e2f195448e877128b227 [file] [log] [blame]
Madan Jampanidbe67032015-07-08 22:48:09 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Madan Jampanidbe67032015-07-08 22:48:09 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Madan Jampanif4c88502016-01-21 12:35:36 -080016package org.onosproject.store.primitives.impl;
Madan Jampani3d76c942015-06-29 23:37:10 -070017
Madan Jampani3d76c942015-06-29 23:37:10 -070018import com.google.common.base.MoreObjects;
Madan Jampanidbe67032015-07-08 22:48:09 -070019import com.google.common.base.Objects;
Jordan Halterman00e92da2018-05-22 23:05:52 -070020import org.onosproject.store.Timestamp;
Madan Jampani3d76c942015-06-29 23:37:10 -070021
Brian O'Connor777b0ae2015-12-08 19:38:18 -080022import static com.google.common.base.Preconditions.checkNotNull;
23
Madan Jampani3d76c942015-06-29 23:37:10 -070024/**
25 * Representation of a value in EventuallyConsistentMap.
26 *
27 * @param <V> value type
28 */
29public class MapValue<V> implements Comparable<MapValue<V>> {
30 private final Timestamp timestamp;
31 private final V value;
Madan Jampani29f52a32016-04-18 15:20:52 -070032 private long creationTime;
Madan Jampani3d76c942015-06-29 23:37:10 -070033
Madan Jampani43f37952015-07-02 12:54:08 -070034 /**
35 * Creates a tombstone value with the specified timestamp.
sangyun-han72fd0e92016-04-26 20:50:08 +090036 *
Madan Jampani43f37952015-07-02 12:54:08 -070037 * @param timestamp timestamp for tombstone
38 * @return tombstone MapValue
39 *
40 * @param <U> value type
41 */
42 public static <U> MapValue<U> tombstone(Timestamp timestamp) {
Madan Jampani29f52a32016-04-18 15:20:52 -070043 return new MapValue<>(null, timestamp, System.currentTimeMillis());
Madan Jampani43f37952015-07-02 12:54:08 -070044 }
45
sangyun-han72fd0e92016-04-26 20:50:08 +090046 /**
47 * Constructor automatically to create the system time of construction.
48 *
49 * @param value value
50 * @param timestamp value timestamp
51 */
Madan Jampani3d76c942015-06-29 23:37:10 -070052 public MapValue(V value, Timestamp timestamp) {
Madan Jampani29f52a32016-04-18 15:20:52 -070053 this(value, timestamp, System.currentTimeMillis());
54 }
55
56 /**
sangyun-han72fd0e92016-04-26 20:50:08 +090057 * Creates a map value using value, timestamp, and creation time.
Madan Jampani29f52a32016-04-18 15:20:52 -070058 *
59 * @param value value
60 * @param timestamp value timestamp.
61 * @param creationTime the system time (on local instance) of construction
62 */
63 public MapValue(V value, Timestamp timestamp, long creationTime) {
Madan Jampani3d76c942015-06-29 23:37:10 -070064 this.value = value;
Brian O'Connor777b0ae2015-12-08 19:38:18 -080065 this.timestamp = checkNotNull(timestamp, "Timestamp cannot be null");
Madan Jampani29f52a32016-04-18 15:20:52 -070066 this.creationTime = creationTime;
67 }
68
69 /**
70 * Creates a copy of MapValue.
71 * <p>
72 * The copy will have an updated creation time corresponding to when the copy was constructed.
73 *
74 * @return MapValue copy
75 */
76 public MapValue<V> copy() {
77 return new MapValue<>(this.value, this.timestamp, System.currentTimeMillis());
Madan Jampani3d76c942015-06-29 23:37:10 -070078 }
79
sangyun-han72fd0e92016-04-26 20:50:08 +090080 /**
81 * Tests if this value is tombstone value with the specified timestamp.
82 *
83 * @return true if this value is null, otherwise false
84 */
Madan Jampani3d76c942015-06-29 23:37:10 -070085 public boolean isTombstone() {
86 return value == null;
87 }
88
sangyun-han72fd0e92016-04-26 20:50:08 +090089 /**
90 * Tests if this value is alive.
91 *
92 * @return true if this value is not null, otherwise false
93 */
Madan Jampani3d76c942015-06-29 23:37:10 -070094 public boolean isAlive() {
95 return value != null;
96 }
97
sangyun-han72fd0e92016-04-26 20:50:08 +090098 /**
99 * Returns the timestamp of this value.
100 *
101 * @return timestamp
102 */
Madan Jampani3d76c942015-06-29 23:37:10 -0700103 public Timestamp timestamp() {
104 return timestamp;
105 }
106
sangyun-han72fd0e92016-04-26 20:50:08 +0900107 /**
108 * Returns this value.
109 *
110 * @return value
111 */
Madan Jampani3d76c942015-06-29 23:37:10 -0700112 public V get() {
113 return value;
114 }
115
sangyun-han72fd0e92016-04-26 20:50:08 +0900116 /**
117 * Returns the creation time of this value.
118 *
119 * @return creationTime
120 */
Madan Jampani29f52a32016-04-18 15:20:52 -0700121 public long creationTime() {
122 return creationTime;
123 }
124
sangyun-han72fd0e92016-04-26 20:50:08 +0900125 /**
126 * Tests if this value is newer than the specified MapValue.
127 *
128 * @param other the value to be compared
129 * @return true if this value is newer than other
130 */
Madan Jampani3d76c942015-06-29 23:37:10 -0700131 public boolean isNewerThan(MapValue<V> other) {
Brian O'Connor777b0ae2015-12-08 19:38:18 -0800132 if (other == null) {
133 return true;
134 }
135 return this.timestamp.isNewerThan(other.timestamp);
Madan Jampani3d76c942015-06-29 23:37:10 -0700136 }
137
sangyun-han72fd0e92016-04-26 20:50:08 +0900138 /**
139 * Tests if this timestamp is newer than the specified timestamp.
140 *
141 * @param timestamp timestamp to be compared
142 * @return true if this instance is newer
143 */
Madan Jampani3d76c942015-06-29 23:37:10 -0700144 public boolean isNewerThan(Timestamp timestamp) {
Madan Jampanidbe67032015-07-08 22:48:09 -0700145 return this.timestamp.isNewerThan(timestamp);
Madan Jampani3d76c942015-06-29 23:37:10 -0700146 }
147
sangyun-han72fd0e92016-04-26 20:50:08 +0900148 /**
149 * Returns summary of a MapValue for use during Anti-Entropy exchanges.
150 *
151 * @return Digest with timestamp and whether this value is null or not
152 */
Madan Jampani3d76c942015-06-29 23:37:10 -0700153 public Digest digest() {
154 return new Digest(timestamp, isTombstone());
155 }
156
157 @Override
sangyun-han72fd0e92016-04-26 20:50:08 +0900158 public int compareTo(MapValue<V> o) {
159 return this.timestamp.compareTo(o.timestamp);
160 }
161
162 @Override
Madan Jampanidbe67032015-07-08 22:48:09 -0700163 public int hashCode() {
164 return Objects.hashCode(timestamp, value);
165 }
166
Madan Jampani92c64eb2015-07-23 15:37:07 -0700167 @SuppressWarnings("unchecked")
Madan Jampanidbe67032015-07-08 22:48:09 -0700168 @Override
169 public boolean equals(Object other) {
170 if (other instanceof MapValue) {
Madan Jampani92c64eb2015-07-23 15:37:07 -0700171 MapValue<V> that = (MapValue<V>) other;
Madan Jampanidbe67032015-07-08 22:48:09 -0700172 return Objects.equal(this.timestamp, that.timestamp) &&
173 Objects.equal(this.value, that.value);
174 }
175 return false;
176 }
177
178 @Override
Madan Jampani3d76c942015-06-29 23:37:10 -0700179 public String toString() {
180 return MoreObjects.toStringHelper(getClass())
181 .add("timestamp", timestamp)
182 .add("value", value)
183 .toString();
184 }
185
186 @SuppressWarnings("unused")
187 private MapValue() {
188 this.timestamp = null;
189 this.value = null;
190 }
191
192 /**
193 * Digest or summary of a MapValue for use during Anti-Entropy exchanges.
194 */
195 public static class Digest {
196 private final Timestamp timestamp;
197 private final boolean isTombstone;
198
199 public Digest(Timestamp timestamp, boolean isTombstone) {
200 this.timestamp = timestamp;
201 this.isTombstone = isTombstone;
202 }
203
204 public Timestamp timestamp() {
205 return timestamp;
206 }
207
208 public boolean isTombstone() {
209 return isTombstone;
210 }
211
212 public boolean isNewerThan(Digest other) {
213 return timestamp.isNewerThan(other.timestamp);
214 }
215
216 @Override
Madan Jampanidbe67032015-07-08 22:48:09 -0700217 public int hashCode() {
218 return Objects.hashCode(timestamp, isTombstone);
219 }
220
221 @Override
222 public boolean equals(Object other) {
223 if (other instanceof Digest) {
224 Digest that = (Digest) other;
225 return Objects.equal(this.timestamp, that.timestamp) &&
226 Objects.equal(this.isTombstone, that.isTombstone);
227 }
228 return false;
229 }
230
231 @Override
Madan Jampani3d76c942015-06-29 23:37:10 -0700232 public String toString() {
233 return MoreObjects.toStringHelper(getClass())
234 .add("timestamp", timestamp)
235 .add("isTombstone", isTombstone)
236 .toString();
237 }
238 }
239}