Project JXTA

net.jxta.impl.util
Class Cache

java.lang.Object
  extended bynet.jxta.impl.util.Cache

public class Cache
extends Object

A pre jdk1.4 replacement for LinkedHashMap which is basically a cache.


Nested Class Summary
(package private)  class Cache.CacheEntryImpl
           
 
Constructor Summary
Cache(long maxSize, CacheEntryListener listener)
          Creates a cache whih will keep at most maxSize purgeable entries.
 
Method Summary
 void clear()
          Empties the cache completely.
 Object get(Object key)
          Return the value, if any associated with the given key.
 CacheEntry getCacheEntry(Object key)
          Return the cache entry, if any, associated with the given key.
 void purge(int fraction)
          Purges some of the cache.
 Object put(Object key, Object value)
          Create a cache entry to hold the given value, and insert it.
protected  CacheEntry putCacheEntry(Object key, CacheEntry value)
          Inserts the given cache entry directly.
 Object remove(Object key)
          Remove the value, if any, and cacheEntry associated with the given key.
protected  CacheEntry removeCacheEntry(Object key)
          Remove the value, if any, and cacheEntry associated with the given key.
 void sticky(Object key, boolean sticky)
          Force the value associated with the given key to be purgeable or non-purgeable from the cache (non-sticky vs. sticky).
 void stickyCacheEntry(CacheEntry ce, boolean sticky)
          Change the purgeability of the given cacheEntry.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Cache

public Cache(long maxSize,
             CacheEntryListener listener)
Creates a cache whih will keep at most maxSize purgeable entries. Every new entry is purgeable by default. Entries that are not purgeable are not counted and are never removed unless clear() or remove() is called. Purgeable entries are removed silently as needed to make room for new entries so that the number of purgeable entries remains < maxSize. Entries prugeability is controlled by invoking the sticky() method or the stickyCacheEntry() method. For now, purged entries are abandonned to the GC which is probably not so bad. To permit acceleration of the collection of resources, a purge listener will be added soon.

Method Detail

clear

public void clear()
Empties the cache completely. The entries are abandonned to the GC.


purge

public void purge(int fraction)
Purges some of the cache. The entries are cleaned-up properly.


putCacheEntry

protected CacheEntry putCacheEntry(Object key,
                                   CacheEntry value)
Inserts the given cache entry directly. Returns the previous cache entry associated with the given key, if any. Not exposed yet. Should not be a problem to expose it, but it is not needed yet.


put

public Object put(Object key,
                  Object value)
Create a cache entry to hold the given value, and insert it. Returns the previous value associated with the given key, if any.


removeCacheEntry

protected CacheEntry removeCacheEntry(Object key)
Remove the value, if any, and cacheEntry associated with the given key. return the cacheEntry that has been removed. Not exposed yet. Should not be a problem to expose it, but it is not needed yet.


remove

public Object remove(Object key)
Remove the value, if any, and cacheEntry associated with the given key. returns the value that has been removed.


getCacheEntry

public CacheEntry getCacheEntry(Object key)
Return the cache entry, if any, associated with the given key. This is public; it improves performance by letting the application do a single lookup instead of two when it needs to find an object in the cache and then change its purgeability.


get

public Object get(Object key)
Return the value, if any associated with the given key.


stickyCacheEntry

public void stickyCacheEntry(CacheEntry ce,
                             boolean sticky)
Change the purgeability of the given cacheEntry. If sticky is true, the entry cannot be purged. Note: if the CacheEntry is known, it is more efficient to use this method than sticky(), since sticky will preform a hashmap lookup to locate the cache entry.


sticky

public void sticky(Object key,
                   boolean sticky)
Force the value associated with the given key to be purgeable or non-purgeable from the cache (non-sticky vs. sticky). Note: Most often, a call to the get() method will be performed before it can be decided to invoke sticky(). Whenever this is the case it is better to invoke getCacheEntry() + getValue() and then stickyCacheEntry() since that eliminates one hashmap lookup.


JXTA J2SE