org.norther.tammi.acorn.cache
Class CachedMapAdapter<K,V>

java.lang.Object
  extended by org.norther.tammi.acorn.cache.CachedMapAdapter<K,V>
Type Parameters:
K - the type of keys maintained by this map.
V - the type of mapped values.
All Implemented Interfaces:
Serializable, ConcurrentMap<K,V>, Map<K,V>, CachedMap<K,V>
Direct Known Subclasses:
DefaultCache

public class CachedMapAdapter<K,V>
extends Object
implements CachedMap<K,V>, Serializable

Extends the given ConcurrentMap implementation to support the CachedMap interface.

Version:
$Id: CachedMapAdapter.java,v 1.28 2009-11-27 14:06:07 cvsimp Exp $
Author:
Ilkka Priha
See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from interface org.norther.tammi.acorn.cache.CachedMap
CachedMap.Value<K,V>
 
Nested classes/interfaces inherited from interface java.util.Map
Map.Entry<K,V>
 
Field Summary
static int DEFAULT_EXPIRATION_SLEEP_INTERVAL
          The default expiration sleep interval.
static int DEFAULT_EXPIRATION_TIME
          The default expiration time.
 
Constructor Summary
CachedMapAdapter(ConcurrentMap<K,CachedMap.Value<K,V>> m)
          Constructs a new map.
CachedMapAdapter(ConcurrentMap<K,CachedMap.Value<K,V>> m, int max)
          Constructs a new map with a max expiration time.
CachedMapAdapter(ConcurrentMap<K,CachedMap.Value<K,V>> m, int max, int def)
          Constructs a new map with max and default expiration times.
CachedMapAdapter(ConcurrentMap<K,CachedMap.Value<K,V>> m, int max, int def, int sleep)
          Constructs a new map with max and default expiration times and an expiration sleep interval.
 
Method Summary
 boolean add(K key, V obj)
          Adds a cachable object into the map but only if the specified key is not already mapped.
 boolean add(K key, V obj, int freq)
          Adds a cachable object into the map with an object specific expiration frequency but only if the specified key is not already mapped.
 void clear()
           
 boolean containsKey(Object key)
           
 boolean containsValue(Object obj)
           
 Set<Map.Entry<K,V>> entrySet()
           
 boolean equals(Object object)
           
 V expire(Object key)
          Expires and removes an entry.
 void expireAll()
          Expires all entries.
 void expireFraction(float fraction)
          Expires a fraction of entries.
 V get(Object key)
           
 V get(Object key, int freq)
          Gets an object from the map and refreshes it with a specific expiration frequency.
 int getDefaultExpirationTime()
          Gets the default expiration time in seconds.
 int getExpirationLimit()
          Gets the expiration limit for the number of cached entries.
 int getExpirationSleepInterval()
          Gets the expiration sleep interval in seconds.
 float getExpirationThreshold()
          Gets the threshold relative to the entry limit before expiration of excessive entries starts.
 ScheduledTimer getExpirationTimer()
          Gets the expiration timer.
 int getMaxExpirationTime()
          Gets the maximum expiration time in seconds.
 CacheListener getSwapOutHandler()
          Gets the swap out handler.
 int hashCode()
           
 boolean isAllowExpired()
          Gets the allow expired option.
 boolean isAutoRefreshDeclined()
          Gets the automatic refresh declined option.
 boolean isDiscardOverloaded()
          Gets the discard overloaded option.
 boolean isEmpty()
           
 Set<K> keySet()
           
 V put(K key, V obj)
           
 V put(K key, V obj, int freq)
          Puts a cachable object into the map with an object specific expiration frequency.
 void putAll(Map<? extends K,? extends V> map)
           
 V putIfAbsent(K key, V obj)
           
 V putIfAbsent(K key, V obj, int freq)
          Puts a cachable object into the map with an object specific expiration frequency but only if the specified key is not already mapped.
 boolean refresh(Object key)
          Refreshes an entry.
 boolean refresh(Object key, int freq)
          Refreshes an entry with a new frequency.
 V remove(Object key)
           
 boolean remove(Object key, Object obj)
           
 V replace(K key, V obj)
           
 boolean replace(K key, V old, V obj)
           
 void setAllowExpired(boolean flag)
          Sets the allow expired option allowing gets of entries already expired but not yet removed by the expiration handler.
 void setAutoRefreshDeclined(boolean flag)
          Sets the automatic refresh declined option preventing cached entries being automatically refreshed during plain gets.
 void setDefaultExpirationTime(int t)
          Sets the default expiration time in seconds.
 void setDiscardOverloaded(boolean flag)
          Sets the discard overloaded option.
 void setExpirationLimit(int c)
          Sets the expiration limit for the number of cached entries.
 void setExpirationSleepInterval(int t)
          Sets the expiration sleep interval in seconds.
 void setExpirationThreshold(float h)
          Sets the threshold relative to the entry limit before expiration of excessive entries starts.
 void setExpirationTimer(ScheduledTimer t)
          Sets the expiration timer.
 void setMaxExpirationTime(int t)
          Sets the maximum expiration time in seconds.
 void setSwapOutHandler(CacheListener handler)
          Sets the swap out handler.
 int size()
           
 String toString()
           
 Collection<V> values()
           
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

