blob: f5e8c13b1713e0bfe55130e727da440389af1b83 [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 /**
Madan Jampani346d4f52015-05-04 11:09:39 -070080 * If the specified key is not already associated with a value (or is mapped to null),
81 * attempts to compute its value using the given mapping function and enters it into
82 * this map unless null.
83 *
84 * @param key key with which the specified value is to be associated
85 * @param mappingFunction the function to compute a value
86 * @return the current (existing or computed) value associated with the specified key,
87 * or null if the computed value is null. Method throws {@code ConsistentMapException.ConcurrentModification}
88 * if a concurrent modification of map is detected
89 */
90 Versioned<V> computeIfAbsent(K key,
91 Function<? super K, ? extends V> mappingFunction);
92
93 /**
94 * Attempts to compute a mapping for the specified key and its current mapped value (or
95 * null if there is no current mapping).
96 * If the computed value is null, the current mapping will be removed from the map.
97 *
98 * @param key key with which the specified value is to be associated
99 * @param remappingFunction the function to compute a value
100 * @return the new value associated with the specified key, or null if none.
101 * This method throws {@code ConsistentMapException.ConcurrentModification}
102 * if a concurrent modification of map is detected
103 */
104 Versioned<V> compute(K key,
105 BiFunction<? super K, ? super V, ? extends V> remappingFunction);
106
107 /**
108 * If the value for the specified key is present and non-null, attempts to compute a new
109 * mapping given the key and its current mapped value.
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> computeIfPresent(K key,
119 BiFunction<? super K, ? super V, ? extends V> remappingFunction);
120
121 /**
122 * If the value for the specified key satisfies a condition, 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 condition condition that should evaluate to true for the computation to proceed
128 * @param remappingFunction the function to compute a value
129 * @return the new value associated with the specified key, or the old value if condition evaluates to false.
130 * This method throws {@code ConsistentMapException.ConcurrentModification} if a concurrent
131 * modification of map is detected
132 */
133 Versioned<V> computeIf(K key,
134 Predicate<? super V> condition,
135 BiFunction<? super K, ? super V, ? extends V> remappingFunction);
136
137 /**
Madan Jampani09342702015-02-05 23:32:40 -0800138 * Associates the specified value with the specified key in this map (optional operation).
139 * If the map previously contained a mapping for the key, the old value is replaced by the
140 * specified value.
141 *
142 * @param key key with which the specified value is to be associated
143 * @param value value to be associated with the specified key
144 * @return the previous value (and version) associated with key, or null if there was
145 * no mapping for key.
146 */
147 Versioned<V> put(K key, V value);
148
149 /**
Madan Jampani346d4f52015-05-04 11:09:39 -0700150 * Associates the specified value with the specified key in this map (optional operation).
151 * If the map previously contained a mapping for the key, the old value is replaced by the
152 * specified value.
153 *
154 * @param key key with which the specified value is to be associated
155 * @param value value to be associated with the specified key
156 * @return new value.
157 */
158 Versioned<V> putAndGet(K key, V value);
159
160 /**
Madan Jampani09342702015-02-05 23:32:40 -0800161 * Removes the mapping for a key from this map if it is present (optional operation).
162 *
163 * @param key key whose value is to be removed from the map
164 * @return the value (and version) to which this map previously associated the key,
165 * or null if the map contained no mapping for the key.
166 */
167 Versioned<V> remove(K key);
168
169 /**
170 * Removes all of the mappings from this map (optional operation).
171 * The map will be empty after this call returns.
172 */
173 void clear();
174
175 /**
176 * Returns a Set view of the keys contained in this map.
177 * This method differs from the behavior of java.util.Map.keySet() in that
178 * what is returned is a unmodifiable snapshot view of the keys in the ConsistentMap.
179 * Attempts to modify the returned set, whether direct or via its iterator,
180 * result in an UnsupportedOperationException.
181 *
182 * @return a set of the keys contained in this map
183 */
184 Set<K> keySet();
185
186 /**
187 * Returns the collection of values (and associated versions) contained in this map.
188 * This method differs from the behavior of java.util.Map.values() in that
189 * what is returned is a unmodifiable snapshot view of the values in the ConsistentMap.
190 * Attempts to modify the returned collection, whether direct or via its iterator,
191 * result in an UnsupportedOperationException.
192 *
193 * @return a collection of the values (and associated versions) contained in this map
194 */
195 Collection<Versioned<V>> values();
196
197 /**
198 * Returns the set of entries contained in this map.
199 * This method differs from the behavior of java.util.Map.entrySet() in that
200 * what is returned is a unmodifiable snapshot view of the entries in the ConsistentMap.
201 * Attempts to modify the returned set, whether direct or via its iterator,
202 * result in an UnsupportedOperationException.
203 *
204 * @return set of entries contained in this map.
205 */
206 Set<Entry<K, Versioned<V>>> entrySet();
207
208 /**
209 * If the specified key is not already associated with a value
210 * associates it with the given value and returns null, else returns the current value.
211 *
212 * @param key key with which the specified value is to be associated
213 * @param value value to be associated with the specified key
214 * @return the previous value associated with the specified key or null
215 * if key does not already mapped to a value.
216 */
217 Versioned<V> putIfAbsent(K key, V value);
218
219 /**
220 * Removes the entry for the specified key only if it is currently
221 * mapped to the specified value.
222 *
223 * @param key key with which the specified value is associated
224 * @param value value expected to be associated with the specified key
225 * @return true if the value was removed
226 */
227 boolean remove(K key, V value);
228
229 /**
230 * Removes the entry for the specified key only if its current
231 * version in the map is equal to the specified version.
232 *
233 * @param key key with which the specified version is associated
234 * @param version version expected to be associated with the specified key
235 * @return true if the value was removed
236 */
237 boolean remove(K key, long version);
238
239 /**
Jihwan Kim9887ad92015-12-12 00:23:57 +0900240 * Replaces the entry for the specified key only if there is any value
241 * which associated with specified key.
242 *
243 * @param key key with which the specified value is associated
244 * @param value value expected to be associated with the specified key
245 * @return the previous value associated with the specified key or null
246 */
247 Versioned<V> replace(K key, V value);
248
249 /**
Madan Jampani09342702015-02-05 23:32:40 -0800250 * Replaces the entry for the specified key only if currently mapped
251 * to the specified value.
252 *
253 * @param key key with which the specified value is associated
254 * @param oldValue value expected to be associated with the specified key
255 * @param newValue value to be associated with the specified key
256 * @return true if the value was replaced
257 */
258 boolean replace(K key, V oldValue, V newValue);
259
260 /**
261 * Replaces the entry for the specified key only if it is currently mapped to the
262 * specified version.
263 *
264 * @param key key key with which the specified value is associated
265 * @param oldVersion version expected to be associated with the specified key
266 * @param newValue value to be associated with the specified key
267 * @return true if the value was replaced
268 */
269 boolean replace(K key, long oldVersion, V newValue);
Madan Jampani346d4f52015-05-04 11:09:39 -0700270
271 /**
Madan Jampani50589ac2015-06-08 11:38:46 -0700272 * Registers the specified listener to be notified whenever the map is updated.
273 *
274 * @param listener listener to notify about map events
275 */
Madan Jampani0463cf92016-05-04 14:46:08 -0700276 default void addListener(MapEventListener<K, V> listener) {
277 addListener(listener, MoreExecutors.directExecutor());
278 }
279
280 /**
281 * Registers the specified listener to be notified whenever the map is updated.
282 *
283 * @param listener listener to notify about map events
284 * @param executor executor to use for handling incoming map events
285 */
286 void addListener(MapEventListener<K, V> listener, Executor executor);
Madan Jampani50589ac2015-06-08 11:38:46 -0700287
288 /**
289 * Unregisters the specified listener such that it will no longer
290 * receive map change notifications.
291 *
292 * @param listener listener to unregister
293 */
294 void removeListener(MapEventListener<K, V> listener);
Madan Jampanie1065222015-08-17 16:21:51 -0700295
296 /**
297 * Returns a java.util.Map instance backed by this ConsistentMap.
298 * @return java.util.Map
299 */
300 Map<K, V> asJavaMap();
Madan Jampanidfde6ba2016-01-13 21:36:09 -0800301}