788 /**
789 * Replaces the entry for the specified key only if currently
790 * mapped to the specified value.
791 *
792 * <p>The default implementation makes no guarantees about synchronization
793 * or atomicity properties of this method. Any implementation providing
794 * atomicity guarantees must override this method and document its
795 * concurrency properties.
796 *
797 * @implSpec
798 * The default implementation is equivalent to, for this {@code map}:
799 *
800 * <pre> {@code
801 * if (map.containsKey(key) && Objects.equals(map.get(key), value)) {
802 * map.put(key, newValue);
803 * return true;
804 * } else
805 * return false;
806 * }</pre>
807 *
808 * @param key key with which the specified value is associated
809 * @param oldValue value expected to be associated with the specified key
810 * @param newValue value to be associated with the specified key
811 * @return {@code true} if the value was replaced
812 * @throws UnsupportedOperationException if the {@code put} operation
813 * is not supported by this map
814 * (<a href="Collection.html#optional-restrictions">optional</a>)
815 * @throws ClassCastException if the class of a specified key or value
816 * prevents it from being stored in this map
817 * @throws NullPointerException if a specified key or value is null,
818 * and this map does not permit null keys or values
819 * @throws IllegalArgumentException if some property of a specified key
820 * or value prevents it from being stored in this map
821 * @since 1.8
822 */
823 default boolean replace(K key, V oldValue, V newValue) {
824 Object curValue = get(key);
825 if (!Objects.equals(curValue, oldValue) ||
826 (curValue == null && !containsKey(key))) {
827 return false;
828 }
829 put(key, newValue);
830 return true;
831 }
832
833 /**
834 * Replaces the entry for the specified key only if it is
835 * currently mapped to some value.
836 *
837 * <p>The default implementation makes no guarantees about synchronization
838 * or atomicity properties of this method. Any implementation providing
|
788 /**
789 * Replaces the entry for the specified key only if currently
790 * mapped to the specified value.
791 *
792 * <p>The default implementation makes no guarantees about synchronization
793 * or atomicity properties of this method. Any implementation providing
794 * atomicity guarantees must override this method and document its
795 * concurrency properties.
796 *
797 * @implSpec
798 * The default implementation is equivalent to, for this {@code map}:
799 *
800 * <pre> {@code
801 * if (map.containsKey(key) && Objects.equals(map.get(key), value)) {
802 * map.put(key, newValue);
803 * return true;
804 * } else
805 * return false;
806 * }</pre>
807 *
808 * The default implementation does not throw NullPointerException
809 * for maps that do not support null values if oldValue is null unless
810 * newValue is also null.
811 *
812 * @param key key with which the specified value is associated
813 * @param oldValue value expected to be associated with the specified key
814 * @param newValue value to be associated with the specified key
815 * @return {@code true} if the value was replaced
816 * @throws UnsupportedOperationException if the {@code put} operation
817 * is not supported by this map
818 * (<a href="Collection.html#optional-restrictions">optional</a>)
819 * @throws ClassCastException if the class of a specified key or value
820 * prevents it from being stored in this map
821 * @throws NullPointerException if a specified key or newValue is null,
822 * and this map does not permit null keys or values
823 * @throws NullPointerException if oldValue is null and this map does not
824 * permit null values
825 * (<a href="Collection.html#optional-restrictions">optional</a>)
826 * @throws IllegalArgumentException if some property of a specified key
827 * or value prevents it from being stored in this map
828 * @since 1.8
829 */
830 default boolean replace(K key, V oldValue, V newValue) {
831 Object curValue = get(key);
832 if (!Objects.equals(curValue, oldValue) ||
833 (curValue == null && !containsKey(key))) {
834 return false;
835 }
836 put(key, newValue);
837 return true;
838 }
839
840 /**
841 * Replaces the entry for the specified key only if it is
842 * currently mapped to some value.
843 *
844 * <p>The default implementation makes no guarantees about synchronization
845 * or atomicity properties of this method. Any implementation providing
|