org.compass.core.util.concurrent
Class ConcurrentLinkedHashMap<K,V>

java.lang.Object
  extended by java.util.AbstractMap<K,V>
      extended by org.compass.core.util.concurrent.ConcurrentLinkedHashMap<K,V>
All Implemented Interfaces:
Serializable, ConcurrentMap<K,V>, Map<K,V>

public class ConcurrentLinkedHashMap<K,V>
extends AbstractMap<K,V>
implements ConcurrentMap<K,V>, Serializable

A ConcurrentMap with a doubly-linked list running through its entries. This class provides the same semantics as a ConcurrentHashMap in terms of iterators, acceptable keys, and concurrency characteristics, but perform slightly worse due to the added expense of maintaining the linked list. It differs from LinkedHashMap in that it does not provide predictable iteration order. This map is intended to be used for caches and provides the following eviction policies:

The Second Chance eviction policy is recommended for common use cases as it provides the best mix of performance and efficiency of the supported replacement policies. If the Least Recently Used policy is chosen then the sizing should compensate for the proliferation of dead nodes on the linked list. While the values are removed immediately, the nodes are evicted only when they reach the head of the list. Under FIFO-based policies, dead nodes occur when explicit removals are requested and does not normally produce a noticeable impact on the map's hit rate. The LRU policy creates a dead node on every successful retrieval and a new node is placed at the tail of the list. For this reason, the LRU's efficiency cannot be compared directly to a LinkedHashMap evicting in access order.

Author:
Ben Manes
See Also:
Serialized Form

Nested Class Summary
static interface ConcurrentLinkedHashMap.EvictionListener<K,V>
          A listener registered for notification when an entry is evicted.
static class ConcurrentLinkedHashMap.EvictionPolicy
          The replacement policy to apply to determine which entry to discard to when the capacity has been reached.
(package private) static class ConcurrentLinkedHashMap.Node<K,V>
          A node on the double-linked list.
 
Nested classes/interfaces inherited from interface java.util.Map
Map.Entry<K,V>
 
Field Summary
(package private)  AtomicInteger capacity
           
(package private)  ConcurrentMap<K,ConcurrentLinkedHashMap.Node<K,V>> data
           
(package private)  ConcurrentLinkedHashMap.Node<K,V> head
           
(package private)  AtomicInteger length
           
(package private)  List<ConcurrentLinkedHashMap.EvictionListener<K,V>> listeners
           
(package private)  ConcurrentLinkedHashMap.EvictionPolicy policy
           
(package private)  ConcurrentLinkedHashMap.Node<K,V> tail
           
 
Constructor Summary
ConcurrentLinkedHashMap(ConcurrentLinkedHashMap.EvictionPolicy policy, int maximumCapacity, ConcurrentLinkedHashMap.EvictionListener<K,V>... listeners)
          Creates a new, empty, unbounded map with the specified maximum capacity and the default concurrencyLevel.
ConcurrentLinkedHashMap(ConcurrentLinkedHashMap.EvictionPolicy policy, int maximumCapacity, int concurrencyLevel, ConcurrentLinkedHashMap.EvictionListener<K,V>... listeners)
          Creates a new, empty, unbounded map with the specified maximum capacity and concurrency level.
 
Method Summary
 int capacity()
          Retrieves the maximum capacity of the map.
 void clear()
          
 boolean containsKey(Object key)
          
 boolean containsValue(Object value)
          
 Set<Map.Entry<K,V>> entrySet()
          
 V get(Object key)
          
 V put(K key, V value)
          
 V putIfAbsent(K key, V value)
          
 V remove(Object key)
          
 boolean remove(Object key, Object value)
          
 V replace(K key, V value)
          
 boolean replace(K key, V oldValue, V newValue)
          
 void setCapacity(int capacity)
          Sets the maximum capacity of the map and eagerly evicts entries until the it shrinks to the appropriate size.
 int size()
          
 