DEFAULT_EXPIRATION_TIME

public static final int DEFAULT_EXPIRATION_TIME
The default expiration time.

See Also:
Constant Field Values

DEFAULT_EXPIRATION_SLEEP_INTERVAL

public static final int DEFAULT_EXPIRATION_SLEEP_INTERVAL
The default expiration sleep interval.

See Also:
Constant Field Values
Constructor Detail

CachedMapAdapter

public CachedMapAdapter(ConcurrentMap<K,CachedMap.Value<K,V>> m)
Constructs a new map.

Parameters:
m - the wrapped map.

CachedMapAdapter

public CachedMapAdapter(ConcurrentMap<K,CachedMap.Value<K,V>> m,
                        int max)
Constructs a new map with a max expiration time.

Parameters:
m - the wrapped map.
max - a max expiration time (0 = default, -1 = infinite).

CachedMapAdapter

public CachedMapAdapter(ConcurrentMap<K,CachedMap.Value<K,V>> m,
                        int max,
                        int def)
Constructs a new map with max and default expiration times.

Parameters:
m - the wrapped map.
max - a max expiration time (0 = default, -1 = infinite).
def - a default expiration time (0 = default, -1 = infinite).

CachedMapAdapter

public CachedMapAdapter(ConcurrentMap<K,CachedMap.Value<K,V>> m,
                        int max,
                        int def,
                        int sleep)
Constructs a new map with max and default expiration times and an expiration sleep interval.

Parameters:
m - the wrapped map.
max - a max expiration time (0 = default, -1 = infinite).
def - a default expiration time (0 = default, -1 = infinite).
sleep - an expiration sleep interval (0 = default, -1 = infinite).
Method Detail

getExpirationTimer

public ScheduledTimer getExpirationTimer()
Gets the expiration timer.

Returns:
the timer.

setExpirationTimer

public void setExpirationTimer(ScheduledTimer t)
Sets the expiration timer.

Note that the specified timer does not remain over serialization but the default timer is applied if not reset.

Parameters:
t - the timer.

getSwapOutHandler

public CacheListener getSwapOutHandler()
Gets the swap out handler.

Returns:
the swap out handler.

setSwapOutHandler

public void setSwapOutHandler(CacheListener handler)
Sets the swap out handler.

Parameters:
handler - the swap out handler.

isAllowExpired

public boolean isAllowExpired()
Description copied from interface: CachedMap
Gets the allow expired option.

Specified by:
isAllowExpired in interface CachedMap<K,V>
Returns:
the allow expired option.

setAllowExpired

public void setAllowExpired(boolean flag)
Description copied from interface: CachedMap
Sets the allow expired option allowing gets of entries already expired but not yet removed by the expiration handler.

Specified by:
setAllowExpired in interface CachedMap<K,V>
Parameters:
flag - the allow expired option.

isDiscardOverloaded

public boolean isDiscardOverloaded()
Description copied from interface: CachedMap
Gets the discard overloaded option.

Specified by:
isDiscardOverloaded in interface CachedMap<K,V>
Returns:
true if new puts are discarded if overloaded, false otherwise.

setDiscardOverloaded

