Madan Jampani | 6468955 | 2015-02-17 10:00:27 -0800 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | |
| 17 | package org.onosproject.store.service; |
| 18 | |
| 19 | import java.util.Collection; |
| 20 | import java.util.Set; |
| 21 | import java.util.Map.Entry; |
| 22 | |
| 23 | /** |
| 24 | * Transactional Map data structure. |
| 25 | * <p> |
| 26 | * A TransactionalMap is created by invoking {@link TransactionContext#createTransactionalMap createTransactionalMap} |
| 27 | * method. All operations performed on this map with in a transaction boundary are invisible externally |
| 28 | * until the point when the transaction commits. A commit usually succeeds in the absence of conflicts. |
| 29 | * |
| 30 | * @param <K> type of key. |
| 31 | * @param <V> type of value. |
| 32 | */ |
| 33 | public interface TransactionalMap<K, V> { |
| 34 | |
| 35 | /** |
| 36 | * Returns the number of entries in the map. |
| 37 | * |
| 38 | * @return map size. |
| 39 | */ |
| 40 | int size(); |
| 41 | |
| 42 | /** |
| 43 | * Returns true if the map is empty. |
| 44 | * |
| 45 | * @return true if map has no entries, false otherwise. |
| 46 | */ |
| 47 | boolean isEmpty(); |
| 48 | |
| 49 | /** |
| 50 | * Returns true if this map contains a mapping for the specified key. |
| 51 | * |
| 52 | * @param key key |
| 53 | * @return true if map contains key, false otherwise. |
| 54 | */ |
| 55 | boolean containsKey(K key); |
| 56 | |
| 57 | /** |
| 58 | * Returns true if this map contains the specified value. |
| 59 | * |
| 60 | * @param value value |
| 61 | * @return true if map contains value, false otherwise. |
| 62 | */ |
| 63 | boolean containsValue(V value); |
| 64 | |
| 65 | /** |
| 66 | * Returns the value to which the specified key is mapped, or null if this |
| 67 | * map contains no mapping for the key. |
| 68 | * |
| 69 | * @param key the key whose associated value is to be returned |
| 70 | * @return the value to which the specified key is mapped, or null if |
| 71 | * this map contains no mapping for the key |
| 72 | */ |
| 73 | V get(K key); |
| 74 | |
| 75 | /** |
| 76 | * Associates the specified value with the specified key in this map (optional operation). |
| 77 | * If the map previously contained a mapping for the key, the old value is replaced by the |
| 78 | * specified value. |
| 79 | * |
| 80 | * @param key key with which the specified value is to be associated |
| 81 | * @param value value to be associated with the specified key |
| 82 | * @return the previous value associated with key, or null if there was |
| 83 | * no mapping for key. |
| 84 | */ |
| 85 | V put(K key, V value); |
| 86 | |
| 87 | /** |
| 88 | * Removes the mapping for a key from this map if it is present (optional operation). |
| 89 | * |
| 90 | * @param key key whose value is to be removed from the map |
| 91 | * @return the value to which this map previously associated the key, |
| 92 | * or null if the map contained no mapping for the key. |
| 93 | */ |
| 94 | V remove(K key); |
| 95 | |
| 96 | /** |
| 97 | * Removes all of the mappings from this map (optional operation). |
| 98 | * The map will be empty after this call returns. |
| 99 | */ |
| 100 | void clear(); |
| 101 | |
| 102 | /** |
| 103 | * Returns a Set view of the keys contained in this map. |
| 104 | * This method differs from the behavior of java.util.Map.keySet() in that |
| 105 | * what is returned is a unmodifiable snapshot view of the keys in the ConsistentMap. |
| 106 | * Attempts to modify the returned set, whether direct or via its iterator, |
| 107 | * result in an UnsupportedOperationException. |
| 108 | * |
| 109 | * @return a set of the keys contained in this map |
| 110 | */ |
| 111 | Set<K> keySet(); |
| 112 | |
| 113 | /** |
| 114 | * Returns the collection of values contained in this map. |
| 115 | * This method differs from the behavior of java.util.Map.values() in that |
| 116 | * what is returned is a unmodifiable snapshot view of the values in the ConsistentMap. |
| 117 | * Attempts to modify the returned collection, whether direct or via its iterator, |
| 118 | * result in an UnsupportedOperationException. |
| 119 | * |
| 120 | * @return a collection of the values contained in this map |
| 121 | */ |
| 122 | Collection<V> values(); |
| 123 | |
| 124 | /** |
| 125 | * Returns the set of entries contained in this map. |
| 126 | * This method differs from the behavior of java.util.Map.entrySet() in that |
| 127 | * what is returned is a unmodifiable snapshot view of the entries in the ConsistentMap. |
| 128 | * Attempts to modify the returned set, whether direct or via its iterator, |
| 129 | * result in an UnsupportedOperationException. |
| 130 | * |
| 131 | * @return set of entries contained in this map. |
| 132 | */ |
| 133 | Set<Entry<K, V>> entrySet(); |
| 134 | |
| 135 | /** |
| 136 | * If the specified key is not already associated with a value |
| 137 | * associates it with the given value and returns null, else returns the current 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 associated with the specified key or null |
| 142 | * if key does not already mapped to a value. |
| 143 | */ |
| 144 | V putIfAbsent(K key, V value); |
| 145 | |
| 146 | /** |
| 147 | * Removes the entry for the specified key only if it is currently |
| 148 | * mapped to the specified value. |
| 149 | * |
| 150 | * @param key key with which the specified value is associated |
| 151 | * @param value value expected to be associated with the specified key |
| 152 | * @return true if the value was removed |
| 153 | */ |
| 154 | boolean remove(K key, V value); |
| 155 | |
| 156 | /** |
| 157 | * Replaces the entry for the specified key only if currently mapped |
| 158 | * to the specified value. |
| 159 | * |
| 160 | * @param key key with which the specified value is associated |
| 161 | * @param oldValue value expected to be associated with the specified key |
| 162 | * @param newValue value to be associated with the specified key |
| 163 | * @return true if the value was replaced |
| 164 | */ |
| 165 | boolean replace(K key, V oldValue, V newValue); |
| 166 | } |