Class CachedMap
- All Implemented Interfaces:
- Map
 This class provides cache access to Map collections.
 
 Instance of this class can be used as "proxy" for any collection implementing the java.util.Map
 interface.
 
 Typically, CachedMap are used to accelerate access to large collections when the access to the collection is
 not evenly distributed (associative cache). The performance gain is about 50% for the fastest hash map collections
 (e.g. FastMap). For slower collections such as java.util.TreeMap, non-resizable FastMap
 (real-time) or database access, performance can be of several orders of magnitude.
 
 Note: The keys used to access elements of a CachedMap do not need to be immutable as they are not
 stored in the cache (only keys specified by the put(java.lang.Object, java.lang.Object) method are). In other words, access can be performed
 using mutable keys as long as these keys can be compared for equality with the real map's keys (e.g. same
 hashCode values).
 
This implementation is not synchronized. Multiple threads accessing or modifying the collection must be synchronized externally.
This class is public domain (not copyrighted).
- Version:
- 5.3, October 30, 2003
- Author:
- Jean-Marie Dautelle
- 
Nested Class Summary
- 
Constructor SummaryConstructors
- 
Method SummaryModifier and TypeMethodDescriptionvoidclear()Removes all mappings from this map (optional operation).booleancontainsKey(Object key) Indicates if this map contains a mapping for the specified key.booleancontainsValue(Object value) Returnstrueif this map maps one or more keys to the specified value.entrySet()Returns an unmodifiable view of the mappings contained in this map.booleanCompares the specified object with this map for equality.voidflush()Flushes the key/value pairs being cached.Returns the value to which this map maps the specified key.Returns the backing map.intReturns the actual cache size.inthashCode()Returns the hash code value for this map.booleanisEmpty()Returnstrueif this map contains no key-value mappings.keySet()Returns an unmodifiable view of the keys contained in this map.Associates the specified value with the specified key in this map.voidCopies all of the mappings from the specified map to this map (optional operation).Removes the mapping for this key from this map if it is present.intsize()Returns the number of key-value mappings in this map.values()Returns an unmodifiable view of the values contained in this map.Methods inherited from class java.lang.Objectclone, finalize, getClass, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface java.util.Mapcompute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, putIfAbsent, remove, replace, replace, replaceAll
- 
Constructor Details- 
CachedMappublic CachedMap()Creates a cached map backed by aFastMap. The default cache size and map capacity is set to256entries.
- 
CachedMappublic CachedMap(int cacheSize) Creates a cached map backed by aFastMapand having the specified cache size.- Parameters:
- cacheSize- the cache size, the actual cache size is the first power of 2 greater or equal to- cacheSize. This is also the initial capacity of the backing map.
 
- 
CachedMapCreates a cached map backed by the specified map and having the specified cache size. In order to maintain cache veracity, it is critical that all update to the backing map is accomplished through theCachedMapinstance; otherwiseflush()has to be called.- Parameters:
- cacheSize- the cache size, the actual cache size is the first power of 2 greater or equal to- cacheSize.
- backingMap- the backing map to be "wrapped" in a cached map.
 
 
- 
- 
Method Details- 
getCacheSizepublic int getCacheSize()Returns the actual cache size.- Returns:
- the cache size (power of 2).
 
- 
getBackingMapReturns the backing map. If the backing map is modified directly, thisCachedMaphas to be flushed.- Returns:
- the backing map.
- See Also:
 
- 
flushpublic void flush()Flushes the key/value pairs being cached. This method should be called if the backing map is externally modified.
- 
getReturns the value to which this map maps the specified key. First, the cache is being checked, then if the cache does not contains the specified key, the backing map is accessed and the key/value pair is stored in the cache.- Specified by:
- getin interface- Map
- Parameters:
- key- the key whose associated value is to be returned.
- Returns:
- the value to which this map maps the specified key, or nullif the map contains no mapping for this key.
- Throws:
- ClassCastException- if the key is of an inappropriate type for the backing map (optional).
- NullPointerException- if the key is- null.
 
- 
putAssociates the specified value with the specified key in this map.- Specified by:
- putin interface- Map
- Parameters:
- key- the key with which the specified value is to be associated.
- value- the value to be associated with the specified key.
- Returns:
- the previous value associated with specified key, or nullif there was no mapping for the key.
- Throws:
- UnsupportedOperationException- if the- putoperation is not supported by the backing map.
- ClassCastException- if the class of the specified key or value prevents it from being stored in this map.
- IllegalArgumentException- if some aspect of this key or value prevents it from being stored in this map.
- NullPointerException- if the key is- null.
 
- 
removeRemoves the mapping for this key from this map if it is present.- Specified by:
- removein interface- Map
- Parameters:
- key- key whose mapping is to be removed from the map.
- Returns:
- previous value associated with specified key, or nullif there was no mapping for key.
- Throws:
- ClassCastException- if the key is of an inappropriate type for the backing map (optional).
- NullPointerException- if the key is- null.
- UnsupportedOperationException- if the- removemethod is not supported by the backing map.
 
- 
containsKeyIndicates if this map contains a mapping for the specified key.- Specified by:
- containsKeyin interface- Map
- Parameters:
- key- the key whose presence in this map is to be tested.
- Returns:
- trueif this map contains a mapping for the specified key;- falseotherwise.
 
- 
sizepublic int size()Returns the number of key-value mappings in this map. If the map contains more thanInteger.MAX_VALUEelements, returnsInteger.MAX_VALUE.
- 
isEmptypublic boolean isEmpty()Returnstrueif this map contains no key-value mappings.
- 
containsValueReturnstrueif this map maps one or more keys to the specified value.- Specified by:
- containsValuein interface- Map
- Parameters:
- value- value whose presence in this map is to be tested.
- Returns:
- trueif this map maps one or more keys to the specified value.
- Throws:
- ClassCastException- if the value is of an inappropriate type for the backing map (optional).
- NullPointerException- if the value is- nulland the backing map does not not permit- nullvalues.
 
- 
putAllCopies all of the mappings from the specified map to this map (optional operation). This method automatically flushes the cache.- Specified by:
- putAllin interface- Map
- Parameters:
- map- the mappings to be stored in this map.
- Throws:
- UnsupportedOperationException- if the- putAllmethod is not supported by the backing map.
- ClassCastException- if the class of a key or value in the specified map prevents it from being stored in this map.
- IllegalArgumentException- some aspect of a key or value in the specified map prevents it from being stored in this map.
- NullPointerException- the specified map is- null, or if the backing map does not permit- nullkeys or values, and the specified map contains- nullkeys or values.
 
- 
clearpublic void clear()Removes all mappings from this map (optional operation). This method automatically flushes the cache.- Specified by:
- clearin interface- Map
- Throws:
- UnsupportedOperationException- if clear is not supported by the backing map.
 
- 
keySetReturns an unmodifiable view of the keys contained in this map.
- 
valuesReturns an unmodifiable view of the values contained in this map.
- 
entrySetReturns an unmodifiable view of the mappings contained in this map. Each element in the returned set is aMap.Entry.
- 
equalsCompares the specified object with this map for equality. Returnstrueif the given object is also a map and the two Maps represent the same mappings.
- 
hashCodepublic int hashCode()Returns the hash code value for this map.
 
-