Methods inherited from class java.util.AbstractMap
clone, equals, hashCode, isEmpty, keySet, putAll, toString, values
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Map
equals, hashCode, isEmpty, keySet, putAll, values
 

Field Detail

listeners

final List<ConcurrentLinkedHashMap.EvictionListener<K,V>> listeners

data

final ConcurrentMap<K,ConcurrentLinkedHashMap.Node<K,V>> data

capacity

final AtomicInteger capacity

policy

final ConcurrentLinkedHashMap.EvictionPolicy policy

length

final AtomicInteger length

head

final ConcurrentLinkedHashMap.Node<K,V> head

tail

final ConcurrentLinkedHashMap.Node<K,V> tail
Constructor Detail

ConcurrentLinkedHashMap

public ConcurrentLinkedHashMap(ConcurrentLinkedHashMap.EvictionPolicy policy,
                               int maximumCapacity,
                               ConcurrentLinkedHashMap.EvictionListener<K,V>... listeners)
Creates a new, empty, unbounded map with the specified maximum capacity and the default concurrencyLevel.

Parameters:
policy - The eviction policy to apply when the size exceeds the maximum capacity.
maximumCapacity - The maximum capacity to coerces to. The size may exceed it temporarily.
listeners - The listeners registered for notification when an entry is evicted.

ConcurrentLinkedHashMap

public ConcurrentLinkedHashMap(ConcurrentLinkedHashMap.EvictionPolicy policy,
                               int maximumCapacity,
                               int concurrencyLevel,
                               ConcurrentLinkedHashMap.EvictionListener<K,V>... listeners)
Creates a new, empty, unbounded map with the specified maximum capacity and concurrency level.

Parameters:
policy - The eviction policy to apply when the size exceeds the maximum capacity.
maximumCapacity - The maximum capacity to coerces to. The size may exceed it temporarily.
concurrencyLevel - The estimated number of concurrently updating threads. The implementation performs internal sizing to try to accommodate this many threads.
listeners - The listeners registered for notification when an entry is evicted.
Method Detail

setCapacity

public void setCapacity(int capacity)
Sets the maximum capacity of the map and eagerly evicts entries until the it shrinks to the appropriate size.

Parameters:
capacity - The maximum capacity of the map.

capacity

public int capacity()
Retrieves the maximum capacity of the map.

Returns:
The maximum capacity.

size

public int size()

Specified by:
size in interface Map<K,V>
Overrides:
size in class AbstractMap<K,V>

clear

public void clear()

Specified by:
clear in interface Map<K,V>
Overrides:
clear in class AbstractMap<K,V>

containsKey

public boolean containsKey(Object key)

Specified by:
containsKey in interface Map<K,V>
Overrides:
containsKey in class AbstractMap<K,V>

containsValue

public boolean containsValue(Object value)

Specified by:
containsValue in interface Map<K,V>
Overrides:
containsValue in class AbstractMap<K,V>

get

public V get(Object key)

Specified by:
get in interface Map<K,V>
Overrides:
get in class AbstractMap<K,V>

put

public V put(K key,
             V value)

Specified by:
put in interface Map<K,V>
Overrides:
put in class AbstractMap<K,V>

putIfAbsent

public V putIfAbsent(K key,
                     V value)

Specified by:
putIfAbsent in interface ConcurrentMap<K,V>

remove

public V remove(Object key)

Specified by:
remove in interface Map<K,V>
Overrides:
remove in class AbstractMap<K,V>

remove

public boolean remove(Object key,
                      Object value)

Specified by:
remove in interface ConcurrentMap<K,V>

replace

public V replace(K key,
                 V value)

Specified by:
replace in interface ConcurrentMap<K,V>

replace

public boolean replace(K key,
                       V oldValue,
                       V newValue)

Specified by:
replace in interface ConcurrentMap<K,V>

entrySet

public Set<Map.Entry<K,V>> entrySet()

Specified by:
entrySet in interface Map<K,V>
Specified by:
entrySet in class AbstractMap<K,V>


Copyright (c) 2004-2009 The Compass Project.