anthill.util
Class MapList

java.lang.Object
  |
  +--anthill.util.MapList
All Implemented Interfaces:
Clearable, Map, Runnable, Serializable
Direct Known Subclasses:
HashList, TreeList

public class MapList
extends Object
implements Map, Runnable, Clearable, Serializable

This class implements a HashList, which is a combination of a HashMap and a doubly linked list. The linked listed is always in sorted order according to the least recently used (LRU) scheme.

Version:
$Revision: 1.15 $
Author:
Hein Meling
, Alberto Montresor
See Also:
Serialized Form

Nested Class Summary
protected  class MapList.Entry
          The Entry class is an inner class used to link objects stored in the map into a list.
 
Nested classes inherited from class java.util.Map
Map.Entry
 
Field Summary
protected  Map map
          A map mapping a key identifier to a composite Entry object implementing a doubly linked list.
 
Constructor Summary
MapList(int maxsize)
          Instantiate a new MapList object by setting the maximum number of key, value pairs which may be contained in it.
MapList(int maxsize, int maxtime)
          Instantiate a new MapList object by setting the maximum number of composite Entry objects which may be contained in it.
 
Method Summary
 Object add(Object key)
          Add the given key to the map with null as value.
 void clear()
          Restores the object to its initial state.
 boolean containsKey(Object key)
           
 boolean containsValue(Object val)
           
 void end()
          Close the thread.
 Set entrySet()
           
 Object get(Object key)
          Gets the value associated with the specified key.
 Object getHeadKey()
          The key of head in the list, which can be used to remove the head from the list; suitable for implementing an LRU policy.
 int getMaxsize()
          Return the maximum size of the current site.
 int getMaxtime()
          Return the maximum size of the current site.
 boolean isEmpty()
           
 ArrayList keyList()
           
 Set keySet()
           
 Object put(Object key, Object value)
          Add the key, value pair to the map.
 void putAll(Map t)
           
 Object remove(Object key)
          Removes the specify key and its associated value from the map list.
 Object[] removeChunck(int elemToRemove)
          Removes the specify number of elements from the head of the linked list and also from the map.
 void run()
          Periodically cleans up the staled elements in the map.
 void setMaxsize(int maxsize)
          Modify the maximum size of this MapList.
 void setMaxtime(int maxtime)
          Modify the maximum time of this MapList.
 int size()
          Returns the number of key-value mappings in this map.
 Collection values()
           
 
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 Map map
A map mapping a key identifier to a composite Entry object implementing a doubly linked list.

Constructor Detail

MapList

public MapList(int maxsize,
               int maxtime)
Instantiate a new MapList object by setting the maximum number of composite Entry objects which may be contained in it.

Parameters:
maxsize - maximum number of pair objects which may be contained in this MapList. If zero, there is no upper limit to the the size.
maxtime - maximum number

MapList

public MapList(int maxsize)
Instantiate a new MapList object by setting the maximum number of key, value pairs which may be contained in it.

Parameters:
maxsize - Maximum number of objects which may be contained in this MapList. If zero, there is no upper limit to the the size.
Method Detail

isEmpty

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

containsKey

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

containsValue

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

putAll

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

clear

public void clear()
Restores the object to its initial state.

Specified by:
clear in interface Map

keyList

public ArrayList keyList()

keySet

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

values

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

entrySet

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

add

public Object add(Object key)
Add the given key to the map with null as value.

Parameters:
key - key with which to associate null.
Returns:
previous value associated with the specified key, or null.

put

public Object put(Object key,
                  Object value)
Add the key, value pair to the map.

Specified by:
put in interface Map
Parameters:
key - key with which the specified value is to be associated.
value - value to be associated with the specified key.
Returns:
previous value associated with specified key, or null if there was no mapping for key. A null return can also indicate that the map previously associated null with the specified key, if the implementation supports null values.

get

public Object get(Object key)
Gets the value associated with the specified key.

This method moves the accessed object to the tail of the linked listed enforcing the LRU policy for elements in the Map. This means that for every access to a given key, its element is moved to the tail of the list. This is because the most recent keys will appear at the tail part of the list, while the least recently used keys will appear at the head part of the list. Hence elements should be removed from the head of the list in order to reclaim memory consumed by the key item.

Specified by:
get in interface Map
Parameters:
key - key whose associated value is to be returned.
Returns:
the value to which this map maps the specified key.

remove

public Object remove(Object key)
Removes the specify key and its associated value from the map list.

Specified by:
remove in interface Map
Parameters:
key - The key whose assoicated value should be removed from the map list.
Returns:
The removed objects value, null if the specified key is not stored in the map list.

getHeadKey

public Object getHeadKey()
The key of head in the list, which can be used to remove the head from the list; suitable for implementing an LRU policy.

Returns:
key of the head element, which will be null of the list is empty.

removeChunck

public Object[] removeChunck(int elemToRemove)
Removes the specify number of elements from the head of the linked list and also from the map.

Parameters:
elemToRemove - The number of elements to be removed.

size

public int size()
Returns the number of key-value mappings in this map.

Specified by:
size in interface Map
Returns:
the number of key-value mappings in this map.

setMaxsize

public void setMaxsize(int maxsize)
Modify the maximum size of this MapList. If this MapList contains more elements than the new maximum size, removes the additional elements.

Parameters:
maxsize - the new maximum size

getMaxsize

public int getMaxsize()
Return the maximum size of the current site.


setMaxtime

public void setMaxtime(int maxtime)
Modify the maximum time of this MapList.

Parameters:
maxtime - the new maximum time

getMaxtime

public int getMaxtime()
Return the maximum size of the current site.


run

public void run()
Periodically cleans up the staled elements in the map.

Specified by:
run in interface Runnable

end

public void end()
Close the thread.