Madan Jampani | dbe6703 | 2015-07-08 22:48:09 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Madan Jampani | dbe6703 | 2015-07-08 22:48:09 -0700 | [diff] [blame] | 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 Jampani | f4c8850 | 2016-01-21 12:35:36 -0800 | [diff] [blame] | 16 | package org.onosproject.store.primitives.impl; |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 17 | |
| 18 | import org.onosproject.store.Timestamp; |
Madan Jampani | dbe6703 | 2015-07-08 22:48:09 -0700 | [diff] [blame] | 19 | |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 20 | import com.google.common.base.MoreObjects; |
Madan Jampani | dbe6703 | 2015-07-08 22:48:09 -0700 | [diff] [blame] | 21 | import com.google.common.base.Objects; |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 22 | |
Brian O'Connor | 777b0ae | 2015-12-08 19:38:18 -0800 | [diff] [blame] | 23 | import static com.google.common.base.Preconditions.checkNotNull; |
| 24 | |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 25 | /** |
| 26 | * Representation of a value in EventuallyConsistentMap. |
| 27 | * |
| 28 | * @param <V> value type |
| 29 | */ |
| 30 | public class MapValue<V> implements Comparable<MapValue<V>> { |
| 31 | private final Timestamp timestamp; |
| 32 | private final V value; |
Madan Jampani | 29f52a3 | 2016-04-18 15:20:52 -0700 | [diff] [blame^] | 33 | private long creationTime; |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 34 | |
Madan Jampani | 43f3795 | 2015-07-02 12:54:08 -0700 | [diff] [blame] | 35 | /** |
| 36 | * Creates a tombstone value with the specified timestamp. |
| 37 | * @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 Jampani | 29f52a3 | 2016-04-18 15:20:52 -0700 | [diff] [blame^] | 43 | return new MapValue<>(null, timestamp, System.currentTimeMillis()); |
Madan Jampani | 43f3795 | 2015-07-02 12:54:08 -0700 | [diff] [blame] | 44 | } |
| 45 | |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 46 | public MapValue(V value, Timestamp timestamp) { |
Madan Jampani | 29f52a3 | 2016-04-18 15:20:52 -0700 | [diff] [blame^] | 47 | this(value, timestamp, System.currentTimeMillis()); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Constructor. |
| 52 | * |
| 53 | * @param value value |
| 54 | * @param timestamp value timestamp. |
| 55 | * @param creationTime the system time (on local instance) of construction |
| 56 | */ |
| 57 | public MapValue(V value, Timestamp timestamp, long creationTime) { |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 58 | this.value = value; |
Brian O'Connor | 777b0ae | 2015-12-08 19:38:18 -0800 | [diff] [blame] | 59 | this.timestamp = checkNotNull(timestamp, "Timestamp cannot be null"); |
Madan Jampani | 29f52a3 | 2016-04-18 15:20:52 -0700 | [diff] [blame^] | 60 | this.creationTime = creationTime; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Creates a copy of MapValue. |
| 65 | * <p> |
| 66 | * The copy will have an updated creation time corresponding to when the copy was constructed. |
| 67 | * |
| 68 | * @return MapValue copy |
| 69 | */ |
| 70 | public MapValue<V> copy() { |
| 71 | return new MapValue<>(this.value, this.timestamp, System.currentTimeMillis()); |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | public boolean isTombstone() { |
| 75 | return value == null; |
| 76 | } |
| 77 | |
| 78 | public boolean isAlive() { |
| 79 | return value != null; |
| 80 | } |
| 81 | |
| 82 | public Timestamp timestamp() { |
| 83 | return timestamp; |
| 84 | } |
| 85 | |
| 86 | public V get() { |
| 87 | return value; |
| 88 | } |
| 89 | |
Madan Jampani | 29f52a3 | 2016-04-18 15:20:52 -0700 | [diff] [blame^] | 90 | public long creationTime() { |
| 91 | return creationTime; |
| 92 | } |
| 93 | |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 94 | @Override |
| 95 | public int compareTo(MapValue<V> o) { |
| 96 | return this.timestamp.compareTo(o.timestamp); |
| 97 | } |
| 98 | |
| 99 | public boolean isNewerThan(MapValue<V> other) { |
Brian O'Connor | 777b0ae | 2015-12-08 19:38:18 -0800 | [diff] [blame] | 100 | if (other == null) { |
| 101 | return true; |
| 102 | } |
| 103 | return this.timestamp.isNewerThan(other.timestamp); |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | public boolean isNewerThan(Timestamp timestamp) { |
Madan Jampani | dbe6703 | 2015-07-08 22:48:09 -0700 | [diff] [blame] | 107 | return this.timestamp.isNewerThan(timestamp); |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | public Digest digest() { |
| 111 | return new Digest(timestamp, isTombstone()); |
| 112 | } |
| 113 | |
| 114 | @Override |
Madan Jampani | dbe6703 | 2015-07-08 22:48:09 -0700 | [diff] [blame] | 115 | public int hashCode() { |
| 116 | return Objects.hashCode(timestamp, value); |
| 117 | } |
| 118 | |
Madan Jampani | 92c64eb | 2015-07-23 15:37:07 -0700 | [diff] [blame] | 119 | @SuppressWarnings("unchecked") |
Madan Jampani | dbe6703 | 2015-07-08 22:48:09 -0700 | [diff] [blame] | 120 | @Override |
| 121 | public boolean equals(Object other) { |
| 122 | if (other instanceof MapValue) { |
Madan Jampani | 92c64eb | 2015-07-23 15:37:07 -0700 | [diff] [blame] | 123 | MapValue<V> that = (MapValue<V>) other; |
Madan Jampani | dbe6703 | 2015-07-08 22:48:09 -0700 | [diff] [blame] | 124 | return Objects.equal(this.timestamp, that.timestamp) && |
| 125 | Objects.equal(this.value, that.value); |
| 126 | } |
| 127 | return false; |
| 128 | } |
| 129 | |
| 130 | @Override |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 131 | public String toString() { |
| 132 | return MoreObjects.toStringHelper(getClass()) |
| 133 | .add("timestamp", timestamp) |
| 134 | .add("value", value) |
| 135 | .toString(); |
| 136 | } |
| 137 | |
| 138 | @SuppressWarnings("unused") |
| 139 | private MapValue() { |
| 140 | this.timestamp = null; |
| 141 | this.value = null; |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Digest or summary of a MapValue for use during Anti-Entropy exchanges. |
| 146 | */ |
| 147 | public static class Digest { |
| 148 | private final Timestamp timestamp; |
| 149 | private final boolean isTombstone; |
| 150 | |
| 151 | public Digest(Timestamp timestamp, boolean isTombstone) { |
| 152 | this.timestamp = timestamp; |
| 153 | this.isTombstone = isTombstone; |
| 154 | } |
| 155 | |
| 156 | public Timestamp timestamp() { |
| 157 | return timestamp; |
| 158 | } |
| 159 | |
| 160 | public boolean isTombstone() { |
| 161 | return isTombstone; |
| 162 | } |
| 163 | |
| 164 | public boolean isNewerThan(Digest other) { |
| 165 | return timestamp.isNewerThan(other.timestamp); |
| 166 | } |
| 167 | |
| 168 | @Override |
Madan Jampani | dbe6703 | 2015-07-08 22:48:09 -0700 | [diff] [blame] | 169 | public int hashCode() { |
| 170 | return Objects.hashCode(timestamp, isTombstone); |
| 171 | } |
| 172 | |
| 173 | @Override |
| 174 | public boolean equals(Object other) { |
| 175 | if (other instanceof Digest) { |
| 176 | Digest that = (Digest) other; |
| 177 | return Objects.equal(this.timestamp, that.timestamp) && |
| 178 | Objects.equal(this.isTombstone, that.isTombstone); |
| 179 | } |
| 180 | return false; |
| 181 | } |
| 182 | |
| 183 | @Override |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 184 | public String toString() { |
| 185 | return MoreObjects.toStringHelper(getClass()) |
| 186 | .add("timestamp", timestamp) |
| 187 | .add("isTombstone", isTombstone) |
| 188 | .toString(); |
| 189 | } |
| 190 | } |
| 191 | } |