blob: aa5796249dc697ff275d7ccf2f05cb9885f60110 [file] [log] [blame]
Madan Jampani3d76c942015-06-29 23:37:10 -07001package org.onosproject.store.ecmap;
2
3import org.onosproject.store.Timestamp;
4import com.google.common.base.MoreObjects;
5
6/**
7 * Representation of a value in EventuallyConsistentMap.
8 *
9 * @param <V> value type
10 */
11public class MapValue<V> implements Comparable<MapValue<V>> {
12 private final Timestamp timestamp;
13 private final V value;
14
Madan Jampani43f37952015-07-02 12:54:08 -070015 /**
16 * Creates a tombstone value with the specified timestamp.
17 * @param timestamp timestamp for tombstone
18 * @return tombstone MapValue
19 *
20 * @param <U> value type
21 */
22 public static <U> MapValue<U> tombstone(Timestamp timestamp) {
23 return new MapValue<>(null, timestamp);
24 }
25
Madan Jampani3d76c942015-06-29 23:37:10 -070026 public MapValue(V value, Timestamp timestamp) {
27 this.value = value;
28 this.timestamp = timestamp;
29 }
30
31 public boolean isTombstone() {
32 return value == null;
33 }
34
35 public boolean isAlive() {
36 return value != null;
37 }
38
39 public Timestamp timestamp() {
40 return timestamp;
41 }
42
43 public V get() {
44 return value;
45 }
46
47 @Override
48 public int compareTo(MapValue<V> o) {
49 return this.timestamp.compareTo(o.timestamp);
50 }
51
52 public boolean isNewerThan(MapValue<V> other) {
53 return timestamp.isNewerThan(other.timestamp);
54 }
55
56 public boolean isNewerThan(Timestamp timestamp) {
57 return timestamp.isNewerThan(timestamp);
58 }
59
60 public Digest digest() {
61 return new Digest(timestamp, isTombstone());
62 }
63
64 @Override
65 public String toString() {
66 return MoreObjects.toStringHelper(getClass())
67 .add("timestamp", timestamp)
68 .add("value", value)
69 .toString();
70 }
71
72 @SuppressWarnings("unused")
73 private MapValue() {
74 this.timestamp = null;
75 this.value = null;
76 }
77
78 /**
79 * Digest or summary of a MapValue for use during Anti-Entropy exchanges.
80 */
81 public static class Digest {
82 private final Timestamp timestamp;
83 private final boolean isTombstone;
84
85 public Digest(Timestamp timestamp, boolean isTombstone) {
86 this.timestamp = timestamp;
87 this.isTombstone = isTombstone;
88 }
89
90 public Timestamp timestamp() {
91 return timestamp;
92 }
93
94 public boolean isTombstone() {
95 return isTombstone;
96 }
97
98 public boolean isNewerThan(Digest other) {
99 return timestamp.isNewerThan(other.timestamp);
100 }
101
102 @Override
103 public String toString() {
104 return MoreObjects.toStringHelper(getClass())
105 .add("timestamp", timestamp)
106 .add("isTombstone", isTombstone)
107 .toString();
108 }
109 }
110}