blob: bb69b4726d5d3023971b772a218605f2909374bf [file] [log] [blame]
Madan Jampanidbe67032015-07-08 22:48:09 -07001/*
2 * Copyright 2015 Open Networking Laboratory
3 *
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 Jampani3d76c942015-06-29 23:37:10 -070016package org.onosproject.store.ecmap;
17
18import org.onosproject.store.Timestamp;
Madan Jampanidbe67032015-07-08 22:48:09 -070019
Madan Jampani3d76c942015-06-29 23:37:10 -070020import com.google.common.base.MoreObjects;
Madan Jampanidbe67032015-07-08 22:48:09 -070021import com.google.common.base.Objects;
Madan Jampani3d76c942015-06-29 23:37:10 -070022
23/**
24 * Representation of a value in EventuallyConsistentMap.
25 *
26 * @param <V> value type
27 */
28public class MapValue<V> implements Comparable<MapValue<V>> {
29 private final Timestamp timestamp;
30 private final V value;
31
Madan Jampani43f37952015-07-02 12:54:08 -070032 /**
33 * Creates a tombstone value with the specified timestamp.
34 * @param timestamp timestamp for tombstone
35 * @return tombstone MapValue
36 *
37 * @param <U> value type
38 */
39 public static <U> MapValue<U> tombstone(Timestamp timestamp) {
40 return new MapValue<>(null, timestamp);
41 }
42
Madan Jampani3d76c942015-06-29 23:37:10 -070043 public MapValue(V value, Timestamp timestamp) {
44 this.value = value;
45 this.timestamp = timestamp;
46 }
47
48 public boolean isTombstone() {
49 return value == null;
50 }
51
52 public boolean isAlive() {
53 return value != null;
54 }
55
56 public Timestamp timestamp() {
57 return timestamp;
58 }
59
60 public V get() {
61 return value;
62 }
63
64 @Override
65 public int compareTo(MapValue<V> o) {
66 return this.timestamp.compareTo(o.timestamp);
67 }
68
69 public boolean isNewerThan(MapValue<V> other) {
70 return timestamp.isNewerThan(other.timestamp);
71 }
72
73 public boolean isNewerThan(Timestamp timestamp) {
Madan Jampanidbe67032015-07-08 22:48:09 -070074 return this.timestamp.isNewerThan(timestamp);
Madan Jampani3d76c942015-06-29 23:37:10 -070075 }
76
77 public Digest digest() {
78 return new Digest(timestamp, isTombstone());
79 }
80
81 @Override
Madan Jampanidbe67032015-07-08 22:48:09 -070082 public int hashCode() {
83 return Objects.hashCode(timestamp, value);
84 }
85
Madan Jampani92c64eb2015-07-23 15:37:07 -070086 @SuppressWarnings("unchecked")
Madan Jampanidbe67032015-07-08 22:48:09 -070087 @Override
88 public boolean equals(Object other) {
89 if (other instanceof MapValue) {
Madan Jampani92c64eb2015-07-23 15:37:07 -070090 MapValue<V> that = (MapValue<V>) other;
Madan Jampanidbe67032015-07-08 22:48:09 -070091 return Objects.equal(this.timestamp, that.timestamp) &&
92 Objects.equal(this.value, that.value);
93 }
94 return false;
95 }
96
97 @Override
Madan Jampani3d76c942015-06-29 23:37:10 -070098 public String toString() {
99 return MoreObjects.toStringHelper(getClass())
100 .add("timestamp", timestamp)
101 .add("value", value)
102 .toString();
103 }
104
105 @SuppressWarnings("unused")
106 private MapValue() {
107 this.timestamp = null;
108 this.value = null;
109 }
110
111 /**
112 * Digest or summary of a MapValue for use during Anti-Entropy exchanges.
113 */
114 public static class Digest {
115 private final Timestamp timestamp;
116 private final boolean isTombstone;
117
118 public Digest(Timestamp timestamp, boolean isTombstone) {
119 this.timestamp = timestamp;
120 this.isTombstone = isTombstone;
121 }
122
123 public Timestamp timestamp() {
124 return timestamp;
125 }
126
127 public boolean isTombstone() {
128 return isTombstone;
129 }
130
131 public boolean isNewerThan(Digest other) {
132 return timestamp.isNewerThan(other.timestamp);
133 }
134
135 @Override
Madan Jampanidbe67032015-07-08 22:48:09 -0700136 public int hashCode() {
137 return Objects.hashCode(timestamp, isTombstone);
138 }
139
140 @Override
141 public boolean equals(Object other) {
142 if (other instanceof Digest) {
143 Digest that = (Digest) other;
144 return Objects.equal(this.timestamp, that.timestamp) &&
145 Objects.equal(this.isTombstone, that.isTombstone);
146 }
147 return false;
148 }
149
150 @Override
Madan Jampani3d76c942015-06-29 23:37:10 -0700151 public String toString() {
152 return MoreObjects.toStringHelper(getClass())
153 .add("timestamp", timestamp)
154 .add("isTombstone", isTombstone)
155 .toString();
156 }
157 }
158}