blob: 3d14a68abae2454560c281a4641bc6df017a2bc6 [file] [log] [blame]
Madan Jampani25461112015-02-17 14:17:29 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Madan Jampani25461112015-02-17 14:17:29 -08003 *
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 Jampani393e0f02015-02-12 07:35:39 +053017package org.onosproject.store.service;
Madan Jampani09342702015-02-05 23:32:40 -080018
19import java.util.Collection;
Madan Jampanie1065222015-08-17 16:21:51 -070020import java.util.Map;
Madan Jampani09342702015-02-05 23:32:40 -080021import java.util.Map.Entry;
Madan Jampanif1b8e172015-03-23 11:42:02 -070022import java.util.Set;
Madan Jampani0463cf92016-05-04 14:46:08 -070023import java.util.concurrent.Executor;
Madan Jampani346d4f52015-05-04 11:09:39 -070024import java.util.function.BiFunction;
25import java.util.function.Function;
26import java.util.function.Predicate;
Madan Jampani09342702015-02-05 23:32:40 -080027
Madan Jampani0463cf92016-05-04 14:46:08 -070028import com.google.common.util.concurrent.MoreExecutors;
29
Madan Jampani09342702015-02-05 23:32:40 -080030/**
Madan Jampanifa242182016-01-22 13:42:54 -080031 * {@code ConsistentMap} provides the same functionality as {@link AsyncConsistentMap} with
32 * the only difference that all its methods block until the corresponding operation completes.
Madan Jampani09342702015-02-05 23:32:40 -080033 *
Madan Jampanifa242182016-01-22 13:42:54 -080034 * @param <K> type of key
35 * @param <V> type of value
Madan Jampani09342702015-02-05 23:32:40 -080036 */
Madan Jampania090a112016-01-18 16:38:17 -080037public interface ConsistentMap<K, V> extends DistributedPrimitive {
Madan Jampani09342702015-02-05 23:32:40 -080038
39 /**
40 * Returns the number of entries in the map.
41 *
42 * @return map size.
43 */
44 int size();
45
46 /**
47 * Returns true if the map is empty.
48 *
Madan Jampani80984052015-07-23 13:11:36 -070049 * @return true if map has no entries, false otherwise
Madan Jampani09342702015-02-05 23:32:40 -080050 */
51 boolean isEmpty();
52
53 /**
54 * Returns true if this map contains a mapping for the specified key.
55 *
56 * @param key key
Madan Jampani80984052015-07-23 13:11:36 -070057 * @return true if map contains key, false otherwise
Madan Jampani09342702015-02-05 23:32:40 -080058 */
59 boolean containsKey(K key);
60
61 /**
62 * Returns true if this map contains the specified value.
63 *
64 * @param value value
65 * @return true if map contains value, false otherwise.
66 */
67 boolean containsValue(V value);
68
69 /**
70 * Returns the value (and version) to which the specified key is mapped, or null if this
71 * map contains no mapping for the key.
72 *
73 * @param key the key whose associated value (and version) is to be returned
74 * @return the value (and version) to which the specified key is mapped, or null if
75 * this map contains no mapping for the key
76 */
77 Versioned<V> get(K key);
78
79 /**
Jordan Haltermanf6272442017-04-20 02:18:08 -070080 * Returns the value (and version) to which the specified key is mapped, or the provided
81 * default value if this map contains no mapping for the key.
82 * <p>
83 * Note: a non-null {@link Versioned} value will be returned even if the {@code defaultValue}
84 * is {@code null}.
85 *
86 * @param key the key whose associated value (and version) is to be returned
87 * @param defaultValue the default value to return if the key is not set
88 * @return the value (and version) to which the specified key is mapped, or null if
89 * this map contains no mapping for the key
90 */
91 Versioned<V> getOrDefault(K key, V defaultValue);
92
93 /**
Madan Jampani346d4f52015-05-04 11:09:39 -070094 * If the specified key is not already associated with a value (or is mapped to null),
95 * attempts to compute its value using the given mapping function and enters it into
96 * this map unless null.
97 *
98 * @param key key with which the specified value is to be associated
99 * @param mappingFunction the function to compute a value
100 * @return the current (existing or computed) value associated with the specified key,
101 * or null if the computed value is null. Method throws {@code ConsistentMapException.ConcurrentModification}
102 * if a concurrent modification of map is detected
103 */
104 Versioned<V> computeIfAbsent(K key,
105 Function<? super K, ? extends V> mappingFunction);
106
107 /**
108 * Attempts to compute a mapping for the specified key and its current mapped value (or
109 * null if there is no current mapping).
110 * If the computed value is null, the current mapping will be removed from the map.
111 *
112 * @param key key with which the specified value is to be associated
113 * @param remappingFunction the function to compute a value
114 * @return the new value associated with the specified key, or null if none.
115 * This method throws {@code ConsistentMapException.ConcurrentModification}
116 * if a concurrent modification of map is detected
117 */
118 Versioned<V> compute(K key,
119 BiFunction<? super K, ? super V, ? extends V> remappingFunction);
120
121 /**
122 * If the value for the specified key is present and non-null, attempts to compute a new
123 * mapping given the key and its current mapped value.
124 * If the computed value is null, the current mapping will be removed from the map.
125 *
126 * @param key key with which the specified value is to be associated
127 * @param remappingFunction the function to compute a value
128 * @return the new value associated with the specified key, or null if none.
129 * This method throws {@code ConsistentMapException.ConcurrentModification}
130 * if a concurrent modification of map is detected
131 */
132 Versioned<V> computeIfPresent(K key,
133 BiFunction<? super K, ? super V, ? extends V> remappingFunction);
134
135 /**
136 * If the value for the specified key satisfies a condition, attempts to compute a new
137 * mapping given the key and its current mapped value.
138 * If the computed value is null, the current mapping will be removed from the map.
139 *
140 * @param key key with which the specified value is to be associated
141 * @param condition condition that should evaluate to true for the computation to proceed
142 * @param remappingFunction the function to compute a value
143 * @return the new value associated with the specified key, or the old value if condition evaluates to false.
144 * This method throws {@code ConsistentMapException.ConcurrentModification} if a concurrent
145 * modification of map is detected
146 */
147 Versioned<V> computeIf(K key,
148 Predicate<? super V> condition,
149 BiFunction<? super K, ? super V, ? extends V> remappingFunction);
150
151 /**
Madan Jampani09342702015-02-05 23:32:40 -0800152 * Associates the specified value with the specified key in this map (optional operation).
153 * If the map previously contained a mapping for the key, the old value is replaced by the
154 * specified value.
155 *
156 * @param key key with which the specified value is to be associated
157 * @param value value to be associated with the specified key
158 * @return the previous value (and version) associated with key, or null if there was
159 * no mapping for key.
160 */
161 Versioned<V> put(K key, V value);
162
163 /**
Madan Jampani346d4f52015-05-04 11:09:39 -0700164 * Associates the specified value with the specified key in this map (optional operation).
165 * If the map previously contained a mapping for the key, the old value is replaced by the
166 * specified value.
167 *
168 * @param key key with which the specified value is to be associated
169 * @param value value to be associated with the specified key
170 * @return new value.
171 */
172 Versioned<V> putAndGet(K key, V value);
173
174 /**
Madan Jampani09342702015-02-05 23:32:40 -0800175 * Removes the mapping for a key from this map if it is present (optional operation).
176 *
177 * @param key key whose value is to be removed from the map
178 * @return the value (and version) to which this map previously associated the key,
179 * or null if the map contained no mapping for the key.
180 */
181 Versioned<V> remove(K key);
182
183 /**
184 * Removes all of the mappings from this map (optional operation).
185 * The map will be empty after this call returns.
186 */
187 void clear();
188
189 /**
190 * Returns a Set view of the keys contained in this map.
191 * This method differs from the behavior of java.util.Map.keySet() in that
192 * what is returned is a unmodifiable snapshot view of the keys in the ConsistentMap.
193 * Attempts to modify the returned set, whether direct or via its iterator,
194 * result in an UnsupportedOperationException.
195 *
196 * @return a set of the keys contained in this map
197 */
198 Set<K> keySet();
199
200 /**
201 * Returns the collection of values (and associated versions) contained in this map.
202 * This method differs from the behavior of java.util.Map.values() in that
203 * what is returned is a unmodifiable snapshot view of the values in the ConsistentMap.
204 * Attempts to modify the returned collection, whether direct or via its iterator,
205 * result in an UnsupportedOperationException.
206 *
207 * @return a collection of the values (and associated versions) contained in this map
208 */
209 Collection<Versioned<V>> values();
210
211 /**
212 * Returns the set of entries contained in this map.
213 * This method differs from the behavior of java.util.Map.entrySet() in that
214 * what is returned is a unmodifiable snapshot view of the entries in the ConsistentMap.
215 * Attempts to modify the returned set, whether direct or via its iterator,
216 * result in an UnsupportedOperationException.
217 *
218 * @return set of entries contained in this map.
219 */
220 Set<Entry<K, Versioned<V>>> entrySet();
221
222 /**
223 * If the specified key is not already associated with a value
224 * associates it with the given value and returns null, else returns the current value.
225 *
226 * @param key key with which the specified value is to be associated
227 * @param value value to be associated with the specified key
228 * @return the previous value associated with the specified key or null
229 * if key does not already mapped to a value.
230 */
231 Versioned<V> putIfAbsent(K key, V value);
232
233 /**
234 * Removes the entry for the specified key only if it is currently
235 * mapped to the specified value.
236 *
237 * @param key key with which the specified value is associated
238 * @param value value expected to be associated with the specified key
239 * @return true if the value was removed
240 */
241 boolean remove(K key, V value);
242
243 /**
244 * Removes the entry for the specified key only if its current
245 * version in the map is equal to the specified version.
246 *
247 * @param key key with which the specified version is associated
248 * @param version version expected to be associated with the specified key
249 * @return true if the value was removed
250 */
251 boolean remove(K key, long version);
252
253 /**
Jihwan Kim9887ad92015-12-12 00:23:57 +0900254 * Replaces the entry for the specified key only if there is any value
255 * which associated with specified key.
256 *
257 * @param key key with which the specified value is associated
258 * @param value value expected to be associated with the specified key
259 * @return the previous value associated with the specified key or null
260 */
261 Versioned<V> replace(K key, V value);
262
263 /**
Madan Jampani09342702015-02-05 23:32:40 -0800264 * Replaces the entry for the specified key only if currently mapped
265 * to the specified value.
266 *
267 * @param key key with which the specified value is associated
268 * @param oldValue value expected to be associated with the specified key
269 * @param newValue value to be associated with the specified key
270 * @return true if the value was replaced
271 */
272 boolean replace(K key, V oldValue, V newValue);
273
274 /**
275 * Replaces the entry for the specified key only if it is currently mapped to the
276 * specified version.
277 *
278 * @param key key key with which the specified value is associated
279 * @param oldVersion version expected to be associated with the specified key
280 * @param newValue value to be associated with the specified key
281 * @return true if the value was replaced
282 */
283 boolean replace(K key, long oldVersion, V newValue);
Madan Jampani346d4f52015-05-04 11:09:39 -0700284
285 /**
Madan Jampani50589ac2015-06-08 11:38:46 -0700286 * Registers the specified listener to be notified whenever the map is updated.
287 *
288 * @param listener listener to notify about map events
289 */
Madan Jampani0463cf92016-05-04 14:46:08 -0700290 default void addListener(MapEventListener<K, V> listener) {
291 addListener(listener, MoreExecutors.directExecutor());
292 }
293
294 /**
295 * Registers the specified listener to be notified whenever the map is updated.
296 *
297 * @param listener listener to notify about map events
298 * @param executor executor to use for handling incoming map events
299 */
300 void addListener(MapEventListener<K, V> listener, Executor executor);
Madan Jampani50589ac2015-06-08 11:38:46 -0700301
302 /**
303 * Unregisters the specified listener such that it will no longer
304 * receive map change notifications.
305 *
306 * @param listener listener to unregister
307 */
308 void removeListener(MapEventListener<K, V> listener);
Madan Jampanie1065222015-08-17 16:21:51 -0700309
310 /**
311 * Returns a java.util.Map instance backed by this ConsistentMap.
312 * @return java.util.Map
313 */
314 Map<K, V> asJavaMap();
Madan Jampanidfde6ba2016-01-13 21:36:09 -0800315}