1. /*
  2. * @(#)ConcurrentMap.java 1.6 04/07/12
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.util.concurrent;
  8. import java.util.Map;
  9. /**
  10. * A {@link java.util.Map} providing additional atomic
  11. * <tt>putIfAbsent</tt>, <tt>remove</tt>, and <tt>replace</tt> methods.
  12. *
  13. * <p>This interface is a member of the
  14. * <a href="{@docRoot}/../guide/collections/index.html">
  15. * Java Collections Framework</a>.
  16. *
  17. * @since 1.5
  18. * @author Doug Lea
  19. * @param <K> the type of keys maintained by this map
  20. * @param <V> the type of mapped values
  21. */
  22. public interface ConcurrentMap<K, V> extends Map<K, V> {
  23. /**
  24. * If the specified key is not already associated
  25. * with a value, associate it with the given value.
  26. * This is equivalent to
  27. * <pre>
  28. * if (!map.containsKey(key))
  29. * return map.put(key, value);
  30. * else
  31. * return map.get(key);
  32. * </pre>
  33. * Except that the action is performed atomically.
  34. * @param key key with which the specified value is to be associated.
  35. * @param value value to be associated with the specified key.
  36. * @return previous value associated with specified key, or <tt>null</tt>
  37. * if there was no mapping for key. A <tt>null</tt> return can
  38. * also indicate that the map previously associated <tt>null</tt>
  39. * with the specified key, if the implementation supports
  40. * <tt>null</tt> values.
  41. *
  42. * @throws UnsupportedOperationException if the <tt>put</tt> operation is
  43. * not supported by this map.
  44. * @throws ClassCastException if the class of the specified key or value
  45. * prevents it from being stored in this map.
  46. * @throws IllegalArgumentException if some aspect of this key or value
  47. * prevents it from being stored in this map.
  48. * @throws NullPointerException if this map does not permit <tt>null</tt>
  49. * keys or values, and the specified key or value is
  50. * <tt>null</tt>.
  51. *
  52. */
  53. V putIfAbsent(K key, V value);
  54. /**
  55. * Remove entry for key only if currently mapped to given value.
  56. * Acts as
  57. * <pre>
  58. * if ((map.containsKey(key) && map.get(key).equals(value)) {
  59. * map.remove(key);
  60. * return true;
  61. * } else return false;
  62. * </pre>
  63. * except that the action is performed atomically.
  64. * @param key key with which the specified value is associated.
  65. * @param value value associated with the specified key.
  66. * @return true if the value was removed, false otherwise
  67. * @throws UnsupportedOperationException if the <tt>remove</tt> operation is
  68. * not supported by this map.
  69. * @throws NullPointerException if this map does not permit <tt>null</tt>
  70. * keys or values, and the specified key or value is
  71. * <tt>null</tt>.
  72. */
  73. boolean remove(Object key, Object value);
  74. /**
  75. * Replace entry for key only if currently mapped to given value.
  76. * Acts as
  77. * <pre>
  78. * if ((map.containsKey(key) && map.get(key).equals(oldValue)) {
  79. * map.put(key, newValue);
  80. * return true;
  81. * } else return false;
  82. * </pre>
  83. * except that the action is performed atomically.
  84. * @param key key with which the specified value is associated.
  85. * @param oldValue value expected to be associated with the specified key.
  86. * @param newValue value to be associated with the specified key.
  87. * @return true if the value was replaced
  88. * @throws UnsupportedOperationException if the <tt>put</tt> operation is
  89. * not supported by this map.
  90. * @throws NullPointerException if this map does not permit <tt>null</tt>
  91. * keys or values, and the specified key or value is
  92. * <tt>null</tt>.
  93. */
  94. boolean replace(K key, V oldValue, V newValue);
  95. /**
  96. * Replace entry for key only if currently mapped to some value.
  97. * Acts as
  98. * <pre>
  99. * if ((map.containsKey(key)) {
  100. * return map.put(key, value);
  101. * } else return null;
  102. * </pre>
  103. * except that the action is performed atomically.
  104. * @param key key with which the specified value is associated.
  105. * @param value value to be associated with the specified key.
  106. * @return previous value associated with specified key, or <tt>null</tt>
  107. * if there was no mapping for key. A <tt>null</tt> return can
  108. * also indicate that the map previously associated <tt>null</tt>
  109. * with the specified key, if the implementation supports
  110. * <tt>null</tt> values.
  111. * @throws UnsupportedOperationException if the <tt>put</tt> operation is
  112. * not supported by this map.
  113. * @throws NullPointerException if this map does not permit <tt>null</tt>
  114. * keys or values, and the specified key or value is
  115. * <tt>null</tt>.
  116. */
  117. V replace(K key, V value);
  118. }