public class Cache extends Dictionary implements Map
The class Cache
provides 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 for
Cache
will 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-null
object 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 hashCode
method and the equals
method.
An instance of Cache
has two parameters that affect its
performance: capacity and load factor. The
capacity is the number of buckets in the hash table, and the
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 the
rehash
method.
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 rehash
operations, which are time-consuming.
No rehash
operations 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.
Object.equals(java.lang.Object)
,
Object.hashCode()
,
Collection
,
Map
Constructor and Description |
---|
Cache()
Constructs a new, empty Cache with a default capacity and load factor,
which is 0.75.
|
Cache(int capacity)
Constructs a new, empty Cache with the specified capacity and default
load factor, which is 0.75.
|
Cache(int capacity,
float loadFactor)
Constructs a new, empty Cache with the specified capacity and the
specified load factor.
|
Modifier and Type | Method and Description |
---|---|
protected void |
adjustEntry(Object key)
This method adjusts the table by removing the entry corresponding to key
from the table.
|
String |
audit() |
void |
clear()
Clears this Cache so that it contains no keys.
|
boolean |
contains(Object value)
Tests if some key maps into the specified value in this Cache.
|
boolean |
containsKey(Object key)
Tests if the specified object is a key in this Cache.
|
boolean |
containsValue(Object value)
Returns true if this Cache maps one or more keys to this value.
|
Enumeration |
elements()
Returns an enumeration of the values in this Cache.
|
Set |
entrySet()
Returns a Set view of the entries contained in this Cache.
|
boolean |
equals(Object o)
Compares the specified Object with this Map for equality, as per the
definition in the Map interface.
|
Object |
get(Object key)
Returns the value to which the specified key is mapped in this Cache.
|
int |
hashCode()
Returns the hash code value for this Map as per the definition in the Map
interface.
|
boolean |
isEmpty()
Tests if this Cache maps no keys to values.
|
Enumeration |
keys()
Returns an enumeration of the keys in this Cache.
|
Set |
keySet()
Returns a Set view of the keys contained in this Cache.
|
Object |
put(Object key,
Object value)
Maps the specified
key to the specified value
in this Cache. |
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.
|
protected void |
rehash()
Increases the capacity of and internally reorganizes this Cache, in order
to accommodate and access its entries more efficiently.
|
Object |
remove(Object key)
Removes the key (and its corresponding value) from this Cache.
|
int |
size()
Returns the number of keys in this Cache.
|
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).
|
Collection |
values()
Returns a Collection view of the values contained in this Cache.
|
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, putIfAbsent, remove, replace, replace, replaceAll
public Cache(int capacity, float loadFactor)
capacity
- the capacity of the Cache.loadFactor
- the load factor of the Cache.IllegalArgumentException
- if the capacity is less than zero, or if the load factor
is nonpositive.public Cache(int capacity)
capacity
- the capacity of the Cache.IllegalArgumentException
- if the capacity is less than zero.public Cache()
public int size()
size
in interface Map
size
in class Dictionary
public boolean isEmpty()
isEmpty
in interface Map
isEmpty
in class Dictionary
true
if this Cache maps no keys to values;
false
otherwise.public Enumeration keys()
keys
in class Dictionary
Enumeration
,
elements()
,
keySet()
,
Map
public Enumeration elements()
elements
in class Dictionary
Enumeration
,
keys()
,
values()
,
Map
public boolean contains(Object value)
containsKey
method.
Note that this method is identical in functionality to containsValue, (which is part of the Map interface in the PolicyCollections framework).
value
- a value to search for.true
if and only if some key maps to the
value
argument in this Cache as determined by the
equals method; false
otherwise.NullPointerException
- if the value is null
.containsKey(Object)
,
containsValue(Object)
,
Map
public boolean containsValue(Object value)
Note that this method is identical in functionality to contains (which predates the Map interface).
containsValue
in interface Map
value
- value whose presence in this Cache is to be tested.Map
public boolean containsKey(Object key)
containsKey
in interface Map
key
- possible key.true
if and only if the specified object is a key
in this Cache, as determined by the equals method;
false
otherwise.contains(Object)
public Object get(Object key)
get
in interface Map
get
in class Dictionary
key
- a key in the Cache.null
if the key is not mapped to any value in this
Cache.put(Object, Object)
protected void rehash()
public Object put(Object key, Object value)
key
to the specified value
in this Cache. Neither the key nor the value can be null
.
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 get
method with
a key that is equal to the original key.
put
in interface Map
put
in class Dictionary
key
- the Cache key.value
- the value.null
if it did not have one.NullPointerException
- if the key or value is null
.Object.equals(Object)
,
get(Object)
protected void adjustEntry(Object key)
public Object remove(Object key)
remove
in interface Map
remove
in class Dictionary
key
- the key that needs to be removed.null
if the key did not have a mapping.public void putAll(Map t)
public void clear()
public String toString()
Overrides to toString method of Object.
public String audit()
public Set keySet()
public Set entrySet()
public Collection values()
public boolean equals(Object o)
equals
in interface Map
equals
in class Object
Map.equals(Object)
public int hashCode()
hashCode
in interface Map
hashCode
in class Object
Map.hashCode()
Copyright © 2010–2025 Open Identity Platform Community. All rights reserved.