@Override public synchronized boolean containsKey(K key) { V val = get(key); return (val != null); }
/** This convenience method filters a request by removing all items in the request which * are in the cache and returning the corresponding values. * * Synchronization note: this method is not synchronized on the cache. Thus, if replacements * are performed during a canonicalization, it is undefined which object is returned. Similarly, * this function is not synchronized on the request collection, so if synchronization is required, * it must be performed externally. * * @param request The list of items to be fetched from the backing store. This collection must * be modifiable. */ public Collection<V> filter(Collection<K> request) { Collection<V> rv = new ArrayList<V>(); for (Iterator<K> iter = request.iterator(); iter.hasNext();) { K key = iter.next(); V val = get(key); if (val != null) { rv.add(val); iter.remove(); } } return rv; }
/** This convenience method takes a map of items returned from the backing store and replaces * references loaded from the backing store with items in the cache. * * A call to canonicalize will typically be followed by a putAll on the returnVal, so that * future requests to the cache will return the new items loaded. * * Synchronization note: this method is not synchronized on the cache. Thus, if replacements * are performed during a canonicalization, it is undefined which object is returned. Similarly, * this function is not synchronized on the returnVal map, so if synchronization is required, it * must be performed externally. * * @param returnVal The map of items to be canonicalized. This map must be modifiable. */ public void canonicalize(Map<K, V> returnVal) { Map<K, V> canonicalizedMappings = new HashMap<K, V>(); for (Iterator<Map.Entry<K, V> > iter = returnVal.entrySet().iterator(); iter.hasNext();) { Map.Entry<K, V> entry = iter.next(); V myValue = get(entry.getKey()); if (myValue != null) { iter.remove(); canonicalizedMappings.put(entry.getKey(), myValue); } } returnVal.putAll(canonicalizedMappings); }
/** * If the specified key is not already associated with a value, associate it with the given value. This is equivalent to * * @param key * @param value * @return The value that was in the cache, null if none was there before */ public synchronized V putIfAbsent(K key, V value) { if (!containsKey(key)) { return put(key, value); } else { return get(key); } }
@Override public synchronized boolean containsKey(K key) { V val = get(key); return (val != null); }
/** This convenience method filters a request by removing all items in the request which * are in the cache and returning the corresponding values. * * Synchronization note: this method is not synchronized on the cache. Thus, if replacements * are performed during a canonicalization, it is undefined which object is returned. Similarly, * this function is not synchronized on the request collection, so if synchronization is required, * it must be performed externally. * * @param request The list of items to be fetched from the backing store. This collection must * be modifiable. */ public Collection<V> filter(Collection<K> request) { Collection<V> rv = new ArrayList<V>(); for (Iterator<K> iter = request.iterator(); iter.hasNext();) { K key = iter.next(); V val = get(key); if (val != null) { rv.add(val); iter.remove(); } } return rv; }
/** This convenience method takes a map of items returned from the backing store and replaces * references loaded from the backing store with items in the cache. * * A call to canonicalize will typically be followed by a putAll on the returnVal, so that * future requests to the cache will return the new items loaded. * * Synchronization note: this method is not synchronized on the cache. Thus, if replacements * are performed during a canonicalization, it is undefined which object is returned. Similarly, * this function is not synchronized on the returnVal map, so if synchronization is required, it * must be performed externally. * * @param returnVal The map of items to be canonicalized. This map must be modifiable. */ public void canonicalize(Map<K, V> returnVal) { Map<K, V> canonicalizedMappings = new HashMap<K, V>(); for (Iterator<Map.Entry<K, V> > iter = returnVal.entrySet().iterator(); iter.hasNext();) { Map.Entry<K, V> entry = iter.next(); V myValue = get(entry.getKey()); if (myValue != null) { iter.remove(); canonicalizedMappings.put(entry.getKey(), myValue); } } returnVal.putAll(canonicalizedMappings); }
/** * If the specified key is not already associated with a value, associate it with the given value. This is equivalent to * * @param key * @param value * @return The value that was in the cache, null if none was there before */ public synchronized V putIfAbsent(K key, V value) { if (!containsKey(key)) { return put(key, value); } else { return get(key); } }