org.norther.tammi.acorn.cache
Class CachedMapAdapter

java.lang.Object
  extended by org.norther.tammi.acorn.cache.CachedMapAdapter
All Implemented Interfaces:
Serializable, ConcurrentMap, Map, CachedMap
Direct Known Subclasses:
DefaultCache

public class CachedMapAdapter
extends Object
implements CachedMap, Serializable

Extends the given ConcurrentMap implementation to support the CachedMap interface.

Author:
Ilkka Priha
See Also:
Serialized Form

Nested Class Summary
 
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 m)
          Constructs a new map.
CachedMapAdapter(ConcurrentMap m, int max)
          Constructs a new map with a max expiration time.
CachedMapAdapter(ConcurrentMap m, int max, int def)
          Constructs a new map with max and default expiration times.
CachedMapAdapter(ConcurrentMap 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(Object key, Object obj)
          Adds a cachable object into the map but only if the specified key is not already mapped.
 boolean add(Object key, Object 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 entrySet()
           
 boolean equals(Object object)
           
 Object expire(Object key)
          Expires and removes an entry.
 void expireAll()
          Expires all entries.
 void expireFraction(float fraction)
          Expires a fraction of entries.
 Object get(Object key)
           
 Object 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 keySet()
           
 Object put(Object key, Object obj)
           
 Object put(Object key, Object obj, int freq)
          Puts a cachable object into the map with an object specific expiration frequency.
 void putAll(Map map)
           
 Object putIfAbsent(Object key, Object obj)
           
 Object putIfAbsent(Object key, Object 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.
 Object remove(Object key)
           
 boolean remove(Object key, Object obj)
           
 Object replace(Object key, Object obj)
           
 boolean replace(Object key, Object old, Object 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 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 m)
Constructs a new map.

Parameters:
m - the wrapped map.

CachedMapAdapter

public CachedMapAdapter(ConcurrentMap 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 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 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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Parameters:
h - the threshold.

equals

public boolean equals(Object object)
Specified by:
equals in interface Map
Overrides:
equals in class Object

hashCode

public int hashCode()
Specified by:
hashCode in interface Map
Overrides:
hashCode in class Object

toString

public String toString()
Overrides:
toString in class Object

clear

public void clear()
Specified by:
clear in interface Map

containsKey

public boolean containsKey(Object key)
Specified by:
containsKey in interface Map

containsValue

public boolean containsValue(Object obj)
Specified by:
containsValue in interface Map

entrySet

public Set entrySet()
Specified by:
entrySet in interface Map

get

public Object get(Object key)
Specified by:
get in interface Map

keySet

public Set keySet()
Specified by:
keySet in interface Map

put

public Object put(Object key,
                  Object obj)
Specified by:
put in interface Map

putIfAbsent

public Object putIfAbsent(Object key,
                          Object obj)
Specified by:
putIfAbsent in interface ConcurrentMap

add

public boolean add(Object key,
                   Object 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
Parameters:
key - a key.
obj - an object.
Returns:
true if added, false otherwise.

replace

public Object replace(Object key,
                      Object obj)
Specified by:
replace in interface ConcurrentMap

replace

public boolean replace(Object key,
                       Object old,
                       Object obj)
Specified by:
replace in interface ConcurrentMap

putAll

public void putAll(Map map)
Specified by:
putAll in interface Map

remove

public Object remove(Object key)
Specified by:
remove in interface Map

remove

public boolean remove(Object key,
                      Object obj)
Specified by:
remove in interface ConcurrentMap

size

public int size()
Specified by:
size in interface Map

isEmpty

public boolean isEmpty()
Specified by:
isEmpty in interface Map

values

public Collection values()
Specified by:
values in interface Map

get

public Object 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
Parameters:
key - a key.
freq - an expiration frequency.
Returns:
the value of the key.

put

public Object put(Object key,
                  Object 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
Parameters:
key - a key.
obj - an object.
freq - an expiration frequency.
Returns:
the previous value of the key.

putIfAbsent

public Object putIfAbsent(Object key,
                          Object 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
Parameters:
key - a key.
obj - an object.
freq - an expiration frequency.
Returns:
the previous value of the key.

add

public boolean add(Object key,
                   Object 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
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
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
Parameters:
key - a key.
freq - an expiration frequency.
Returns:
true if refreshed, otherwise false.

expire

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

Specified by:
expire in interface CachedMap
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

expireFraction

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

Specified by:
expireFraction in interface CachedMap
Parameters:
fraction - the fraction to expire (0.0..1.0).


Copyright © 2004 The Norther Organization. All rights reserved.