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