com.thimbleware.jmemcached.storage.hash
Class ConcurrentLinkedHashMap<K,V extends SizedItem>

java.lang.Object
  extended by java.util.AbstractMap<K,V>
      extended by com.thimbleware.jmemcached.storage.hash.ConcurrentLinkedHashMap<K,V>
All Implemented Interfaces:
CacheStorage<K,V>, java.io.Serializable, java.util.concurrent.ConcurrentMap<K,V>, java.util.Map<K,V>

public final class ConcurrentLinkedHashMap<K,V extends SizedItem>
extends java.util.AbstractMap<K,V>
implements java.io.Serializable, CacheStorage<K,V>

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:

Author:
Ben Manes
See Also:
http://code.google.com/p/concurrentlinkedhashmap/, 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 when the capacity has been reached.
protected static class ConcurrentLinkedHashMap.Node<K,V>
          A node on the double-linked list.
 
Nested classes/interfaces inherited from class java.util.AbstractMap
java.util.AbstractMap.SimpleImmutableEntry<K,V>
 
Nested classes/interfaces inherited from interface java.util.Map
java.util.Map.Entry<K,V>
 
Method Summary
 int capacity()
          Retrieves the maximum capacity of the map.
 void clear()
          
 void close()
          Close the storage unit, deallocating any resources it might be currently holding.
 boolean containsKey(java.lang.Object key)
          
 boolean containsValue(java.lang.Object value)
          
static
<K,V extends SizedItem>
ConcurrentLinkedHashMap<K,V>
create(ConcurrentLinkedHashMap.EvictionPolicy policy, int maximumCapacity, long maximumMemoryCapacity)
          Creates a map with the specified eviction policy, maximum capacity, and at the default concurrency level.
static
<K,V extends SizedItem>
ConcurrentLinkedHashMap<K,V>
create(ConcurrentLinkedHashMap.EvictionPolicy policy, int maximumCapacity, long maximumMemoryCapacity, ConcurrentLinkedHashMap.EvictionListener<K,V> listener)
          Creates a map with the specified eviction policy, maximum capacity, eviction listener, and at the default concurrency level.
static
<K,V extends SizedItem>
ConcurrentLinkedHashMap<K,V>
create(ConcurrentLinkedHashMap.EvictionPolicy policy, int maximumCapacity, long maximumMemoryCapacity, int concurrencyLevel)
          Creates a map with the specified eviction policy, maximum capacity, and concurrency level.
