Madan Jampani | 2546111 | 2015-02-17 14:17:29 -0800 | [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 | */ |
| 16 | |
Madan Jampani | 393e0f0 | 2015-02-12 07:35:39 +0530 | [diff] [blame] | 17 | package org.onosproject.store.service; |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 18 | |
| 19 | import java.util.Collection; |
Madan Jampani | e106522 | 2015-08-17 16:21:51 -0700 | [diff] [blame] | 20 | import java.util.Map; |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 21 | import java.util.Map.Entry; |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 22 | import java.util.Set; |
Madan Jampani | 346d4f5 | 2015-05-04 11:09:39 -0700 | [diff] [blame] | 23 | import java.util.function.BiFunction; |
| 24 | import java.util.function.Function; |
| 25 | import java.util.function.Predicate; |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 26 | |
| 27 | /** |
Madan Jampani | fa24218 | 2016-01-22 13:42:54 -0800 | [diff] [blame] | 28 | * {@code ConsistentMap} provides the same functionality as {@link AsyncConsistentMap} with |
| 29 | * the only difference that all its methods block until the corresponding operation completes. |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 30 | * |
Madan Jampani | fa24218 | 2016-01-22 13:42:54 -0800 | [diff] [blame] | 31 | * @param <K> type of key |
| 32 | * @param <V> type of value |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 33 | */ |
Madan Jampani | a090a11 | 2016-01-18 16:38:17 -0800 | [diff] [blame] | 34 | public interface ConsistentMap<K, V> extends DistributedPrimitive { |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 35 | |
| 36 | /** |
| 37 | * Returns the number of entries in the map. |
| 38 | * |
| 39 | * @return map size. |
| 40 | */ |
| 41 | int size(); |
| 42 | |
| 43 | /** |
| 44 | * Returns true if the map is empty. |
| 45 | * |
Madan Jampani | 8098405 | 2015-07-23 13:11:36 -0700 | [diff] [blame] | 46 | * @return true if map has no entries, false otherwise |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 47 | */ |
| 48 | boolean isEmpty(); |
| 49 | |
| 50 | /** |
| 51 | * Returns true if this map contains a mapping for the specified key. |
| 52 | * |
| 53 | * @param key key |
Madan Jampani | 8098405 | 2015-07-23 13:11:36 -0700 | [diff] [blame] | 54 | * @return true if map contains key, false otherwise |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 55 | */ |
| 56 | boolean containsKey(K key); |
| 57 | |
| 58 | /** |
| 59 | * Returns true if this map contains the specified value. |
| 60 | * |
| 61 | * @param value value |
| 62 | * @return true if map contains value, false otherwise. |
| 63 | */ |
| 64 | boolean containsValue(V value); |
| 65 | |
| 66 | /** |
| 67 | * Returns the value (and version) to which the specified key is mapped, or null if this |
| 68 | * map contains no mapping for the key. |
| 69 | * |
| 70 | * @param key the key whose associated value (and version) is to be returned |
| 71 | * @return the value (and version) to which the specified key is mapped, or null if |
| 72 | * this map contains no mapping for the key |
| 73 | */ |
| 74 | Versioned<V> get(K key); |
| 75 | |
| 76 | /** |
Madan Jampani | 346d4f5 | 2015-05-04 11:09:39 -0700 | [diff] [blame] | 77 | * If the specified key is not already associated with a value (or is mapped to null), |
| 78 | * attempts to compute its value using the given mapping function and enters it into |
| 79 | * this map unless null. |
| 80 | * |
| 81 | * @param key key with which the specified value is to be associated |
| 82 | * @param mappingFunction the function to compute a value |
| 83 | * @return the current (existing or computed) value associated with the specified key, |
| 84 | * or null if the computed value is null. Method throws {@code ConsistentMapException.ConcurrentModification} |
| 85 | * if a concurrent modification of map is detected |
| 86 | */ |
| 87 | Versioned<V> computeIfAbsent(K key, |
| 88 | Function<? super K, ? extends V> mappingFunction); |
| 89 | |
| 90 | /** |
| 91 | * Attempts to compute a mapping for the specified key and its current mapped value (or |
| 92 | * null if there is no current mapping). |
| 93 | * If the computed value is null, the current mapping will be removed from the map. |
| 94 | * |
| 95 | * @param key key with which the specified value is to be associated |
| 96 | * @param remappingFunction the function to compute a value |
| 97 | * @return the new value associated with the specified key, or null if none. |
| 98 | * This method throws {@code ConsistentMapException.ConcurrentModification} |
| 99 | * if a concurrent modification of map is detected |
| 100 | */ |
| 101 | Versioned<V> compute(K key, |
| 102 | BiFunction<? super K, ? super V, ? extends V> remappingFunction); |
| 103 | |
| 104 | /** |
| 105 | * If the value for the specified key is present and non-null, attempts to compute a new |
| 106 | * mapping given the key and its current mapped value. |
| 107 | * If the computed value is null, the current mapping will be removed from the map. |
| 108 | * |
| 109 | * @param key key with which the specified value is to be associated |
| 110 | * @param remappingFunction the function to compute a value |
| 111 | * @return the new value associated with the specified key, or null if none. |
| 112 | * This method throws {@code ConsistentMapException.ConcurrentModification} |
| 113 | * if a concurrent modification of map is detected |
| 114 | */ |
| 115 | Versioned<V> computeIfPresent(K key, |
| 116 | BiFunction<? super K, ? super V, ? extends V> remappingFunction); |
| 117 | |
| 118 | /** |
| 119 | * If the value for the specified key satisfies a condition, attempts to compute a new |
| 120 | * mapping given the key and its current mapped value. |
| 121 | * If the computed value is null, the current mapping will be removed from the map. |
| 122 | * |
| 123 | * @param key key with which the specified value is to be associated |
| 124 | * @param condition condition that should evaluate to true for the computation to proceed |
| 125 | * @param remappingFunction the function to compute a value |
| 126 | * @return the new value associated with the specified key, or the old value if condition evaluates to false. |
| 127 | * This method throws {@code ConsistentMapException.ConcurrentModification} if a concurrent |
| 128 | * modification of map is detected |
| 129 | */ |
| 130 | Versioned<V> computeIf(K key, |
| 131 | Predicate<? super V> condition, |
| 132 | BiFunction<? super K, ? super V, ? extends V> remappingFunction); |
| 133 | |
| 134 | /** |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 135 | * Associates the specified value with the specified key in this map (optional operation). |
| 136 | * If the map previously contained a mapping for the key, the old value is replaced by the |
| 137 | * specified value. |
| 138 | * |
| 139 | * @param key key with which the specified value is to be associated |
| 140 | * @param value value to be associated with the specified key |
| 141 | * @return the previous value (and version) associated with key, or null if there was |
| 142 | * no mapping for key. |
| 143 | */ |
| 144 | Versioned<V> put(K key, V value); |
| 145 | |
| 146 | /** |
Madan Jampani | 346d4f5 | 2015-05-04 11:09:39 -0700 | [diff] [blame] | 147 | * Associates the specified value with the specified key in this map (optional operation). |
| 148 | * If the map previously contained a mapping for the key, the old value is replaced by the |
| 149 | * specified value. |
| 150 | * |
| 151 | * @param key key with which the specified value is to be associated |
| 152 | * @param value value to be associated with the specified key |
| 153 | * @return new value. |
| 154 | */ |
| 155 | Versioned<V> putAndGet(K key, V value); |
| 156 | |
| 157 | /** |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 158 | * Removes the mapping for a key from this map if it is present (optional operation). |
| 159 | * |
| 160 | * @param key key whose value is to be removed from the map |
| 161 | * @return the value (and version) to which this map previously associated the key, |
| 162 | * or null if the map contained no mapping for the key. |
| 163 | */ |
| 164 | Versioned<V> remove(K key); |
| 165 | |
| 166 | /** |
| 167 | * Removes all of the mappings from this map (optional operation). |
| 168 | * The map will be empty after this call returns. |
| 169 | */ |
| 170 | void clear(); |
| 171 | |
| 172 | /** |
| 173 | * Returns a Set view of the keys contained in this map. |
| 174 | * This method differs from the behavior of java.util.Map.keySet() in that |
| 175 | * what is returned is a unmodifiable snapshot view of the keys in the ConsistentMap. |
| 176 | * Attempts to modify the returned set, whether direct or via its iterator, |
| 177 | * result in an UnsupportedOperationException. |
| 178 | * |
| 179 | * @return a set of the keys contained in this map |
| 180 | */ |
| 181 | Set<K> keySet(); |
| 182 | |
| 183 | /** |
| 184 | * Returns the collection of values (and associated versions) contained in this map. |
| 185 | * This method differs from the behavior of java.util.Map.values() in that |
| 186 | * what is returned is a unmodifiable snapshot view of the values in the ConsistentMap. |
| 187 | * Attempts to modify the returned collection, whether direct or via its iterator, |
| 188 | * result in an UnsupportedOperationException. |
| 189 | * |
| 190 | * @return a collection of the values (and associated versions) contained in this map |
| 191 | */ |
| 192 | Collection<Versioned<V>> values(); |
| 193 | |
| 194 | /** |
| 195 | * Returns the set of entries contained in this map. |
| 196 | * This method differs from the behavior of java.util.Map.entrySet() in that |
| 197 | * what is returned is a unmodifiable snapshot view of the entries in the ConsistentMap. |
| 198 | * Attempts to modify the returned set, whether direct or via its iterator, |
| 199 | * result in an UnsupportedOperationException. |
| 200 | * |
| 201 | * @return set of entries contained in this map. |
| 202 | */ |
| 203 | Set<Entry<K, Versioned<V>>> entrySet(); |
| 204 | |
| 205 | /** |
| 206 | * If the specified key is not already associated with a value |
| 207 | * associates it with the given value and returns null, else returns the current value. |
| 208 | * |
| 209 | * @param key key with which the specified value is to be associated |
| 210 | * @param value value to be associated with the specified key |
| 211 | * @return the previous value associated with the specified key or null |
| 212 | * if key does not already mapped to a value. |
| 213 | */ |
| 214 | Versioned<V> putIfAbsent(K key, V value); |
| 215 | |
| 216 | /** |
| 217 | * Removes the entry for the specified key only if it is currently |
| 218 | * mapped to the specified value. |
| 219 | * |
| 220 | * @param key key with which the specified value is associated |
| 221 | * @param value value expected to be associated with the specified key |
| 222 | * @return true if the value was removed |
| 223 | */ |
| 224 | boolean remove(K key, V value); |
| 225 | |
| 226 | /** |
| 227 | * Removes the entry for the specified key only if its current |
| 228 | * version in the map is equal to the specified version. |
| 229 | * |
| 230 | * @param key key with which the specified version is associated |
| 231 | * @param version version expected to be associated with the specified key |
| 232 | * @return true if the value was removed |
| 233 | */ |
| 234 | boolean remove(K key, long version); |
| 235 | |
| 236 | /** |
Jihwan Kim | 9887ad9 | 2015-12-12 00:23:57 +0900 | [diff] [blame] | 237 | * Replaces the entry for the specified key only if there is any value |
| 238 | * which associated with specified key. |
| 239 | * |
| 240 | * @param key key with which the specified value is associated |
| 241 | * @param value value expected to be associated with the specified key |
| 242 | * @return the previous value associated with the specified key or null |
| 243 | */ |
| 244 | Versioned<V> replace(K key, V value); |
| 245 | |
| 246 | /** |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 247 | * Replaces the entry for the specified key only if currently mapped |
| 248 | * to the specified value. |
| 249 | * |
| 250 | * @param key key with which the specified value is associated |
| 251 | * @param oldValue value expected to be associated with the specified key |
| 252 | * @param newValue value to be associated with the specified key |
| 253 | * @return true if the value was replaced |
| 254 | */ |
| 255 | boolean replace(K key, V oldValue, V newValue); |
| 256 | |
| 257 | /** |
| 258 | * Replaces the entry for the specified key only if it is currently mapped to the |
| 259 | * specified version. |
| 260 | * |
| 261 | * @param key key key with which the specified value is associated |
| 262 | * @param oldVersion version expected to be associated with the specified key |
| 263 | * @param newValue value to be associated with the specified key |
| 264 | * @return true if the value was replaced |
| 265 | */ |
| 266 | boolean replace(K key, long oldVersion, V newValue); |
Madan Jampani | 346d4f5 | 2015-05-04 11:09:39 -0700 | [diff] [blame] | 267 | |
| 268 | /** |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 269 | * Registers the specified listener to be notified whenever the map is updated. |
| 270 | * |
| 271 | * @param listener listener to notify about map events |
| 272 | */ |
| 273 | void addListener(MapEventListener<K, V> listener); |
| 274 | |
| 275 | /** |
| 276 | * Unregisters the specified listener such that it will no longer |
| 277 | * receive map change notifications. |
| 278 | * |
| 279 | * @param listener listener to unregister |
| 280 | */ |
| 281 | void removeListener(MapEventListener<K, V> listener); |
Madan Jampani | e106522 | 2015-08-17 16:21:51 -0700 | [diff] [blame] | 282 | |
| 283 | /** |
| 284 | * Returns a java.util.Map instance backed by this ConsistentMap. |
| 285 | * @return java.util.Map |
| 286 | */ |
| 287 | Map<K, V> asJavaMap(); |
Madan Jampani | dfde6ba | 2016-01-13 21:36:09 -0800 | [diff] [blame] | 288 | } |