org.sandev.basics.util
Class IDCacheBase

java.lang.Object
  extended byorg.sandev.basics.util.IDCacheBase
All Implemented Interfaces:
IDCache
Direct Known Subclasses:
NamedSingletonIDCache, NodeLocalIDCache, SingletonIDCache

public class IDCacheBase
extends java.lang.Object
implements IDCache

Provides common methods for all IDCache implementations. This class provides utilities and a framework for cache implementation using a Map, override the init method to change what kind of map is used.


Field Summary
protected  java.util.Map map
          The caching data structure.
protected  boolean revisionCheck
          If true, then only newer revisions of a message will be accepted by put.
 
Constructor Summary
IDCacheBase()
          Default ctor calls init
 
Method Summary
 boolean checkRevisionNumber()
          accessor for revisionCheck
 void clear()
          Remove all entries from this cache.
 SandPersistMessage getInstance(long id)
          Given a persistent ID, return the associated message instance.
 java.util.Map getMap()
          Return the underlying map used by the cache.
static java.lang.Object idToKey(long id)
          We need an object to use as a key.
 void init()
          Initialize the Map to a synchronized HashMap.
 boolean putInstance(SandPersistMessage msg)
          Given a persistent messsage, store it in the cache.
 SandPersistMessage removeInstance(long id)
          Given a persistent ID, remove it from the cache.
 void setRevisionCheck(boolean val)
          mutator for revisionCheck
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

map

protected java.util.Map map
The caching data structure. See the init method.


revisionCheck

protected boolean revisionCheck
If true, then only newer revisions of a message will be accepted by put. Older or equivalent revisions will be ignored and put will return false. By default revisionCheck is true.

Constructor Detail

IDCacheBase

public IDCacheBase()
Default ctor calls init

Method Detail

checkRevisionNumber

public boolean checkRevisionNumber()
accessor for revisionCheck


setRevisionCheck

public void setRevisionCheck(boolean val)
mutator for revisionCheck


putInstance

public boolean putInstance(SandPersistMessage msg)
Description copied from interface: IDCache
Given a persistent messsage, store it in the cache. Returns true if the cached instance was replaced or added. Returns false if the instance already exists but was not replaced.

Specified by:
putInstance in interface IDCache

getInstance

public SandPersistMessage getInstance(long id)
Description copied from interface: IDCache
Given a persistent ID, return the associated message instance. Return null if the instance cannot be retrieved.

Specified by:
getInstance in interface IDCache

removeInstance

public SandPersistMessage removeInstance(long id)
Description copied from interface: IDCache
Given a persistent ID, remove it from the cache. Return null if the instance cannot be retrieved.

Specified by:
removeInstance in interface IDCache

clear

public void clear()
Description copied from interface: IDCache
Remove all entries from this cache.

Specified by:
clear in interface IDCache

getMap

public java.util.Map getMap()
Description copied from interface: IDCache
Return the underlying map used by the cache. Since this returns a live reference to the map, manipulating the map directly can invalidate or corrupt the contents of the cache. Generally the IDCache.putInstance(org.sandev.basics.structs.SandPersistMessage) and IDCache.removeInstance(long) methods should be used for cache manipulation, and this method should only be used for read-only access like iteration over the map's contents.

If the IDCache is not backed by a map, returns null.

Specified by:
getMap in interface IDCache

idToKey

public static java.lang.Object idToKey(long id)
We need an object to use as a key. From what I can tell, two separate Long objects with the same value aren't necessarily the same hash, so they don't refer to the same key. I know this works if you intern the String value, so that's what we do.


init

public void init()
Initialize the Map to a synchronized HashMap. Override to use something else.