/** * Removes the value associated to the given key in the cache. * * @param key the key of the value to removed. * @return the value that were associated to the given key, or {@code null} if none. */ @Override public V remove(final Object key) { return Cache.<V>valueOf(map.remove(key)); }
/** * Returns the value associated to the given key in the cache. This method is similar to * {@link #peek} except that it blocks if the value is currently under computation in an * other thread. * * @param key the key of the value to get. * @return the value associated to the given key, or {@code null} if none. */ @Override public V get(final Object key) { return Cache.<V>valueOf(map.get(key)); }
/** * Returns the value mapped to the given key in the cache, potentially waiting for computation to complete. * This method is similar to {@link #peek(Object)} except that it blocks if the value is currently under * computation in another thread. * * @param key the key of the value to get. * @return the value mapped to the given key, or {@code null} if none. * * @see #peek(Object) * @see #containsKey(Object) * @see #computeIfAbsent(Object, Function) */ @Override public V get(final Object key) { return valueOf(map.get(key)); }
/** * If no value is already mapped and no value is under computation for the given key, puts the given value * in the cache. Otherwise returns the current value (potentially blocking until the computation finishes). * A null {@code value} argument is equivalent to a no-op. Otherwise a {@code null} return value means that * the given {@code value} has been stored in the {@code Cache}. * * @param key the key to associate with a value. * @param value the value to associate with the given key if no value already exists, or {@code null}. * @return the existing value mapped to the given key, or {@code null} if none existed before this method call. * * @see #get(Object) * @see #computeIfAbsent(Object, Function) * * @since 1.0 */ @Override public V putIfAbsent(final K key, final V value) { if (value == null) { return null; } ensureValidType(value); final Object previous = map.putIfAbsent(key, value); if (previous == null) { // A non-null value means that 'putIfAbsent' did nothing. notifyChange(key, value); } return valueOf(previous); }
/** * Puts the given value in cache. * * @param key the key for which to set a value. * @param value the value to store. * @return the value previously stored at the given key, or {@code null} if none. */ @Override public V put(final K key, final V value) { if (isReservedType(value)) { throw new IllegalArgumentException(Errors.format( Errors.Keys.IllegalArgumentClass_2, "value", value.getClass())); } final Object previous; if (value != null) { previous = map.put(key, value); DelayedExecutor.schedule(new Strong(key, value)); } else { previous = map.remove(key); } return Cache.<V>valueOf(previous); }
/** * Replaces the value mapped to the given key by a new value computed from the old value. * If a value for the given key is under computation in another thread, then this method * blocks until that computation is completed. This is equivalent to the work performed * by {@link #replaceAll replaceAll(…)} but on a single entry. * * @param key key of the value to replace. * @param remapping the function computing new values from the old ones. * @return the new value associated with the given key. * * @see #computeIfAbsent(Object, Function) * * @since 1.0 */ @Override public V computeIfPresent(final K key, final BiFunction<? super K, ? super V, ? extends V> remapping) { final ReplaceAdapter adapter = new ReplaceAdapter(remapping); final Object value = map.computeIfPresent(key, adapter); Deferred.notifyChanges(this, adapter.changes); return valueOf(value); }
/** * Replaces the value mapped to the given key by a new value computed from the old value. * If there is no value for the given key, then the "old value" is taken as {@code null}. * If a value for the given key is under computation in another thread, then this method * blocks until that computation is completed. This method is equivalent to * {@link #computeIfPresent computeIfPresent(…)} except that a new value will be computed * even if no value existed for the key before this method call. * * @param key key of the value to replace. * @param remapping the function computing new values from the old ones, or from a {@code null} value. * @return the new value associated with the given key. * * @see #computeIfAbsent(Object, Function) * * @since 1.0 */ @Override public V compute(final K key, final BiFunction<? super K, ? super V, ? extends V> remapping) { final ReplaceAdapter adapter = new ReplaceAdapter(remapping); final Object value = map.compute(key, adapter); Deferred.notifyChanges(this, adapter.changes); return valueOf(value); }
final Object newValue = map.merge(key, value, adapter); Deferred.notifyChanges(this, adapter.changes); return valueOf(newValue);