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 | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 16 | package org.onosproject.store.ecmap; |
| 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 | |
| 23 | /** |
| 24 | * Representation of a value in EventuallyConsistentMap. |
| 25 | * |
| 26 | * @param <V> value type |
| 27 | */ |
| 28 | public class MapValue<V> implements Comparable<MapValue<V>> { |
| 29 | private final Timestamp timestamp; |
| 30 | private final V value; |
| 31 | |
Madan Jampani | 43f3795 | 2015-07-02 12:54:08 -0700 | [diff] [blame] | 32 | /** |
| 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 Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 43 | 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 Jampani | dbe6703 | 2015-07-08 22:48:09 -0700 | [diff] [blame] | 74 | return this.timestamp.isNewerThan(timestamp); |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | public Digest digest() { |
| 78 | return new Digest(timestamp, isTombstone()); |
| 79 | } |
| 80 | |
| 81 | @Override |
Madan Jampani | dbe6703 | 2015-07-08 22:48:09 -0700 | [diff] [blame] | 82 | public int hashCode() { |
| 83 | return Objects.hashCode(timestamp, value); |
| 84 | } |
| 85 | |
Madan Jampani | 92c64eb | 2015-07-23 15:37:07 -0700 | [diff] [blame] | 86 | @SuppressWarnings("unchecked") |
Madan Jampani | dbe6703 | 2015-07-08 22:48:09 -0700 | [diff] [blame] | 87 | @Override |
| 88 | public boolean equals(Object other) { |
| 89 | if (other instanceof MapValue) { |
Madan Jampani | 92c64eb | 2015-07-23 15:37:07 -0700 | [diff] [blame] | 90 | MapValue<V> that = (MapValue<V>) other; |
Madan Jampani | dbe6703 | 2015-07-08 22:48:09 -0700 | [diff] [blame] | 91 | return Objects.equal(this.timestamp, that.timestamp) && |
| 92 | Objects.equal(this.value, that.value); |
| 93 | } |
| 94 | return false; |
| 95 | } |
| 96 | |
| 97 | @Override |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 98 | 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 Jampani | dbe6703 | 2015-07-08 22:48:09 -0700 | [diff] [blame] | 136 | 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 Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 151 | public String toString() { |
| 152 | return MoreObjects.toStringHelper(getClass()) |
| 153 | .add("timestamp", timestamp) |
| 154 | .add("isTombstone", isTombstone) |
| 155 | .toString(); |
| 156 | } |
| 157 | } |
| 158 | } |