Package gov.nasa.worldwind.cache
Class BasicSessionCache
- java.lang.Object
-
- gov.nasa.worldwind.cache.BasicSessionCache
-
- All Implemented Interfaces:
SessionCache
public class BasicSessionCache extends java.lang.Object implements SessionCache
BasicSessionCache is a general receiving area for data represented as key-value pairs. Entries in a BasicSessionCache may persist for the length of a Virtual Machine's run time, but may be evicted if the cache size increases beyond its capacity.Eviction of BasicSessionCache entries is accomplished by controlling the maximum number of entries in the cache. This maximum value is set by calling
setCapacity(int)
. The eldest entry in the cache (the first entry added) is always evicted before any others.BasicSessionClass is a thread safe class. Access to the cache data structures is synchronized at the method level. Care must be taken by subclasses to ensure that method level synchronization is maintained.
-
-
Field Summary
Fields Modifier and Type Field Description protected static int
DEFAULT_CAPACITY
protected BoundedHashMap<java.lang.Object,java.lang.Object>
entries
-
Constructor Summary
Constructors Constructor Description BasicSessionCache()
Creates a BasicSessionCache with the default capacity.BasicSessionCache(int capacity)
Creates a BasicSessionCache with a specified maximum number of entries.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
clear()
Removes all entries from the cache.boolean
contains(java.lang.Object key)
Returns true if the cache contains a specified key, and false if it does not.java.lang.Object
get(java.lang.Object key)
Returns a reference to an entry's value in the cache corresponding to a specified key, or null if no entry with that key exists.int
getCapacity()
Returns the maximum number of entries in the cache.int
getEntryCount()
Returns the number of entries currently in the cache.java.util.Set<java.lang.Object>
getKeySet()
Returns aSet
view of the keys contained in the cache.void
put(java.lang.Object key, java.lang.Object value)
Adds an entry in the cache with a specified key and value.java.lang.Object
remove(java.lang.Object key)
Removes the entry with the specified key from the cache, and returns that entry's value.void
setCapacity(int capacity)
Sets the maximum number of entries in the cache.
-
-
-
Field Detail
-
DEFAULT_CAPACITY
protected static final int DEFAULT_CAPACITY
- See Also:
- Constant Field Values
-
entries
protected BoundedHashMap<java.lang.Object,java.lang.Object> entries
-
-
Constructor Detail
-
BasicSessionCache
public BasicSessionCache(int capacity)
Creates a BasicSessionCache with a specified maximum number of entries.- Parameters:
capacity
- maximum number of entries in the cache.- Throws:
java.lang.IllegalArgumentException
- if capacity is negative.
-
BasicSessionCache
public BasicSessionCache()
Creates a BasicSessionCache with the default capacity.
-
-
Method Detail
-
getCapacity
public int getCapacity()
Returns the maximum number of entries in the cache.- Specified by:
getCapacity
in interfaceSessionCache
- Returns:
- maximum number of entries in the cache.
-
setCapacity
public void setCapacity(int capacity)
Sets the maximum number of entries in the cache. If the new capacity is less than the number of cache entries, this evicts the eldest entry until the cache size is equal to its capacity.- Specified by:
setCapacity
in interfaceSessionCache
- Parameters:
capacity
- maximum number of entries in the cache.- Throws:
java.lang.IllegalArgumentException
- if capacity is negative.
-
getEntryCount
public int getEntryCount()
Returns the number of entries currently in the cache.- Specified by:
getEntryCount
in interfaceSessionCache
- Returns:
- number of cached entries.
-
getKeySet
public java.util.Set<java.lang.Object> getKeySet()
Returns aSet
view of the keys contained in the cache. The returned set is immutable: changes to the set are not reflected in the session cache.- Specified by:
getKeySet
in interfaceSessionCache
- Returns:
- a
Set
view of the keys contained in the cache.
-
contains
public boolean contains(java.lang.Object key)
Returns true if the cache contains a specified key, and false if it does not.- Specified by:
contains
in interfaceSessionCache
- Parameters:
key
- the entry key in question. A null value is not permitted.- Returns:
- true if the cache contains the key; false otherwise.
- Throws:
java.lang.IllegalArgumentException
- if the key is null.
-
get
public java.lang.Object get(java.lang.Object key)
Returns a reference to an entry's value in the cache corresponding to a specified key, or null if no entry with that key exists.- Specified by:
get
in interfaceSessionCache
- Parameters:
key
- the entry key to look for.- Returns:
- a reference to the found entry's value.
- Throws:
java.lang.IllegalArgumentException
- if the key is null.
-
put
public void put(java.lang.Object key, java.lang.Object value)
Adds an entry in the cache with a specified key and value. If the cache size after adding the new entry is greater than its capacity, this evicts the eldest entry in the cache.- Specified by:
put
in interfaceSessionCache
- Parameters:
key
- the entry's key. A null value is not permitted.value
- the entry's value. A null value is permitted.- Throws:
java.lang.IllegalArgumentException
- if the key is null.
-
remove
public java.lang.Object remove(java.lang.Object key)
Removes the entry with the specified key from the cache, and returns that entry's value. If no entry exists for the specified key, this does nothing and returns null.- Specified by:
remove
in interfaceSessionCache
- Parameters:
key
- the entry key to look for.- Returns:
- a reference to the removed entry's value, or null of no entry matches the specified key.
-
clear
public void clear()
Removes all entries from the cache.- Specified by:
clear
in interfaceSessionCache
-
-