org.jopendocument.util.cache
Class ICache<K,V,D>

java.lang.Object
  extended by org.jopendocument.util.cache.ICache<K,V,D>
Type Parameters:
K - key type, eg String.
V - value type, eg List of SQLRow.
D - source data type, eg SQLTable.

public class ICache<K,V,D>
extends Object

To keep results computed from some data. The results will be automatically invalidated after some period of time or when the data is modified.

Author:
Sylvain CUAZ

Constructor Summary
ICache()
           
ICache(int delay)
           
ICache(int delay, int size)
           
ICache(int delay, int size, String name)
          Creates a cache with the given parameters.
 
Method Summary
 void addRunning(K key)
          Tell this cache that we're in process of getting the value for key, so if someone else ask have them wait.
 CacheResult<V> check(K key)
          Check if key is in cache, in that case returns the value otherwise adds key to running and returns NOT_IN_CACHE.
 void clear()
           
 void clear(K select)
           
 CacheResult<V> get(K sel)
          If sel is in cache returns its value, else if key is running block until the key is put (or the current thread is interrupted).
 ICache<K,V,D> getParent()
           
 boolean isRunning(K sel)
           
 void put(K sel, V res)
          Put a result which doesn't depend on variable data in this cache.
 Set<? extends CacheWatcher<K,D>> put(K sel, V res, Set<? extends D> data)
          Put a result in this cache.
 void removeRunning(K key)
           
 void setParent(ICache<K,V,D> parent)
          Allow to continue the search for a key in another instance.
 void setWatcherFactory(CacheWatcherFactory<K,D> f)
           
 int size()
           
 String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ICache

public ICache()

ICache

public ICache(int delay)

ICache

public ICache(int delay,
              int size)

ICache

public ICache(int delay,
              int size,
              String name)
Creates a cache with the given parameters.

Parameters:
delay - the delay in seconds before a key is cleared.
size - the maximum size of the cache, negative means no limit.
name - name of this cache and associated thread.
Throws:
IllegalArgumentException - if size is 0.
Method Detail

setWatcherFactory

public final void setWatcherFactory(CacheWatcherFactory<K,D> f)

setParent

public final void setParent(ICache<K,V,D> parent)
Allow to continue the search for a key in another instance.

Parameters:
parent - the cache to search when a key isn't found in this.

getParent

public final ICache<K,V,D> getParent()

get

public final CacheResult<V> get(K sel)
If sel is in cache returns its value, else if key is running block until the key is put (or the current thread is interrupted). Then if a parent has been set, use it. Otherwise the key is not in cache so return a CacheResult of state CacheResult.State.NOT_IN_CACHE.

Parameters:
sel - the key we're getting the value for.
Returns:
a CacheResult with the appropriate state.

addRunning

public final void addRunning(K key)
Tell this cache that we're in process of getting the value for key, so if someone else ask have them wait. ATTN after calling this method you MUST call put(), otherwise get() will always block for key.

Parameters:
key - the key we're getting the value for.
See Also:
put(Object, Object, Set)

removeRunning

public final void removeRunning(K key)

isRunning

public final boolean isRunning(K sel)

check

public final CacheResult<V> check(K key)
Check if key is in cache, in that case returns the value otherwise adds key to running and returns NOT_IN_CACHE.

Parameters:
key - the key to be checked.
Returns:
the associated value, or null.
See Also:
addRunning(Object)

put

public final void put(K sel,
                      V res)
Put a result which doesn't depend on variable data in this cache.

Parameters:
sel - the key.
res - the result associated with sel.

put

public final Set<? extends CacheWatcher<K,D>> put(K sel,
                                                  V res,
                                                  Set<? extends D> data)
Put a result in this cache.

Parameters:
sel - the key.
res - the result associated with sel.
data - the data from which res is computed.
Returns:
the watchers monitoring the passed key.

clear

public final void clear(K select)

clear

public final void clear()

size

public final int size()

toString

public final String toString()
Overrides:
toString in class Object


Copyright © 2010 jOpenDocument All Rights Reserved.