wt.util
Class Cache

java.lang.Object
  extended byjava.util.AbstractMap
      extended bywt.util.Cache
All Implemented Interfaces:
Map
Direct Known Subclasses:
DirtyCache, ManagedCache, ProjectModelCache, ReferenceCache.ObjectReferenceCacheTable.ObjectReferenceCache, ServerAffinityCache, ServerAffinityCache, StandardNmProjMgmtService.PlanModelCache, StandardNmTopicService.ForumModelCache, StatementCache, TeamTemplateCache.CachedTeamTemplates, WrappedPostlessSocket.SocketCache, WrappedSocket.SocketCache

public class Cache
extends AbstractMap

A fixed size, most recently used object cache. The caches size is set at construction, and only the most recently used entries are maintained. Each get or put operation makes that entry the most recently used. When putting new entries, if the cache is full, the least recently used entry is aged out of the cache.

Supported API: true
Extendable: false


Nested Class Summary
protected  class Cache.CacheEntry
          Default implementation of Map.Entry
(package private)  class Cache.EntrySet
          Implements a modifiable set based on the entries in this cache
(package private)  class Cache.KeySet
           
 
Nested classes inherited from class java.util.AbstractMap
 
Nested classes inherited from class java.util.Map
Map.Entry
 
Field Summary
protected  int agedOut
           
protected  int cacheSize
           
protected  int count
           
(package private)  Set entrySet
           
protected  int[] hashCodes
           
protected  int hits
           
protected  Object[] keys
           
(package private)  Set keySet
           
private  int leastRecentlyUsed
           
protected  int misses
           
protected  int modCount
           
private  int mostRecentlyUsed
           
private  int[] newer
           
private  int[] next
           
private  int[] older
           
private  int[] prev
           
private static String RESOURCE
           
protected  int tableSize
           
protected  Object[] values
           
 
Constructor Summary
Cache(int size)
          Construct a new fixed-size, most-recently-used cache.
Cache(Map m)
           
 
Method Summary
 void clear()
          Clears the cache.
 boolean containsKey(Object key)
          Determines if this cache contains the given key
 boolean containsValue(Object value)
          Determines if this cache contains the given value
protected  Map.Entry createEntry(int index)
          Used to create the Map.Entry instances stored by this cache
 Enumeration entries()
          Deprecated. Replaced by values().iterator()

Supported API: true
 Set entrySet()
           
protected  int find(Object key)
          Protected method that finds existing entry.
 Object get(Object key)
          Get an entry from the cache.
 Object getAndRemove(Object key)
          Deprecated. Replaced by remove(Object)

Supported API: true
 Object getAndReplace(Object key, Object value)
          Deprecated. Replaced by put(Object,Object)

Supported API: true
 Object[] getKeys()
          Get an array of the keys in this Cache.
 Set keySet()
           
protected  int link(Object key)
          Protected method that finds slot for an existing or new entry.
protected  void overflow(Object key, Object value)
          Called when an entry is being aged out of the cache.
protected  void print()
           
 Object put(Object key, Object value)
          Put an entry in the cache.
 Object remove(Object key)
          Remove an entry from the cache.
 int size()
          Get the number of mappings in this cache
 String toString()
          Returns string representation of the cache.
protected  void touch(int index)
          Protected method to update age links to make the given entry be the most recently used.
private  void unlink(int index)
          Private method to unlink an entry from the cache.
 
Methods inherited from class java.util.AbstractMap
clone, equals, hashCode, isEmpty, putAll, values
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

RESOURCE

private static final String RESOURCE
See Also:
Constant Field Values

cacheSize

protected int cacheSize

tableSize

protected int tableSize

count

protected int count

keys

protected Object[] keys

values

protected Object[] values

hashCodes

protected int[] hashCodes

hits

protected int hits

misses

protected int misses

agedOut

protected int agedOut

modCount

protected int modCount

mostRecentlyUsed

private int mostRecentlyUsed

leastRecentlyUsed

private int leastRecentlyUsed

newer

private int[] newer

older

private int[] older

next

private int[] next

prev

private int[] prev

entrySet

transient Set entrySet

