Madan Jampani | dbe6703 | 2015-07-08 22:48:09 -0700 | [diff] [blame] | 1 | /* |
| 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 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; |
| 33 | |
Madan Jampani | 43f3795 | 2015-07-02 12:54:08 -0700 | [diff] [blame] | 34 | /** |
| 35 | * Creates a tombstone value with the specified timestamp. |
| 36 | * @param timestamp timestamp for tombstone |
| 37 | * @return tombstone MapValue |
| 38 | * |
| 39 | * @param <U> value type |
| 40 | */ |
| 41 | public static <U> MapValue<U> tombstone(Timestamp timestamp) { |
| 42 | return new MapValue<>(null, timestamp); |
| 43 | } |
| 44 | |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 45 | public MapValue(V value, Timestamp timestamp) { |
| 46 | this.value = value; |
Brian O'Connor | 777b0ae | 2015-12-08 19:38:18 -0800 | [diff] [blame] | 47 | this.timestamp = checkNotNull(timestamp, "Timestamp cannot be null"); |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | public boolean isTombstone() { |
| 51 | return value == null; |
| 52 | } |
| 53 | |
| 54 | public boolean isAlive() { |
| 55 | return value != null; |
| 56 | } |
| 57 | |
| 58 | public Timestamp timestamp() { |
| 59 | return timestamp; |
| 60 | } |
| 61 | |
| 62 | public V get() { |
| 63 | return value; |
| 64 | } |
| 65 | |
| 66 | @Override |
| 67 | public int compareTo(MapValue<V> o) { |
| 68 | return this.timestamp.compareTo(o.timestamp); |
| 69 | } |
| 70 | |
| 71 | public boolean isNewerThan(MapValue<V> other) { |
Brian O'Connor | 777b0ae | 2015-12-08 19:38:18 -0800 | [diff] [blame] | 72 | if (other == null) { |
| 73 | return true; |
| 74 | } |
| 75 | return this.timestamp.isNewerThan(other.timestamp); |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | public boolean isNewerThan(Timestamp timestamp) { |
Madan Jampani | dbe6703 | 2015-07-08 22:48:09 -0700 | [diff] [blame] | 79 | return this.timestamp.isNewerThan(timestamp); |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | public Digest digest() { |
| 83 | return new Digest(timestamp, isTombstone()); |
| 84 | } |
| 85 | |
| 86 | @Override |
Madan Jampani | dbe6703 | 2015-07-08 22:48:09 -0700 | [diff] [blame] | 87 | public int hashCode() { |
| 88 | return Objects.hashCode(timestamp, value); |
| 89 | } |
| 90 | |
Madan Jampani | 92c64eb | 2015-07-23 15:37:07 -0700 | [diff] [blame] | 91 | @SuppressWarnings("unchecked") |
Madan Jampani | dbe6703 | 2015-07-08 22:48:09 -0700 | [diff] [blame] | 92 | @Override |
| 93 | public boolean equals(Object other) { |
| 94 | if (other instanceof MapValue) { |
Madan Jampani | 92c64eb | 2015-07-23 15:37:07 -0700 | [diff] [blame] | 95 | MapValue<V> that = (MapValue<V>) other; |
Madan Jampani | dbe6703 | 2015-07-08 22:48:09 -0700 | [diff] [blame] | 96 | return Objects.equal(this.timestamp, that.timestamp) && |
| 97 | Objects.equal(this.value, that.value); |
| 98 | } |
| 99 | return false; |
| 100 | } |
| 101 | |
| 102 | @Override |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 103 | public String toString() { |
| 104 | return MoreObjects.toStringHelper(getClass()) |
| 105 | .add("timestamp", timestamp) |
| 106 | .add("value", value) |
| 107 | .toString(); |
| 108 | } |
| 109 | |
| 110 | @SuppressWarnings("unused") |
| 111 | private MapValue() { |
| 112 | this.timestamp = null; |
| 113 | this.value = null; |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Digest or summary of a MapValue for use during Anti-Entropy exchanges. |
| 118 | */ |
| 119 | public static class Digest { |
| 120 | private final Timestamp timestamp; |
| 121 | private final boolean isTombstone; |
| 122 | |
| 123 | public Digest(Timestamp timestamp, boolean isTombstone) { |
| 124 | this.timestamp = timestamp; |
| 125 | this.isTombstone = isTombstone; |
| 126 | } |
| 127 | |
| 128 | public Timestamp timestamp() { |
| 129 | return timestamp; |
| 130 | } |
| 131 | |
| 132 | public boolean isTombstone() { |
| 133 | return isTombstone; |
| 134 | } |
| 135 | |
| 136 | public boolean isNewerThan(Digest other) { |
| 137 | return timestamp.isNewerThan(other.timestamp); |
| 138 | } |
| 139 | |
| 140 | @Override |
Madan Jampani | dbe6703 | 2015-07-08 22:48:09 -0700 | [diff] [blame] | 141 | public int hashCode() { |
| 142 | return Objects.hashCode(timestamp, isTombstone); |
| 143 | } |
| 144 | |
| 145 | @Override |
| 146 | public boolean equals(Object other) { |
| 147 | if (other instanceof Digest) { |
| 148 | Digest that = (Digest) other; |
| 149 | return Objects.equal(this.timestamp, that.timestamp) && |
| 150 | Objects.equal(this.isTombstone, that.isTombstone); |
| 151 | } |
| 152 | return false; |
| 153 | } |
| 154 | |
| 155 | @Override |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 156 | public String toString() { |
| 157 | return MoreObjects.toStringHelper(getClass()) |
| 158 | .add("timestamp", timestamp) |
| 159 | .add("isTombstone", isTombstone) |
| 160 | .toString(); |
| 161 | } |
| 162 | } |
| 163 | } |