Class Cache
- java.lang.Object
-
- java.util.Dictionary
-
- com.sun.identity.entitlement.opensso.Cache
-
- All Implemented Interfaces:
Map
public class Cache extends Dictionary implements Map
IMPORTANT NOTE: The cache has be implemented by modifing the existing java.util.Hashtable code. Its has added functionality of a built in replacement strategy which in this case is based on Least Recently Used concept. The synchronization functionality that exists in a Hashtable has been removed to improve performance.The class
Cacheprovides the functionality to cache objects based on their usage. The maximum size of the cache can be set using the constructor. If the maximum size is not set the default cache size forCachewill be obtained from the config file???file, defined using the key???. The object that needs to be cached can be supplied to the instance of this class using the put method. The object can be obtained by invoking the get method on the instance. Each object that is cached is tracked based on its usage. If a new object needs to added to the cache and the maximum size limit of the cache is reached, then the least recently used object is replaced. This class implements a Cache, which maps keys to values. Any non-nullobject can be used as a key or as a value.To successfully store and retrieve objects from a Cache, the objects used as keys must implement the
hashCodemethod and theequalsmethod.An instance of
Cachehas three parameters that affect its performance: initial capacity, maximum size and load factor. The maximum capacity is the maximum number of buckets in the hash table, and the initial capacity is simply the capacity at the time the hash table is created. Note that the hash table is open: in the case a "hash collision", a single bucket stores multiple entries, which must be searched sequentially. The load factor is a measure of how full the hash table is allowed to get before its capacity is automatically increased. When the number of entries in the Cache exceeds the product of the load factor and the current capacity, the capacity is increased by calling therehashmethod.Generally, the default load factor (.75) offers a good tradeoff between time and space costs. Higher values decrease the space overhead but increase the time cost to look up an entry (which is reflected in most Cache operations, including get and put).
The capacity controls a tradeoff between wasted space and the need for
rehashoperations, which are time-consuming. Norehashoperations will ever occur if the capacity is greater than the maximum number of entries the Cache will contain divided by its load factor. However, setting the capacity too high can waste space.If many entries are to be made into a
Cache, creating it with a sufficiently large capacity may allow the entries to be inserted more efficiently than letting it perform automatic rehashing as needed to grow the table.This class has been retrofitted to implement Map, so that it becomes a part of Java's collection framework. The Iterators returned by the iterator and listIterator methods of the Collections returned by all of Cache's "collection view methods" are fail-fast: if the Cache is structurally modified at any time after the Iterator is created, in any way except through the Iterator's own remove or add methods, the Iterator will throw a ConcurrentModificationException. Thus, in the face of concurrent modification, the Iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future. The Enumerations returned by Cache's keys and values methods are not fail-fast.
- Since:
- JDK1.0
- See Also:
Object.equals(java.lang.Object),Object.hashCode(),Collection,Map
-
-
Constructor Summary
Constructors Constructor Description Cache()Cache(String name, int initCapacity, int maxSize)Constructs a new, empty Cache with the specified capacity and default load factor, which is 0.75.Cache(String name, int initCapacity, int maxSize, float loadFactor)Constructs a new, empty Cache with the specified capacity and the specified load factor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voidadjustEntry(Object key)This method adjusts the table by removing the entry corresponding to key from the table.Stringaudit()voidclear()Clears this Cache so that it contains no keys.booleancontains(Object value)Tests if some key maps into the specified value in this Cache.booleancontainsKey(Object key)Tests if the specified object is a key in this Cache.booleancontainsValue(Object value)Returns true if this Cache maps one or more keys to this value.Enumerationelements()Returns an enumeration of the values in this Cache.SetentrySet()Returns a Set view of the entries contained in this Cache.booleanequals(Object o)Compares the specified Object with this Map for equality, as per the definition in the Map interface.Objectget(Object key)Returns the value to which the specified key is mapped in this Cache.StringgetName()Returns name.inthashCode()Returns the hash code value for this Map as per the definition in the Map interface.booleanisEmpty()Tests if this Cache maps no keys to values.Enumerationkeys()Returns an enumeration of the keys in this Cache.SetkeySet()Returns a Set view of the keys contained in this Cache.Objectput(Object key, Object value)Maps the specifiedkeyto the specifiedvaluein this Cache.voidputAll(Map t)Copies all of the mappings from the specified Map to this Hashtable These mappings will replace any mappings that this Hashtable had for any of the keys currently in the specified Map.protected voidrehash()Increases the capacity of and internally reorganizes this Cache, in order to accommodate and access its entries more efficiently.Objectremove(Object key)Removes the key (and its corresponding value) from this Cache.intsize()Returns the number of keys in this Cache.StringtoString()Returns a string representation of this Cache object in the form of a set of entries, enclosed in braces and separated by the ASCII characters ", " (comma and space).Collectionvalues()Returns a Collection view of the values contained in this Cache.-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface java.util.Map
compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, putIfAbsent, remove, replace, replace, replaceAll
-
-
-
-
Constructor Detail
-
Cache
public Cache(String name, int initCapacity, int maxSize, float loadFactor)
Constructs a new, empty Cache with the specified capacity and the specified load factor.- Parameters:
name- Name of cache.initCapacity- the capacity of the Cache.maxSize- the maximum size of the cache.loadFactor- the load factor of the Cache.- Throws:
IllegalArgumentException- if the capacity is less than zero, or if the load factor is nonpositive.
-
Cache
public Cache(String name, int initCapacity, int maxSize)
Constructs a new, empty Cache with the specified capacity and default load factor, which is 0.75.- Parameters:
name- Name of cache.initCapacity- the initial capacity of the Cache.maxSize- the maximum size of the cache.- Throws:
IllegalArgumentException- if the capacity is less than zero.
-
Cache
public Cache()
-
-
Method Detail
-
getName
public String getName()
Returns name.- Returns:
- name.
-
size
public int size()
Returns the number of keys in this Cache.- Specified by:
sizein interfaceMap- Specified by:
sizein classDictionary- Returns:
- the number of keys in this Cache.
-
isEmpty
public boolean isEmpty()
Tests if this Cache maps no keys to values.- Specified by:
isEmptyin interfaceMap- Specified by:
isEmptyin classDictionary- Returns:
trueif this Cache maps no keys to values;falseotherwise.
-
keys
public Enumeration keys()
Returns an enumeration of the keys in this Cache.- Specified by:
keysin classDictionary- Returns:
- an enumeration of the keys in this Cache.
- See Also:
Enumeration,elements(),keySet(),Map
-
elements
public Enumeration elements()
Returns an enumeration of the values in this Cache. Use the Enumeration methods on the returned object to fetch the elements sequentially.- Specified by:
elementsin classDictionary- Returns:
- an enumeration of the values in this Cache.
- See Also:
Enumeration,keys(),values(),Map
-
contains
public boolean contains(Object value)
Tests if some key maps into the specified value in this Cache. This operation is more expensive than thecontainsKeymethod.Note that this method is identical in functionality to containsValue, (which is part of the Map interface in the PolicyCollections framework).
- Parameters:
value- a value to search for.- Returns:
trueif and only if some key maps to thevalueargument in this Cache as determined by the equals method;falseotherwise.- Throws:
NullPointerException- if the value isnull.- See Also:
containsKey(Object),containsValue(Object),Map
-
containsValue
public boolean containsValue(Object value)
Returns true if this Cache maps one or more keys to this value.Note that this method is identical in functionality to contains (which predates the Map interface).
- Specified by:
containsValuein interfaceMap- Parameters:
value- value whose presence in this Cache is to be tested.- Since:
- JDK1.2
- See Also:
Map
-
containsKey
public boolean containsKey(Object key)
Tests if the specified object is a key in this Cache.- Specified by:
containsKeyin interfaceMap- Parameters:
key- possible key.- Returns:
trueif and only if the specified object is a key in this Cache, as determined by the equals method;falseotherwise.- See Also:
contains(Object)
-
get
public Object get(Object key)
Returns the value to which the specified key is mapped in this Cache.- Specified by:
getin interfaceMap- Specified by:
getin classDictionary- Parameters:
key- a key in the Cache.- Returns:
- the value to which the key is mapped in this Cache;
nullif the key is not mapped to any value in this Cache. - See Also:
put(Object, Object)
-
rehash
protected void rehash()
Increases the capacity of and internally reorganizes this Cache, in order to accommodate and access its entries more efficiently. This method is called automatically when the number of keys in the Cache exceeds this Cache's capacity and load factor. It ensures the new capacity does not exceed the maximum size.
-
put
public Object put(Object key, Object value)
Maps the specifiedkeyto the specifiedvaluein this Cache. Neither the key nor the value can benull. If the cache is full to its capacity, then the least recently used entry in the cache will be replaced.The value can be retrieved by calling the
getmethod with a key that is equal to the original key.- Specified by:
putin interfaceMap- Specified by:
putin classDictionary- Parameters:
key- the Cache key.value- the value.- Returns:
- the previous value of the specified key in this Cache, or
nullif it did not have one. - Throws:
NullPointerException- if the key or value isnull.- See Also:
Object.equals(Object),get(Object)
-
adjustEntry
protected void adjustEntry(Object key)
This method adjusts the table by removing the entry corresponding to key from the table.
-
remove
public Object remove(Object key)
Removes the key (and its corresponding value) from this Cache. This method does nothing if the key is not in the Cache.- Specified by:
removein interfaceMap- Specified by:
removein classDictionary- Parameters:
key- the key that needs to be removed.- Returns:
- the value to which the key had been mapped in this Cache, or
nullif the key did not have a mapping.
-
putAll
public void putAll(Map t)
Copies all of the mappings from the specified Map to this Hashtable These mappings will replace any mappings that this Hashtable had for any of the keys currently in the specified Map.
-
clear
public void clear()
Clears this Cache so that it contains no keys.
-
toString
public String toString()
Returns a string representation of this Cache object in the form of a set of entries, enclosed in braces and separated by the ASCII characters ", " (comma and space). Each entry is rendered as the key, an equals sign =, and the associated element, where the toString method is used to convert the key and element to strings.Overrides to toString method of Object.
-
audit
public String audit()
-
keySet
public Set keySet()
Returns a Set view of the keys contained in this Cache. The Set is backed by the Cache, so changes to the Cache are reflected in the Set, and vice-versa. The Set supports element removal (which removes the corresponding entry from the Cache), but not element addition.
-
entrySet
public Set entrySet()
Returns a Set view of the entries contained in this Cache. Each element in this collection is a Map.Entry. The Set is backed by the Cache, so changes to the Cache are reflected in the Set, and vice-versa. The Set supports element removal (which removes the corresponding entry from the Cache), but not element addition.
-
values
public Collection values()
Returns a Collection view of the values contained in this Cache. The Collection is backed by the Cache, so changes to the Cache are reflected in the Collection, and vice-versa. The Collection supports element removal (which removes the corresponding entry from the Cache), but not element addition.
-
equals
public boolean equals(Object o)
Compares the specified Object with this Map for equality, as per the definition in the Map interface.- Specified by:
equalsin interfaceMap- Overrides:
equalsin classObject- Returns:
- true if the specified Object is equal to this Map.
- Since:
- JDK1.2
- See Also:
Map.equals(Object)
-
hashCode
public int hashCode()
Returns the hash code value for this Map as per the definition in the Map interface.- Specified by:
hashCodein interfaceMap- Overrides:
hashCodein classObject- Since:
- JDK1.2
- See Also:
Map.hashCode()
-
-