keySet

transient Set keySet
Constructor Detail

Cache

public Cache(int size)
Construct a new fixed-size, most-recently-used cache. Sparse arrays of ints are used to maintain linked lists in a memory efficient manner - the maximum size supported by this implementation is 715827882.

Supported API: true

Parameters:
size - the maximum number of entries stored in the cache.

Cache

public Cache(Map m)
Method Detail

containsKey

public boolean containsKey(Object key)
Determines if this cache contains the given key

Parameters:
key -
Returns:
boolean

containsValue

public boolean containsValue(Object value)
Determines if this cache contains the given value

Parameters:
value -
Returns:
boolean

size

public int size()
Get the number of mappings in this cache

Returns:
int

get

public Object get(Object key)
Get an entry from the cache. Hit and miss statistics are updated.

Supported API: true

Parameters:
key - the entry key
Returns:
the cached object or null if not found

put

public Object put(Object key,
                  Object value)
Put an entry in the cache. If an entry for the given key already exists, the previous value is discarded. This entry is considered the most recently used.

Supported API: true

Parameters:
key - the entry key
value - the value to associate with the given key
Returns:
The previous value that mapped to the key, or null if there was no previous value

getAndReplace

public Object getAndReplace(Object key,
                            Object value)
Deprecated. Replaced by put(Object,Object)

Supported API: true

Get an entry from the cache and replace it with a new entry. The new entry is considered the most recently used. Hit and miss statistics are updated.

Parameters:
key - the entry key
value - the value to associate with the given key
Returns:
previous value

remove

public Object remove(Object key)
Remove an entry from the cache. Does nothing if the entry is not currently in the cache.

Supported API: true

Parameters:
key - the entry key

getAndRemove

public Object getAndRemove(Object key)
Deprecated. Replaced by remove(Object)

Supported API: true

Get an entry from the cache and remove it at the same time. Does nothing if the entry is not currently in the cache. Hit and miss statistics are updated.

Parameters:
key - the entry key
Returns:
previous value

clear

public void clear()
Clears the cache. All entries are removed.

Supported API: true


find

protected int find(Object key)
Protected method that finds existing entry. Hit and miss statistics are not updated. Most recently used list is not updated.

Supported API: false

Parameters:
key - the entry key
Returns:
index of the entry or 0 if not found

link

protected int link(Object key)
Protected method that finds slot for an existing or new entry. Hit and miss statistics are not updated. Most recently used list is not updated.

Supported API: false

Parameters:
key - the entry key
Returns:
index of the entry

touch

protected void touch(int index)
Protected method to update age links to make the given entry be the most recently used.

Supported API: false

Parameters:
index - the index of the entry

unlink

private void unlink(int index)
Private method to unlink an entry from the cache.

Supported API: false

Parameters:
index - the index of the entry

entries

public Enumeration entries()
Deprecated. Replaced by values().iterator()

Supported API: true

Returns an Enumeration of the values in the cache.


toString

public String toString()
Returns string representation of the cache. The string contains the default string representation of the object plus a summary of the current cache size, hits and misses counts, and aged out entry count.

Supported API: true


overflow

protected void overflow(Object key,
                        Object value)
Called when an entry is being aged out of the cache. Can be overriden by subclasses that want to take some action when an entry is being aged out of the cache due to overflow. It is only called when an entry is aged out due to overflow, not when entries are explicitly removed or the cache is cleared. It is called before the entry is removed from the cache.

Parameters:
key - the entry key
value - the entry value

print

protected void print()

getKeys

public Object[] getKeys()
Get an array of the keys in this Cache. Care should be taken when modifying this array, as it is used by the cache internally.

Returns:
An Object[] of keys. There will likely be null values in the array
See Also:
For a Collection-based view of the keys in this cache

entrySet

public Set entrySet()
Returns:
A set of Map.Entry objects for the mappings in this cache. The Entry objects are reused by the cache, so there is no internal guarantee that the key in a given entry will stay the same over time.

keySet

public Set keySet()

createEntry

protected Map.Entry createEntry(int index)
Used to create the Map.Entry instances stored by this cache

Returns:
Entry