blob: 9aaa00c555833552af4431289af6ed1bddf91145 [file] [log] [blame]
Jonathan Hartdb3af892015-01-26 13:19:07 -08001/*
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 */
16package org.onosproject.store.impl;
17
18import java.util.Collection;
19import java.util.Map;
20import java.util.Set;
21
22/**
23 * A distributed, eventually consistent map.
Jonathan Hart4f397e82015-02-04 09:10:41 -080024 * <p>
Jonathan Hartdb3af892015-01-26 13:19:07 -080025 * This map does not offer read after writes consistency. Operations are
26 * serialized via the timestamps issued by the clock service. If two updates
27 * are in conflict, the update with the more recent timestamp will endure.
Jonathan Hart4f397e82015-02-04 09:10:41 -080028 * </p><p>
Jonathan Hartdb3af892015-01-26 13:19:07 -080029 * The interface is mostly similar to {@link java.util.Map} with some minor
30 * semantic changes and the addition of a listener framework (because the map
31 * can be mutated by clients on other instances, not only through the local Java
32 * API).
Jonathan Hart4f397e82015-02-04 09:10:41 -080033 * </p><p>
Jonathan Hartdb3af892015-01-26 13:19:07 -080034 * Clients are expected to register an
35 * {@link org.onosproject.store.impl.EventuallyConsistentMapListener} if they
36 * are interested in receiving notifications of update to the map.
Jonathan Hart4f397e82015-02-04 09:10:41 -080037 * </p><p>
38 * Null values are not allowed in this map.
39 * </p>
Jonathan Hartdb3af892015-01-26 13:19:07 -080040 */
41public interface EventuallyConsistentMap<K, V> {
42
43 /**
44 * Returns the number of key-value mappings in this map.
45 *
46 * @return number of key-value mappings
47 */
48 public int size();
49
50 /**
51 * Returns true if this map is empty.
52 *
53 * @return true if this map is empty, otherwise false
54 */
55 public boolean isEmpty();
56
57 /**
58 * Returns true if the map contains a mapping for the specified key.
59 *
60 * @param key the key to check if this map contains
61 * @return true if this map has a mapping for the key, otherwise false
62 */
63 public boolean containsKey(K key);
64
65 /**
66 * Returns true if the map contains a mapping from any key to the specified
67 * value.
68 *
69 * @param value the value to check if this map has a mapping for
70 * @return true if this map has a mapping to this value, otherwise false
71 */
72 public boolean containsValue(V value);
73
74 /**
75 * Returns the value mapped to the specified key.
76 *
77 * @param key the key to look up in this map
78 * @return the value mapped to the key, or null if no mapping is found
79 */
80 public V get(K key);
81
82 /**
83 * Associates the specified value to the specified key in this map.
84 * <p>
85 * Note: this differs from the specification of {@link java.util.Map}
86 * because it does not return the previous value associated with the key.
87 * Clients are expected to register an
88 * {@link org.onosproject.store.impl.EventuallyConsistentMapListener} if
89 * they are interested in receiving notification of updates to the map.
Jonathan Hart4f397e82015-02-04 09:10:41 -080090 * </p><p>
91 * Null values are not allowed in the map.
Jonathan Hartdb3af892015-01-26 13:19:07 -080092 * </p>
93 *
94 * @param key the key to add a mapping for in this map
95 * @param value the value to associate with the key in this map
96 */
97 public void put(K key, V value);
98
99 /**
100 * Removes the mapping associated with the specified key from the map.
101 * <p>
102 * Note: this differs from the specification of {@link java.util.Map}
103 * because it does not return the previous value associated with the key.
104 * Clients are expected to register an
105 * {@link org.onosproject.store.impl.EventuallyConsistentMapListener} if
106 * they are interested in receiving notification of updates to the map.
107 * </p>
108 *
109 * @param key the key to remove the mapping for
110 */
111 public void remove(K key);
112
113 /**
114 * Adds mappings for all key-value pairs in the specified map to this map.
115 * <p>
116 * This will be more efficient in communication than calling individual put
117 * operations.
118 * </p>
119 *
120 * @param m a map of values to add to this map
121 */
122 public void putAll(Map<? extends K, ? extends V> m);
123
124 /**
125 * Removes all mappings from this map.
126 */
127 public void clear();
128
129 /**
130 * Returns a set of the keys in this map. Changes to the set are not
131 * reflected back to the map.
132 *
133 * @return set of keys in the map
134 */
135 public Set<K> keySet();
136
137 /**
138 * Returns a collections of values in this map. Changes to the collection
139 * are not reflected back to the map.
140 *
141 * @return collection of values in the map
142 */
143 public Collection<V> values();
144
145 /**
146 * Returns a set of mappings contained in this map. Changes to the set are
147 * not reflected back to the map.
148 *
149 * @return set of key-value mappings in this map
150 */
151 public Set<Map.Entry<K, V>> entrySet();
152
153 /**
154 * Adds the specified listener to the map which will be notified whenever
155 * the mappings in the map are changed.
156 *
157 * @param listener listener to register for events
158 */
Jonathan Hart539a6462015-01-27 17:05:43 -0800159 public void addListener(EventuallyConsistentMapListener<K, V> listener);
Jonathan Hartdb3af892015-01-26 13:19:07 -0800160
161 /**
162 * Removes the specified listener from the map such that it will no longer
163 * receive change notifications.
164 *
165 * @param listener listener to deregister for events
166 */
Jonathan Hart539a6462015-01-27 17:05:43 -0800167 public void removeListener(EventuallyConsistentMapListener<K, V> listener);
Jonathan Hartdb3af892015-01-26 13:19:07 -0800168
169 /**
170 * Shuts down the map and breaks communication between different instances.
171 * This allows the map objects to be cleaned up and garbage collected.
172 * Calls to any methods on the map subsequent to calling destroy() will
173 * throw a {@link java.lang.RuntimeException}.
174 */
175 public void destroy();
176}