org.tentackle.appworx
Class AppDbObjectCacheIndex<T extends AppDbObject,C extends Comparable<? super C>>

java.lang.Object
  extended by org.tentackle.appworx.AppDbObjectCacheIndex<T,C>
Type Parameters:
T - the AppDbObject class
C - the Comparable class

public abstract class AppDbObjectCacheIndex<T extends AppDbObject,C extends Comparable<? super C>>
extends Object

Cache index.
Holds the mapping of keys to objects.

Author:
harald

Constructor Summary
AppDbObjectCacheIndex(String name)
          Create a new index.
 
Method Summary
protected  boolean add(T object)
          add an object to the index.
protected  void addUnique(T object)
          add an unique object to the index.
protected  void clear()
          clear contents of the cache index.
protected  void clearCacheStats()
          Clears the cache statistics.
abstract  C extract(T object)
          Extract the Comparable that uniquely identifies the object from that object.
protected  T get(ContextDb db, C key)
          get object from cache by key.
protected  List<T> getObjects()
          get all objects from cache index
protected  List<T> getObjects(ContextDb db, C fromKey, C toKey)
          get a subset of objects
protected  String printCacheStats()
          Gets the cache index statistics as a printable string.
protected  boolean remove(T object)
          remove object from cache.
protected  void removeExisting(T object)
          remove object from cache.
abstract  T select(ContextDb db, C key)
          Select an AppDbObject by key from db.
protected  int size()
          Gets the number of objects in this index.
 String toString()
          Returns a string representation of the object.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

AppDbObjectCacheIndex

public AppDbObjectCacheIndex(String name)
Create a new index. The constructor takes an argument to enforce invocation of super() in order to create the cacheMap.

Parameters:
name - is the symbolic name of the index (for diagnostics only).
Method Detail

select

public abstract T select(ContextDb db,
                         C key)
Select an AppDbObject by key from db. This method needs to be implemented for each index.

Parameters:
db - is the contextDb to select the object from
key - is the Comparable used to uniquely identify the object (can be null)
Returns:
the selected object or null if it does not exist.

extract

public abstract C extract(T object)
Extract the Comparable that uniquely identifies the object from that object. This method needs to be implemented for each index.

Parameters:
object - is the object to extract the key from (never null)
Returns:
the Comparable (can be null)

toString

public String toString()
Description copied from class: java.lang.Object
Returns a string representation of the object. In general, the toString method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this method.

The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of:

 getClass().getName() + '@' + Integer.toHexString(hashCode())
 

Overrides:
toString in class Object
Returns:
a string representation of the object.

clear

protected void clear()
clear contents of the cache index.


size

protected int size()
Gets the number of objects in this index.

Returns:
the number of objects

printCacheStats

protected String printCacheStats()
Gets the cache index statistics as a printable string.

Returns:
the string for cache stats

clearCacheStats

protected void clearCacheStats()
Clears the cache statistics.


get

protected T get(ContextDb db,
                C key)
                             throws ApplicationException
get object from cache by key.

Parameters:
db - is the contextdb
key - is the Comparable that uniquely identifies the object
Returns:
the object or null if not in cache
Throws:
ApplicationException - if the objects key has been changed by application

getObjects

protected List<T> getObjects()
                                          throws ApplicationException
get all objects from cache index

Returns:
the objects in a collection
Throws:
ApplicationException - if a key of one of the objects has been changed by application

getObjects

protected List<T> getObjects(ContextDb db,
                             C fromKey,
                             C toKey)
                                          throws ApplicationException
get a subset of objects

Parameters:
db - is the contextDb
fromKey - is the start of range (including)
toKey - is the end of range (exluding)
Returns:
the objects with fromKey <= object < toKey.
Throws:
ApplicationException - if a key of one of the objects has been changed by application

add

protected boolean add(T object)
add an object to the index. Does not throw an exception if it is already in cache.

Parameters:
object - is the object to append (never null)
Returns:
true if added, false if object already in index or key in object evaluated to null

addUnique

protected void addUnique(T object)
                  throws ApplicationException
add an unique object to the index. Unique violations usually indicate an error in comparing contextDb's or that the context of a cached object was changed accidently.

Parameters:
object - is the object to append (never null)
Throws:
ApplicationException - if unique violation

remove

protected boolean remove(T object)
remove object from cache. Does not throw an exception if it not in cache. For use by AppDbObjectCache.remove() only.

Parameters:
object - the object to remove (never null)
Returns:
true if object removed

removeExisting

protected void removeExisting(T object)
                       throws ApplicationException
remove object from cache. Assumes that the object really is in cache.

Parameters:
object - the object to remove (never null)
Throws:
ApplicationException - if not in cache


Copyright © 2001-2008 Harald Krake, Bergstr. 48, 78098 Triberg, Germany, harald@krake.de