src/share/classes/java/util/concurrent/ConcurrentMap.java

Print this page
rev 6858 : 8004518: Add in-place operations to Map
8010122: Add defaults for ConcurrentMap operations to Map
Reviewed-by: darcy, briangoetz, mduigou, dholmes, ulfzibis
Contributed-by: Doug Lea <dl at cs.oswego.edu>, Henry Jen <henry.jen@oracle.com>, Akhil Arora <akhil.arora@oracle.com>, Peter Levart <peter.levart@gmail.com>

*** 36,46 **** package java.util.concurrent; import java.util.Map; /** * A {@link java.util.Map} providing additional atomic ! * <tt>putIfAbsent</tt>, <tt>remove</tt>, and <tt>replace</tt> methods. * * <p>Memory consistency effects: As with other concurrent * collections, actions in a thread prior to placing an object into a * {@code ConcurrentMap} as a key or value * <a href="package-summary.html#MemoryVisibility"><i>happen-before</i></a> --- 36,46 ---- package java.util.concurrent; import java.util.Map; /** * A {@link java.util.Map} providing additional atomic ! * {@code putIfAbsent}, {@code remove}, and {@code replace} methods. * * <p>Memory consistency effects: As with other concurrent * collections, actions in a thread prior to placing an object into a * {@code ConcurrentMap} as a key or value * <a href="package-summary.html#MemoryVisibility"><i>happen-before</i></a>
*** 55,64 **** --- 55,79 ---- * @author Doug Lea * @param <K> the type of keys maintained by this map * @param <V> the type of mapped values */ public interface ConcurrentMap<K, V> extends Map<K, V> { + + /** + * {@inheritDoc} + * + * @implNote This implementation assumes that the ConcurrentMap cannot + * contain null values and get() returning null unambiguously means no + * mapping is present. Implementations which support null values must + * override this default implementation. + */ + @Override + default V getOrDefault(Object key, V defaultValue) { + V v; + return (null != (v = get(key))) ? v : defaultValue; + } + /** * If the specified key is not already associated * with a value, associate it with the given value. * This is equivalent to * <pre> {@code
*** 89,110 **** /** * Removes the entry for a key only if currently mapped to a given value. * This is equivalent to * <pre> {@code ! * if (map.containsKey(key) && map.get(key).equals(value)) { * map.remove(key); * return true; * } else * return false;}</pre> * * except that the action is performed atomically. * * @param key key with which the specified value is associated * @param value value expected to be associated with the specified key ! * @return <tt>true</tt> if the value was removed ! * @throws UnsupportedOperationException if the <tt>remove</tt> operation * is not supported by this map * @throws ClassCastException if the key or value is of an inappropriate * type for this map * (<a href="../Collection.html#optional-restrictions">optional</a>) * @throws NullPointerException if the specified key or value is null, --- 104,125 ---- /** * Removes the entry for a key only if currently mapped to a given value. * This is equivalent to * <pre> {@code ! * if (map.containsKey(key) && Objects.equals(map.get(key), value)) { * map.remove(key); * return true; * } else * return false;}</pre> * * except that the action is performed atomically. * * @param key key with which the specified value is associated * @param value value expected to be associated with the specified key ! * @return {@code true} if the value was removed ! * @throws UnsupportedOperationException if the {@code remove} operation * is not supported by this map * @throws ClassCastException if the key or value is of an inappropriate * type for this map * (<a href="../Collection.html#optional-restrictions">optional</a>) * @throws NullPointerException if the specified key or value is null,
*** 115,137 **** /** * Replaces the entry for a key only if currently mapped to a given value. * This is equivalent to * <pre> {@code ! * if (map.containsKey(key) && map.get(key).equals(oldValue)) { * map.put(key, newValue); * return true; * } else * return false;}</pre> * * except that the action is performed atomically. * * @param key key with which the specified value is associated * @param oldValue value expected to be associated with the specified key * @param newValue value to be associated with the specified key ! * @return <tt>true</tt> if the value was replaced ! * @throws UnsupportedOperationException if the <tt>put</tt> operation * is not supported by this map * @throws ClassCastException if the class of a specified key or value * prevents it from being stored in this map * @throws NullPointerException if a specified key or value is null, * and this map does not permit null keys or values --- 130,152 ---- /** * Replaces the entry for a key only if currently mapped to a given value. * This is equivalent to * <pre> {@code ! * if (map.containsKey(key) && Objects.equals(map.get(key), oldValue)) { * map.put(key, newValue); * return true; * } else * return false;}</pre> * * except that the action is performed atomically. * * @param key key with which the specified value is associated * @param oldValue value expected to be associated with the specified key * @param newValue value to be associated with the specified key ! * @return {@code true} if the value was replaced ! * @throws UnsupportedOperationException if the {@code put} operation * is not supported by this map * @throws ClassCastException if the class of a specified key or value * prevents it from being stored in this map * @throws NullPointerException if a specified key or value is null, * and this map does not permit null keys or values
*** 152,166 **** * except that the action is performed atomically. * * @param key key with which the specified value is associated * @param value value to be associated with the specified key * @return the previous value associated with the specified key, or ! * <tt>null</tt> if there was no mapping for the key. ! * (A <tt>null</tt> return can also indicate that the map ! * previously associated <tt>null</tt> with the key, * if the implementation supports null values.) ! * @throws UnsupportedOperationException if the <tt>put</tt> operation * is not supported by this map * @throws ClassCastException if the class of the specified key or value * prevents it from being stored in this map * @throws NullPointerException if the specified key or value is null, * and this map does not permit null keys or values --- 167,181 ---- * except that the action is performed atomically. * * @param key key with which the specified value is associated * @param value value to be associated with the specified key * @return the previous value associated with the specified key, or ! * {@code null} if there was no mapping for the key. ! * (A {@code null} return can also indicate that the map ! * previously associated {@code null} with the key, * if the implementation supports null values.) ! * @throws UnsupportedOperationException if the {@code put} operation * is not supported by this map * @throws ClassCastException if the class of the specified key or value * prevents it from being stored in this map * @throws NullPointerException if the specified key or value is null, * and this map does not permit null keys or values