public void setDiscardOverloaded(boolean flag)
Description copied from interface: CachedMap
Sets the discard overloaded option. If set to true, new entries are discarded and an exception thrown when the capacity of the cache is exceeded. The default policy is to expire old entries until enough capacity is available.

Specified by:
setDiscardOverloaded in interface CachedMap<K,V>
Parameters:
flag - true if new puts are discarded if overloaded, false otherwise.

isAutoRefreshDeclined

public boolean isAutoRefreshDeclined()
Description copied from interface: CachedMap
Gets the automatic refresh declined option.

Specified by:
isAutoRefreshDeclined in interface CachedMap<K,V>
Returns:
the automatic refresh declined option.

setAutoRefreshDeclined

public void setAutoRefreshDeclined(boolean flag)
Description copied from interface: CachedMap
Sets the automatic refresh declined option preventing cached entries being automatically refreshed during plain gets.

Specified by:
setAutoRefreshDeclined in interface CachedMap<K,V>
Parameters:
flag - the automatic refresh declined option.

getMaxExpirationTime

public int getMaxExpirationTime()
Description copied from interface: CachedMap
Gets the maximum expiration time in seconds.

Specified by:
getMaxExpirationTime in interface CachedMap<K,V>
Returns:
the secs.

setMaxExpirationTime

public void setMaxExpirationTime(int t)
Description copied from interface: CachedMap
Sets the maximum expiration time in seconds.

Specified by:
setMaxExpirationTime in interface CachedMap<K,V>
Parameters:
t - the secs.

getDefaultExpirationTime

public int getDefaultExpirationTime()
Description copied from interface: CachedMap
Gets the default expiration time in seconds.

Specified by:
getDefaultExpirationTime in interface CachedMap<K,V>
Returns:
the secs.

setDefaultExpirationTime

public void setDefaultExpirationTime(int t)
Description copied from interface: CachedMap
Sets the default expiration time in seconds.

Specified by:
setDefaultExpirationTime in interface CachedMap<K,V>
Parameters:
t - the secs.

getExpirationSleepInterval

public int getExpirationSleepInterval()
Description copied from interface: CachedMap
Gets the expiration sleep interval in seconds.

Specified by:
getExpirationSleepInterval in interface CachedMap<K,V>
Returns:
the secs.

setExpirationSleepInterval

public void setExpirationSleepInterval(int t)
Description copied from interface: CachedMap
Sets the expiration sleep interval in seconds.

Specified by:
setExpirationSleepInterval in interface CachedMap<K,V>
Parameters:
t - the secs

getExpirationLimit

public int getExpirationLimit()
Description copied from interface: CachedMap
Gets the expiration limit for the number of cached entries.

Specified by:
getExpirationLimit in interface CachedMap<K,V>
Returns:
the expiration limit.

setExpirationLimit

public void setExpirationLimit(int c)
Description copied from interface: CachedMap
Sets the expiration limit for the number of cached entries.

Specified by:
setExpirationLimit in interface CachedMap<K,V>
Parameters:
c - the expiration limit.

getExpirationThreshold

public float getExpirationThreshold()
Description copied from interface: CachedMap
Gets the threshold relative to the entry limit before expiration of excessive entries starts.

Specified by:
getExpirationThreshold in interface CachedMap<K,V>
Returns:
the threshold.

setExpirationThreshold

public void setExpirationThreshold(float h)
Description copied from interface: CachedMap
Sets the threshold relative to the entry limit before expiration of excessive entries starts. Values below 1.0 define the filling rate right after expiration and values above 1.0 define the filling rate just before expiration. The exact value 1.0 is not recommended as it starts expiration after every addition of entries when the filling rate is at the limit.

Specified by:
setExpirationThreshold in interface CachedMap<K,V>
Parameters:
h - the threshold.

equals

public boolean equals(Object object)
Specified by:
equals in interface Map<K,V>
Overrides:
equals in class Object

hashCode

public int hashCode()
Specified by:
hashCode in interface Map<K,V>
Overrides:
hashCode in class Object

toString

public String toString()
Overrides:
toString in class Object

clear

public void clear()
Specified by:
clear in interface Map<K,V>

containsKey

public boolean containsKey(Object key)
Specified by:
containsKey in interface Map<K,V>

containsValue