static
<K,V extends SizedItem>
ConcurrentLinkedHashMap<K,V>
create(ConcurrentLinkedHashMap.EvictionPolicy policy, int maximumCapacity, long maximumMemoryCapacity, int concurrencyLevel, ConcurrentLinkedHashMap.EvictionListener<K,V> listener)
          Creates a map with the specified eviction policy, maximum capacity, eviction listener, and concurrency level.
 java.util.Set<java.util.Map.Entry<K,V>> entrySet()
          
 V get(java.lang.Object key)
          
 long getMemoryCapacity()
           
 long getMemoryUsed()
           
 java.util.Set<K> keySet()
          
 V put(K key, V value)
          
 V putIfAbsent(K key, V value)
          
 V remove(java.lang.Object key)
          
 boolean remove(java.lang.Object key, java.lang.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 it shrinks to the appropriate size.
 void setMemoryCapacity(int capacity)
          Sets the maximum capacity of the map and eagerly evicts entries until it shrinks to the appropriate size.
 int size()
          
 java.util.Collection<V> values()
          
 
Methods inherited from class java.util.AbstractMap
clone, equals, hashCode, isEmpty, putAll, toString
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Map
equals, hashCode, isEmpty, putAll
 

Method Detail

create

public static <K,V extends SizedItem> ConcurrentLinkedHashMap<K,V> create(ConcurrentLinkedHashMap.EvictionPolicy policy,
                                                                          int maximumCapacity,
                                                                          long maximumMemoryCapacity)
Creates a map with the specified eviction policy, maximum capacity, and at the default 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.

create

public static <K,V extends SizedItem> ConcurrentLinkedHashMap<K,V> create(ConcurrentLinkedHashMap.EvictionPolicy policy,
                                                                          int maximumCapacity,
                                                                          long maximumMemoryCapacity,
                                                                          ConcurrentLinkedHashMap.EvictionListener<K,V> listener)
Creates a map with the specified eviction policy, maximum capacity, eviction listener, and at the default 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.
listener - The listener registered for notification when an entry is evicted.

create

public static <K,V extends SizedItem> ConcurrentLinkedHashMap<K,V> create(ConcurrentLinkedHashMap.EvictionPolicy policy,
                                                                          int maximumCapacity,
                                                                          long maximumMemoryCapacity,
                                                                          int concurrencyLevel)
Creates a map with the specified eviction policy, 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.

create

public static <K,V extends SizedItem> ConcurrentLinkedHashMap<K,V> create(ConcurrentLinkedHashMap.EvictionPolicy policy,
                                                                          int maximumCapacity,
                                                                          long maximumMemoryCapacity,
                                                                          int concurrencyLevel,
                                                                          ConcurrentLinkedHashMap.EvictionListener<K,V> listener)
Creates a map with the specified eviction policy, maximum capacity, eviction listener, 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.
listener - The listener registered for notification when an entry is evicted.

getMemoryCapacity

public long getMemoryCapacity()
Specified by:
getMemoryCapacity in interface CacheStorage<K,V extends SizedItem>
Returns:
the capacity (in bytes) of the storage

getMemoryUsed

public long getMemoryUsed()
Specified by:
getMemoryUsed in interface CacheStorage<K,V extends SizedItem>
Returns:
the current usage (in bytes) of the storage

setCapacity

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

Parameters:
capacity - The maximum capacity of the map.

setMemoryCapacity

public void setMemoryCapacity(int capacity)
Sets the maximum capacity of the map and eagerly evicts entries until 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.

Specified by:
capacity in interface CacheStorage<K,V extends SizedItem>
Returns:
The maximum capacity.

close

public void close()
Description copied from interface: CacheStorage
Close the storage unit, deallocating any resources it might be currently holding.

Specified by:
close in interface CacheStorage<K,V extends SizedItem>

size

public int size()

Specified by:
size in interface java.util.Map<K,V extends SizedItem>
Overrides:
size in class java.util.AbstractMap<K,V extends SizedItem>

clear

public void clear()

Specified by:
clear in interface java.util.Map<K,V extends SizedItem>
Overrides:
clear in class java.util.AbstractMap<K,V extends SizedItem>

containsKey

public boolean containsKey(java.lang.Object key)

Specified by:
containsKey in interface java.util.Map<K,V extends SizedItem>
Overrides:
containsKey in class java.util.AbstractMap<K,V extends SizedItem>

containsValue

public boolean containsValue(java.lang.Object value)

Specified by:
containsValue in interface java.util.Map<K,V extends SizedItem>
Overrides:
containsValue in class java.util.AbstractMap<K,V extends SizedItem>

get

public V get(java.lang.Object key)

Specified by:
get in interface java.util.Map<K,V extends SizedItem>
Overrides:
get in class java.util.AbstractMap<K,V extends SizedItem>

put

public V put(K key,
             V value)

Specified by:
put in interface java.util.Map<K,V extends SizedItem>
Overrides:
put in class java.util.AbstractMap<K,V extends SizedItem>

putIfAbsent

public V putIfAbsent(K key,
                     V value)

Specified by:
putIfAbsent in interface java.util.concurrent.ConcurrentMap<K,V extends SizedItem>

remove

public V remove(java.lang.Object key)

Specified by:
remove in interface java.util.Map<K,V extends SizedItem>
Overrides:
remove in class java.util.AbstractMap<K,V extends SizedItem>

remove

public boolean remove(java.lang.Object key,
                      java.lang.Object value)

Specified by:
remove in interface java.util.concurrent.ConcurrentMap<K,V extends SizedItem>

replace

public V replace(K key,
                 V value)

Specified by:
replace in interface java.util.concurrent.ConcurrentMap<K,V extends SizedItem>

replace

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

Specified by:
replace in interface java.util.concurrent.ConcurrentMap<K,V extends SizedItem>

keySet

public java.util.Set<K> keySet()

Specified by:
keySet in interface java.util.Map<K,V extends SizedItem>
Overrides:
keySet in class java.util.AbstractMap<K,V extends SizedItem>

values

public java.util.Collection<V> values()

Specified by:
values in interface java.util.Map<K,V extends SizedItem>
Overrides:
values in class java.util.AbstractMap<K,V extends SizedItem>

entrySet

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

Specified by:
entrySet in interface java.util.Map<K,V extends SizedItem>
Specified by:
entrySet in class java.util.AbstractMap<K,V extends SizedItem>


Copyright © 2008-2011 ThimbleWare. All Rights Reserved.