/** * Removes the value mapped to the given key in the cache. If a value is under computation in another thread, * then the other thread may fail with an {@link IllegalStateException} unless {@link #isKeyCollisionAllowed()} * returns {@code true}. For more safety, consider using {@link #remove(Object, Object)} instead. * * @param key the key of the value to removed. * @return the value previously mapped to the given key, or {@code null} if no value existed before this * method call or if the value was under computation in another thread. * * @see #get(Object) * @see #remove(Object, Object) */ @Override @SuppressWarnings("unchecked") public V remove(final Object key) { final Object oldValue = map.remove(key); if (oldValue != null) { notifyChange((K) key, null); } return immediateValueOf(oldValue); }
/** * If the given key is mapped to any value, replaces that value with the given new value. * Otherwise does nothing. A null {@code value} argument removes the entry. * If a different value is under computation in another thread, then the other thread may fail with * an {@link IllegalStateException} unless {@link #isKeyCollisionAllowed()} returns {@code true}. * * @param key key of the value to replace. * @param value the new value to use in replacement of the previous one, or {@code null} for removing the mapping. * @return the value previously mapped to the given key, or {@code null} if no value existed before this * method call or if the value was under computation in another thread. * * @see #replace(Object, Object, Object) * * @since 1.0 */ @Override public V replace(final K key, final V value) { ensureValidType(value); final Object previous = (value != null) ? map.replace(key, value) : map.remove(key); if (previous != null) { // A null value means that 'replace' did nothing. notifyChange(key, value); } return immediateValueOf(previous); }
/** * Puts the given value in cache and immediately returns the old value. * A null {@code value} argument removes the entry. If a different value is under computation in another thread, * then the other thread may fail with an {@link IllegalStateException} unless {@link #isKeyCollisionAllowed()} * returns {@code true}. For more safety, consider using {@link #putIfAbsent putIfAbsent(…)} instead. * * @param key the key to associate with a value. * @param value the value to associate with the given key, or {@code null} for removing the mapping. * @return the value previously mapped to the given key, or {@code null} if no value existed before this * method call or if the value was under computation in another thread. * * @see #get(Object) * @see #putIfAbsent(Object, Object) */ @Override public V put(final K key, final V value) { ensureValidType(value); final Object previous = (value != null) ? map.put(key, value) : map.remove(key); if (previous != value) { notifyChange(key, value); } return immediateValueOf(previous); }