blob: 1a89c6bd93689c9af4f4165ab5ee8745204b6d62 [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
86 @Override
87 public boolean equals(Object other) {
88 if (other instanceof MapValue) {
89 MapValue<V> that = (MapValue) other;
90 return Objects.equal(this.timestamp, that.timestamp) &&
91 Objects.equal(this.value, that.value);
92 }
93 return false;
94 }
95
96 @Override
Madan Jampani3d76c942015-06-29 23:37:10 -070097 public String toString() {
98 return MoreObjects.toStringHelper(getClass())
99 .add("timestamp", timestamp)
100 .add("value", value)
101 .toString();
102 }
103
104 @SuppressWarnings("unused")
105 private MapValue() {
106 this.timestamp = null;
107 this.value = null;
108 }
109
110 /**
111 * Digest or summary of a MapValue for use during Anti-Entropy exchanges.
112 */
113 public static class Digest {
114 private final Timestamp timestamp;
115 private final boolean isTombstone;
116
117 public Digest(Timestamp timestamp, boolean isTombstone) {
118 this.timestamp = timestamp;
119 this.isTombstone = isTombstone;
120 }
121
122 public Timestamp timestamp() {
123 return timestamp;
124 }
125
126 public boolean isTombstone() {
127 return isTombstone;
128 }
129
130 public boolean isNewerThan(Digest other) {
131 return timestamp.isNewerThan(other.timestamp);
132 }
133
134 @Override
Madan Jampanidbe67032015-07-08 22:48:09 -0700135 public int hashCode() {
136 return Objects.hashCode(timestamp, isTombstone);
137 }
138
139 @Override
140 public boolean equals(Object other) {
141 if (other instanceof Digest) {
142 Digest that = (Digest) other;
143 return Objects.equal(this.timestamp, that.timestamp) &&
144 Objects.equal(this.isTombstone, that.isTombstone);
145 }
146 return false;
147 }
148
149 @Override
Madan Jampani3d76c942015-06-29 23:37:10 -0700150 public String toString() {
151 return MoreObjects.toStringHelper(getClass())
152 .add("timestamp", timestamp)
153 .add("isTombstone", isTombstone)
154 .toString();
155 }
156 }
157}