com.livis.livingtalks.ejb
Class WriteThroughCache

java.lang.Object
  |
  +--com.livis.livingtalks.ejb.WriteThroughCache

public class WriteThroughCache
extends java.lang.Object
implements DataHandler

A thread-safe miss-free write-through cache. This particular implementation will never forget! Maybe, one should call it a mirror instead.

See Also:
DataHandler

Constructor Summary
WriteThroughCache(DataHandler handler)
          Create the cache.
 
Method Summary
 java.util.Collection getAllData()
           
 java.util.Collection getAllKeys()
           
 void invalidate()
           
 boolean isFilled()
           
 java.lang.Object read(java.lang.String key)
          Read data stored at key.
 void setFilled(boolean filled)
          Set whether the cache has read all data already.
 java.lang.String writeNew(java.lang.String key, java.lang.Object data)
          Write new data.
 void writeOld(java.lang.String key, java.lang.Object data)
          Updates old data.
 
Methods inherited from class java.lang.Object
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

WriteThroughCache

public WriteThroughCache(DataHandler handler)
Create the cache.
Parameters:
handler - The cached DataHandler who must do the underlying writes and reads.
Method Detail

isFilled

public boolean isFilled()
Returns:
whether the cache caches all data (do not confuse with exceeded capacity).

invalidate

public void invalidate()
Returns:
whether the cache caches all data (do not confuse with exceeded capacity).

setFilled

public void setFilled(boolean filled)
Set whether the cache has read all data already. Initially, this is false.
Parameters:
whether - the cache is completely filled.

writeNew

public java.lang.String writeNew(java.lang.String key,
                                 java.lang.Object data)
                          throws javax.ejb.CreateException
Write new data.
Specified by:
writeNew in interface DataHandler
Parameters:
data - The data to be stored. data must not be null.
key - The key under which the data should be stored. key may be null.
Returns:
the key under which the data finally got stored.
Throws:
CreateException - if key does already exist and no other key was used.

writeOld

public void writeOld(java.lang.String key,
                     java.lang.Object data)
              throws javax.ejb.CreateException
Updates old data.
Specified by:
writeOld in interface DataHandler
Parameters:
data - the data to be stored. A value of null will delete the data.
key - The key under which to store the data. key must not be null.
Throws:
CreateException - if key does not exist

read

public java.lang.Object read(java.lang.String key)
Read data stored at key.
Specified by:
read in interface DataHandler
Parameters:
key - The key to retrieve the data for. A value of null will return null.
Returns:
The object previously stored, or null if no such data exists.
Throws:
CreateException - if key does not exist

getAllKeys

public java.util.Collection getAllKeys()
Returns:
all keys sorted into a Collection if cache is filled, null otherwise.

getAllData

public java.util.Collection getAllData()
Returns:
all data as a collection (no entry is null) if cache is filled, null otherwise.