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