blob: 39119b7c292297d84c7bab67cf9a12b9c67f9d51 [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 Jampani346d4f52015-05-04 11:09:39 -070023import java.util.function.BiFunction;
24import java.util.function.Function;
25import java.util.function.Predicate;
Madan Jampani09342702015-02-05 23:32:40 -080026
27/**
Madan Jampanifa242182016-01-22 13:42:54 -080028 * {@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 Jampani09342702015-02-05 23:32:40 -080030 *
Madan Jampanifa242182016-01-22 13:42:54 -080031 * @param <K> type of key
32 * @param <V> type of value
Madan Jampani09342702015-02-05 23:32:40 -080033 */
Madan Jampania090a112016-01-18 16:38:17 -080034public interface ConsistentMap<K, V> extends DistributedPrimitive {
Madan Jampani09342702015-02-05 23:32:40 -080035
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 Jampani80984052015-07-23 13:11:36 -070046 * @return true if map has no entries, false otherwise
Madan Jampani09342702015-02-05 23:32:40 -080047 */
48 boolean isEmpty();
49
50 /**
51 * Returns true if this map contains a mapping for the specified key.
52 *
53 * @param key key
Madan Jampani80984052015-07-23 13:11:36 -070054 * @return true if map contains key, false otherwise
Madan Jampani09342702015-02-05 23:32:40 -080055 */
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 Jampani346d4f52015-05-04 11:09:39 -070077 * 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 Jampani09342702015-02-05 23:32:40 -0800135 * 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 Jampani346d4f52015-05-04 11:09:39 -0700147 * 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 Jampani09342702015-02-05 23:32:40 -0800158 * 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 Kim9887ad92015-12-12 00:23:57 +0900237 * 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 Jampani09342702015-02-05 23:32:40 -0800247 * 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 Jampani346d4f52015-05-04 11:09:39 -0700267
268 /**
Madan Jampani50589ac2015-06-08 11:38:46 -0700269 * 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 Jampanie1065222015-08-17 16:21:51 -0700282
283 /**
284 * Returns a java.util.Map instance backed by this ConsistentMap.
285 * @return java.util.Map
286 */
287 Map<K, V> asJavaMap();
Madan Jampanidfde6ba2016-01-13 21:36:09 -0800288}