|
| 1 | +/* |
| 2 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 3 | + * |
| 4 | + * This code is free software; you can redistribute it and/or modify it |
| 5 | + * under the terms of the GNU General Public License version 2 only, as |
| 6 | + * published by the Free Software Foundation. Oracle designates this |
| 7 | + * particular file as subject to the "Classpath" exception as provided |
| 8 | + * by Oracle in the LICENSE file that accompanied this code. |
| 9 | + * |
| 10 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 11 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 12 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 13 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 14 | + * accompanied this code). |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU General Public License version |
| 17 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 18 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 | + * |
| 20 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 21 | + * or visit www.oracle.com if you need additional information or have any |
| 22 | + * questions. |
| 23 | + */ |
| 24 | + |
| 25 | +/* |
| 26 | + * This file is available under and governed by the GNU General Public |
| 27 | + * License version 2 only, as published by the Free Software Foundation. |
| 28 | + * However, the following notice accompanied the original version of this |
| 29 | + * file: |
| 30 | + * |
| 31 | + * Written by Doug Lea with assistance from members of JCP JSR-166 |
| 32 | + * Expert Group and released to the public domain, as explained at |
| 33 | + * http://creativecommons.org/licenses/publicdomain |
| 34 | + */ |
| 35 | + |
| 36 | +package java.util.concurrent; |
| 37 | +import java.util.Map; |
| 38 | + |
| 39 | +/** |
| 40 | + * A {@link java.util.Map} providing additional atomic |
| 41 | + * <tt>putIfAbsent</tt>, <tt>remove</tt>, and <tt>replace</tt> methods. |
| 42 | + * |
| 43 | + * <p>Memory consistency effects: As with other concurrent |
| 44 | + * collections, actions in a thread prior to placing an object into a |
| 45 | + * {@code ConcurrentMap} as a key or value |
| 46 | + * <a href="package-summary.html#MemoryVisibility"><i>happen-before</i></a> |
| 47 | + * actions subsequent to the access or removal of that object from |
| 48 | + * the {@code ConcurrentMap} in another thread. |
| 49 | + * |
| 50 | + * <p>This interface is a member of the |
| 51 | + * <a href="{@docRoot}/../technotes/guides/collections/index.html"> |
| 52 | + * Java Collections Framework</a>. |
| 53 | + * |
| 54 | + * @since 1.5 |
| 55 | + * @author Doug Lea |
| 56 | + * @param <K> the type of keys maintained by this map |
| 57 | + * @param <V> the type of mapped values |
| 58 | + */ |
| 59 | +public interface ConcurrentMap<K, V> extends Map<K, V> { |
| 60 | + /** |
| 61 | + * If the specified key is not already associated |
| 62 | + * with a value, associate it with the given value. |
| 63 | + * This is equivalent to |
| 64 | + * <pre> |
| 65 | + * if (!map.containsKey(key)) |
| 66 | + * return map.put(key, value); |
| 67 | + * else |
| 68 | + * return map.get(key);</pre> |
| 69 | + * except that the action is performed atomically. |
| 70 | + * |
| 71 | + * @param key key with which the specified value is to be associated |
| 72 | + * @param value value to be associated with the specified key |
| 73 | + * @return the previous value associated with the specified key, or |
| 74 | + * <tt>null</tt> if there was no mapping for the key. |
| 75 | + * (A <tt>null</tt> return can also indicate that the map |
| 76 | + * previously associated <tt>null</tt> with the key, |
| 77 | + * if the implementation supports null values.) |
| 78 | + * @throws UnsupportedOperationException if the <tt>put</tt> operation |
| 79 | + * is not supported by this map |
| 80 | + * @throws ClassCastException if the class of the specified key or value |
| 81 | + * prevents it from being stored in this map |
| 82 | + * @throws NullPointerException if the specified key or value is null, |
| 83 | + * and this map does not permit null keys or values |
| 84 | + * @throws IllegalArgumentException if some property of the specified key |
| 85 | + * or value prevents it from being stored in this map |
| 86 | + * |
| 87 | + */ |
| 88 | + V putIfAbsent(K key, V value); |
| 89 | + |
| 90 | + /** |
| 91 | + * Removes the entry for a key only if currently mapped to a given value. |
| 92 | + * This is equivalent to |
| 93 | + * <pre> |
| 94 | + * if (map.containsKey(key) && map.get(key).equals(value)) { |
| 95 | + * map.remove(key); |
| 96 | + * return true; |
| 97 | + * } else return false;</pre> |
| 98 | + * except that the action is performed atomically. |
| 99 | + * |
| 100 | + * @param key key with which the specified value is associated |
| 101 | + * @param value value expected to be associated with the specified key |
| 102 | + * @return <tt>true</tt> if the value was removed |
| 103 | + * @throws UnsupportedOperationException if the <tt>remove</tt> operation |
| 104 | + * is not supported by this map |
| 105 | + * @throws ClassCastException if the key or value is of an inappropriate |
| 106 | + * type for this map (optional) |
| 107 | + * @throws NullPointerException if the specified key or value is null, |
| 108 | + * and this map does not permit null keys or values (optional) |
| 109 | + */ |
| 110 | + boolean remove(Object key, Object value); |
| 111 | + |
| 112 | + /** |
| 113 | + * Replaces the entry for a key only if currently mapped to a given value. |
| 114 | + * This is equivalent to |
| 115 | + * <pre> |
| 116 | + * if (map.containsKey(key) && map.get(key).equals(oldValue)) { |
| 117 | + * map.put(key, newValue); |
| 118 | + * return true; |
| 119 | + * } else return false;</pre> |
| 120 | + * except that the action is performed atomically. |
| 121 | + * |
| 122 | + * @param key key with which the specified value is associated |
| 123 | + * @param oldValue value expected to be associated with the specified key |
| 124 | + * @param newValue value to be associated with the specified key |
| 125 | + * @return <tt>true</tt> if the value was replaced |
| 126 | + * @throws UnsupportedOperationException if the <tt>put</tt> operation |
| 127 | + * is not supported by this map |
| 128 | + * @throws ClassCastException if the class of a specified key or value |
| 129 | + * prevents it from being stored in this map |
| 130 | + * @throws NullPointerException if a specified key or value is null, |
| 131 | + * and this map does not permit null keys or values |
| 132 | + * @throws IllegalArgumentException if some property of a specified key |
| 133 | + * or value prevents it from being stored in this map |
| 134 | + */ |
| 135 | + boolean replace(K key, V oldValue, V newValue); |
| 136 | + |
| 137 | + /** |
| 138 | + * Replaces the entry for a key only if currently mapped to some value. |
| 139 | + * This is equivalent to |
| 140 | + * <pre> |
| 141 | + * if (map.containsKey(key)) { |
| 142 | + * return map.put(key, value); |
| 143 | + * } else return null;</pre> |
| 144 | + * except that the action is performed atomically. |
| 145 | + * |
| 146 | + * @param key key with which the specified value is associated |
| 147 | + * @param value value to be associated with the specified key |
| 148 | + * @return the previous value associated with the specified key, or |
| 149 | + * <tt>null</tt> if there was no mapping for the key. |
| 150 | + * (A <tt>null</tt> return can also indicate that the map |
| 151 | + * previously associated <tt>null</tt> with the key, |
| 152 | + * if the implementation supports null values.) |
| 153 | + * @throws UnsupportedOperationException if the <tt>put</tt> operation |
| 154 | + * is not supported by this map |
| 155 | + * @throws ClassCastException if the class of the specified key or value |
| 156 | + * prevents it from being stored in this map |
| 157 | + * @throws NullPointerException if the specified key or value is null, |
| 158 | + * and this map does not permit null keys or values |
| 159 | + * @throws IllegalArgumentException if some property of the specified key |
| 160 | + * or value prevents it from being stored in this map |
| 161 | + */ |
| 162 | + V replace(K key, V value); |
| 163 | +} |
0 commit comments