public static <K extends Serializable, V extends Serializable> void replaceValueInCache( Cache<K, V> cache, V oldValue, V newValue )
    {
        if ( newValue.equals( oldValue ) )
        {
            return;
        }

        // In some cache implementations, the entry-set is unmodifiable. To guard against potential
        // future changes of this implementation (that would make the implementation incompatible with
        // these cache implementations), the entry-set that's operated on in this implementation is
        // explicitly wrapped in an unmodifiable collection. That forces this implementation to be
        // compatible with the 'lowest common denominator'.
        final Set<Map.Entry<K, V>> entries = Collections.unmodifiableSet( cache.entrySet() );

        for ( final Map.Entry<K, V> entry : entries )
        {
            final K key = entry.getKey();

            final Lock lock = cache.getLock( key );
            lock.lock();
            try
            {
                if ( entry.getValue().equals( oldValue ) )
                {
                    // The cluster-based cache needs an explicit 'put' to cause the change to propagate.
                    cache.put( entry.getKey(), newValue );
                }
            }
            finally
            {
                lock.unlock();
            }
        }
    }
Replaces all instances of a particular value in a cache.

Every instance of the old value that is found in a cache value is replaced by the new value.

The implementation of this method is designed to be compatible with both clustered as well as non-clustered caches.

@param cache    The cache from which to remove the element (cannot be null).
@param oldValue The element to be replaced (cannot be null).
@param newValue The replacement element (cannot be null).
@param <K> the type of key contained by the cache
@param <V> the type of value contained by the cache
xmppserver/src/main/java/org/jivesoftware/util/cache/CacheUtil.java