org.apache.jcs.utils.struct
Class LRUMap

java.lang.Object
  extended by org.apache.jcs.utils.struct.LRUMap
All Implemented Interfaces:
java.util.Map
Direct Known Subclasses:
LRUMapJCS

public class LRUMap
extends java.lang.Object
implements java.util.Map

This is a simple LRUMap. It implements most of the map methods. It is not recommended that you use any but put, get, remove, and clear.

Children can implement the processRemovedLRU method if they want to handle the removal of the lest recently used item.

This class was abstracted out of the LRU Memory cache. Put, remove, and get should be thread safe. It uses a hashtable and our own double linked list.

Locking is done on the instance.

Author:
aaron smuts

Nested Class Summary
 
Nested classes/interfaces inherited from interface java.util.Map
java.util.Map.Entry<K,V>
 
Field Summary
protected  java.util.Map map
          Map where items are stored by key.
 
Constructor Summary
LRUMap()
          This creates an unbounded version.
LRUMap(int maxObjects)
          This sets the size limit.
 
Method Summary
 void clear()
          This removes all the items.
 boolean containsKey(java.lang.Object key)
          Returns true if the map contains an element for the supplied key.
 boolean containsValue(java.lang.Object value)
          This is an expensive operation that determines if the object supplied is mapped to any key.
 void dumpCacheEntries()
          Dump the cache entries from first to list for debugging.
 void dumpMap()
          Dump the cache map for debugging.
 java.util.Set entrySet()
          This returns a set of entries.
 java.lang.Object get(java.lang.Object key)
           
 int getChunkSize()
           
 java.lang.Object getQuiet(java.lang.Object key)
          This gets an element out of the map without adjusting it's posisiton in the LRU.
 IStats getStatistics()
           
 boolean isEmpty()
          Returns true if the map is empty.
 java.util.Set keySet()
           
protected  void processRemovedLRU(java.lang.Object key, java.lang.Object value)
          This is called when an item is removed from the LRU.
 java.lang.Object put(java.lang.Object key, java.lang.Object value)
           
 void putAll(java.util.Map source)
           
 java.lang.Object remove(java.lang.Object key)
           
 void setChunkSize(int chunkSize)
          The chunk size is the number of items to remove when the max is reached.
 int size()
          This simply returned the number of elements in the map.
 java.util.Collection values()
           
protected  void verifyCache()
          Checks to see if all the items that should be in the cache are.
protected  void verifyCache(java.lang.Object key)
          Logs an error is an element that should be in the cache is not.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface java.util.Map
equals, hashCode
 

Field Detail

map

protected java.util.Map map
Map where items are stored by key.

Constructor Detail

LRUMap

public LRUMap()
This creates an unbounded version. Setting the max objects will result in spooling on subsequent puts.


LRUMap

public LRUMap(int maxObjects)
This sets the size limit.

Parameters:
maxObjects -
Method Detail

size

public int size()
This simply returned the number of elements in the map.

Specified by:
size in interface java.util.Map
See Also:
Map.size()

clear

public void clear()
This removes all the items. It clears the map and the double linked list.

Specified by:
clear in interface java.util.Map
See Also:
Map.clear()

isEmpty

public boolean isEmpty()
Returns true if the map is empty.

Specified by:
isEmpty in interface java.util.Map
See Also:
Map.isEmpty()

containsKey

public boolean containsKey(java.lang.Object key)
Returns true if the map contains an element for the supplied key.

Specified by:
containsKey in interface java.util.Map
See Also:
Map.containsKey(java.lang.Object)

containsValue

public boolean containsValue(java.lang.Object value)
This is an expensive operation that determines if the object supplied is mapped to any key.

Specified by:
containsValue in interface java.util.Map
See Also:
Map.containsValue(java.lang.Object)

values

public java.util.Collection values()
Specified by:
values in interface java.util.Map
Returns:
map.values();

putAll

public void putAll(java.util.Map source)
Specified by:
putAll in interface java.util.Map
Parameters:
source -

get

public java.lang.Object get(java.lang.Object key)
Specified by:
get in interface java.util.Map
Parameters:
key -
Returns:
Object

getQuiet

public java.lang.Object getQuiet(java.lang.Object key)
This gets an element out of the map without adjusting it's posisiton in the LRU. In other words, this does not count as being used. If the element is the last item in the list, it will still be the last itme in the list.

Parameters:
key -
Returns:
Object

remove

public java.lang.Object remove(java.lang.Object key)
Specified by:
remove in interface java.util.Map
Parameters:
key -
Returns:
Object removed

put

public java.lang.Object put(java.lang.Object key,
                            java.lang.Object value)
Specified by:
put in interface java.util.Map
Parameters:
key -
value -
Returns:
Object

dumpCacheEntries

public void dumpCacheEntries()
Dump the cache entries from first to list for debugging.


dumpMap

public void dumpMap()
Dump the cache map for debugging.


verifyCache

protected void verifyCache()
Checks to see if all the items that should be in the cache are. Checks consistency between List and map.


verifyCache

protected void verifyCache(java.lang.Object key)
Logs an error is an element that should be in the cache is not.

Parameters:
key -

processRemovedLRU

protected void processRemovedLRU(java.lang.Object key,
                                 java.lang.Object value)
This is called when an item is removed from the LRU. We just log some information.

Children can implement this method for special behavior.

Parameters:
key -
value -

setChunkSize

public void setChunkSize(int chunkSize)
The chunk size is the number of items to remove when the max is reached. By default it is 1.

Parameters:
chunkSize - The chunkSize to set.

getChunkSize

public int getChunkSize()
Returns:
Returns the chunkSize.

getStatistics

public IStats getStatistics()
Returns:
IStats

entrySet

public java.util.Set entrySet()
This returns a set of entries. Our LRUMapEntry is used since the value stored in the underlying map is a node in the double linked list. We wouldn't want to return this to the client, so we construct a new entry with the payload of the node.

TODO we should return out own set wrapper, so we can avoid the extra object creation if it isn't necessary.

Specified by:
entrySet in interface java.util.Map
See Also:
Map.entrySet()

keySet

public java.util.Set keySet()
Specified by:
keySet in interface java.util.Map
Returns:
map.keySet();


Copyright © 2002-2010 Apache Software Foundation. All Rights Reserved.