Class CacheUtil


  • public class CacheUtil
    extends Object
    Utility methods for working with caches. Different implementations of Cache have idiosyncrasies, which this implementation takes into account.
    Author:
    Guus der Kinderen, guus.der.kinderen@gmail.com
    • Constructor Detail

      • CacheUtil

        public CacheUtil()
    • Method Detail

      • addValueToMultiValuedCache

        public static <K extends Serializable,​V,​C extends Collection<V> & Serializable> void addValueToMultiValuedCache​(Cache<K,​C> cache,
                                                                                                                                    K key,
                                                                                                                                    V element,
                                                                                                                                    Supplier<C> supplier)
        Adds the specified element to a cache, in a collection that is mapped by the provided key. When the cache does not contain an entry for the provided key, a cache entry is added, which will have a new collection created using the provided Supplier, to which the specified element is added. The implementation of this method is designed to be compatible with both clustered as well as non-clustered caches.
        Type Parameters:
        K - the type of key contained by the cache
        V - the type of value contained by the cache
        C - the type of collection contained by the cache
        Parameters:
        cache - The cache from which to remove the element (cannot be null).
        element - The element to be added (can be null only if the value-Collection supports null values).
        key - The cache entry identifier (cannot be null)
        supplier - A provider of empty instances of the collection used as a value of the cache (in which elements are placed).
      • removeValueFromCache

        public static <K extends Serializable,​V extends SerializableSet<K> removeValueFromCache​(Cache<K,​V> cache,
                                                                                                        V element)
        Removes all entries of a cache that map to the provided value. The implementation of this method is designed to be compatible with both clustered as well as non-clustered caches.
        Type Parameters:
        K - the type of key contained by the cache
        V - the type of value contained by the cache
        Parameters:
        cache - The cache from which to remove the element (cannot be null).
        element - The element to be removed (can not be null ).
        Returns:
        a Set containing the keys of all affected cache entries (never null)
      • removeValueFromMultiValuedCache

        public static <K extends Serializable,​V,​C extends Collection<V> & Serializable> boolean removeValueFromMultiValuedCache​(Cache<K,​C> cache,
                                                                                                                                            K key,
                                                                                                                                            V element)
        Removes all instances of the specified element from the collection that is mapped by the provided key. When the element removed from the set leaves that set empty, the cache entry is removed completely. The implementation of this method is designed to be compatible with both clustered as well as non-clustered caches.
        Type Parameters:
        K - the type of key contained by the cache
        V - the type of value contained by the cache
        C - the type of collection contained by the cache
        Parameters:
        cache - The cache from which to remove the element (cannot be null).
        key - The cache entry identifier (cannot be null)
        element - The element to be removed (can be null only if the value-Collection supports null values).
        Returns:
        True if the collection was modified
      • removeValueFromMultiValuedCache

        public static <K extends Serializable,​V,​C extends Collection<V> & SerializableMap<Boolean,​Map<K,​C>> removeValueFromMultiValuedCache​(Cache<K,​C> cache,
                                                                                                                                                                      V element)
        Removes all instances of the specified element from every collection that is a value of the cache. When the element removed from the collection leaves that collection empty, the cache entry is removed completely. The implementation of this method is designed to be compatible with both clustered as well as non-clustered caches. The return value is a Map that contains all entries that were affected by the call. The returned has exactly two keys, that each have a Map for its value. - the Map that is the value of the 'false' key contains all entries that have been removed from the cache. - the Map that is the value of the 'true' key contains all entries that have been modified.
        Type Parameters:
        K - the type of key contained by the cache
        V - the type of value contained by the cache
        C - the type of collection contained by the cache
        Parameters:
        cache - The cache from which to remove the element (cannot be null).
        element - The element to be removed (can be null only if the value-Collection supports null values).
        Returns:
        a map containing all affected cache entries (never null)
      • retainValueInMultiValuedCache

        public static <K extends Serializable,​V,​C extends Collection<V> & SerializableMap<Boolean,​Map<K,​C>> retainValueInMultiValuedCache​(Cache<K,​C> cache,
                                                                                                                                                                    V element)
        Remove elements from every collection that is a value of the cache, except for the specified element. When removal leaves a collection empty, the cache entry is removed completely. The implementation of this method is designed to be compatible with both clustered as well as non-clustered caches. The return value is a Map that contains all entries that were affected by the call. The returned has exactly two keys, that each have a Map for its value. - the Map that is the value of the 'false' key contains all entries that have been removed from the cache. - the Map that is the value of the 'true' key contains all entries that have been modified.
        Type Parameters:
        K - the type of key contained by the cache
        V - the type of value contained by the cache
        C - the type of collection contained by the cache
        Parameters:
        cache - The cache in which to retain the element (cannot be null).
        element - The element to be retained (can be null only if the value-Collection supports null values).
        Returns:
        a map containing all affected cache entries (never null)
      • retainValueInCache

        public static <K extends Serializable,​V extends SerializableSet<K> retainValueInCache​(Cache<K,​V> cache,
                                                                                                      V element)
        Remove elements from every collection that is a value of the cache, except for the specified element. The implementation of this method is designed to be compatible with both clustered as well as non-clustered caches.
        Type Parameters:
        K - the type of key contained by the cache
        V - the type of value contained by the cache
        Parameters:
        cache - The cache in which to retain the element (cannot be null).
        element - The element to be retained (cannot be null).
        Returns:
        a Set containing the keys of all affected cache entries (never null)
      • replaceValueInCache

        public static <K extends Serializable,​V extends Serializable> void replaceValueInCache​(Cache<K,​V> cache,
                                                                                                     V oldValue,
                                                                                                     V newValue)
        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.
        Type Parameters:
        K - the type of key contained by the cache
        V - the type of value contained by the cache
        Parameters:
        cache - The cache from which to remove the element (cannot be null).
        oldValue - The element to be replaced (cannot be null).
        newValue - The replacement element (cannot be null).
      • replaceValueInCacheByMapping

        public static <K extends Serializable,​V extends Serializable> void replaceValueInCacheByMapping​(Cache<K,​V> cache,
                                                                                                              Function<V,​V> mapper)
        Applies a mapping function to all values in a cache. The provided mapping function is applied to all values in the cache. A cache modification is made only when the mapping function returns a value that is not equal to the original value. The implementation of this method is designed to be compatible with both clustered as well as non-clustered caches.
        Type Parameters:
        K - the type of key contained by the cache
        V - the type of value contained by the cache
        Parameters:
        cache - The cache from which to remove the element (cannot be null).
        mapper - The mapping function (cannot be null).
      • replaceValueInMultivaluedCache

        public static <K extends Serializable,​V,​C extends Collection<V> & Serializable> void replaceValueInMultivaluedCache​(Cache<K,​C> cache,
                                                                                                                                        V oldValue,
                                                                                                                                        V newValue)
        Replaces an element in a cache that has collection-based values. Every instance of the old value that is found in a collection-based value is removed, and for each such removal, the new value is added to the cache. Note that no guarantees regarding collection order are given. Cache entries for which the value-collection does contain the old value are left unchanged. The implementation of this method is designed to be compatible with both clustered as well as non-clustered caches.
        Type Parameters:
        K - the type of key contained by the cache
        V - the type of value contained by the cache
        C - the type of collection contained by the cache
        Parameters:
        cache - The cache from which to remove the element (cannot be null).
        oldValue - The element to be replaced (can be null only if the value-Collection supports null values).
        newValue - The replacement element (can be null only if the value-Collection supports null values).