public boolean containsValue(Object obj)
Specified by:
containsValue in interface Map<K,V>

entrySet

public Set<Map.Entry<K,V>> entrySet()
Specified by:
entrySet in interface Map<K,V>

get

public V get(Object key)
Specified by:
get in interface Map<K,V>

keySet

public Set<K> keySet()
Specified by:
keySet in interface Map<K,V>

put

public V put(K key,
             V obj)
Specified by:
put in interface Map<K,V>

putIfAbsent

public V putIfAbsent(K key,
                     V obj)
Specified by:
putIfAbsent in interface ConcurrentMap<K,V>

add

public boolean add(K key,
                   V obj)
Description copied from interface: CachedMap
Adds a cachable object into the map but only if the specified key is not already mapped.

Specified by:
add in interface CachedMap<K,V>
Parameters:
key - a key.
obj - an object.
Returns:
true if added, false otherwise.

replace

public V replace(K key,
                 V obj)
Specified by:
replace in interface ConcurrentMap<K,V>

replace

public boolean replace(K key,
                       V old,
                       V obj)
Specified by:
replace in interface ConcurrentMap<K,V>

putAll

public void putAll(Map<? extends K,? extends V> map)
Specified by:
putAll in interface Map<K,V>

remove

public V remove(Object key)
Specified by:
remove in interface Map<K,V>

remove

public boolean remove(Object key,
                      Object obj)
Specified by:
remove in interface ConcurrentMap<K,V>

size

public int size()
Specified by:
size in interface Map<K,V>

isEmpty

public boolean isEmpty()
Specified by:
isEmpty in interface Map<K,V>

values

public Collection<V> values()
Specified by:
values in interface Map<K,V>

get

public V get(Object key,
             int freq)
Description copied from interface: CachedMap
Gets an object from the map and refreshes it with a specific expiration frequency.

Specified by:
get in interface CachedMap<K,V>
Parameters:
key - a key.
freq - an expiration frequency.
Returns:
the value of the key.

put

public V put(K key,
             V obj,
             int freq)
Description copied from interface: CachedMap
Puts a cachable object into the map with an object specific expiration frequency.

Specified by:
put in interface CachedMap<K,V>
Parameters:
key - a key.
obj - an object.
freq - an expiration frequency.
Returns:
the previous value of the key.

putIfAbsent

public V putIfAbsent(K key,
                     V obj,
                     int freq)
Description copied from interface: CachedMap
Puts a cachable object into the map with an object specific expiration frequency but only if the specified key is not already mapped.

Specified by:
putIfAbsent in interface CachedMap<K,V>
Parameters:
key - a key.
obj - an object.
freq - an expiration frequency.
Returns:
the previous value of the key.

add

public boolean add(K key,
                   V obj,
                   int freq)
Description copied from interface: CachedMap
Adds a cachable object into the map with an object specific expiration frequency but only if the specified key is not already mapped.

Specified by:
add in interface CachedMap<K,V>
Parameters:
key - a key.
obj - an object.
freq - an expiration frequency.
Returns:
true if added, false otherwise.

refresh

public boolean refresh(Object key)
Description copied from interface: CachedMap
Refreshes an entry.

Specified by:
refresh in interface CachedMap<K,V>
Parameters:
key - a key.
Returns:
true if refreshed, otherwise false.

refresh

public boolean refresh(Object key,
                       int freq)
Description copied from interface: CachedMap
Refreshes an entry with a new frequency.

Specified by:
refresh in interface CachedMap<K,V>
Parameters:
key - a key.
freq - an expiration frequency.
Returns:
true if refreshed, otherwise false.

expire

public V expire(Object key)
Description copied from interface: CachedMap
Expires and removes an entry.

Specified by:
expire in interface CachedMap<K,V>
Parameters:
key - the key.
Returns:
the expired value or null.

expireAll

public void expireAll()
Description copied from interface: CachedMap
Expires all entries.

Specified by:
expireAll in interface CachedMap<K,V>

expireFraction

public void expireFraction(float fraction)
Description copied from interface: CachedMap
Expires a fraction of entries.

Specified by:
expireFraction in interface CachedMap<K,V>
Parameters:
fraction - the fraction to expire (0.0..1.0).


Copyright © 2004 The Norther Organization. All rights reserved.