org.omnaest.utils.structure.map
Class ThreadLocalMap<K,V>

java.lang.Object
  extended by org.omnaest.utils.structure.map.ThreadLocalMap<K,V>
All Implemented Interfaces:
Map<K,V>

public class ThreadLocalMap<K,V>
extends Object
implements Map<K,V>

A ThreadLocalMap is a Map stored independently for each Thread by using ThreadLocal. As underlying Map a LinkedHashMap is used as default. To use other implementations set the Map explicit for each Thread using setMap(Map) or provide another Factory for the Map creation using setMapFactory(Factory)

Author:
Omnaest

Nested Class Summary
 
Nested classes/interfaces inherited from interface java.util.Map
Map.Entry<K,V>
 
Field Summary
protected  Factory<Map<K,V>> mapFactory
           
protected  ThreadLocal<Map<K,V>> threadLocalForMap
           
 
Constructor Summary
ThreadLocalMap()
           
 
Method Summary
 void clear()
           
 boolean containsKey(Object key)
           
 boolean containsValue(Object value)
           
 Set<Map.Entry<K,V>> entrySet()
           
 boolean equals(Object o)
           
 V get(Object key)
           
 Map<K,V> getMap()
          Gets the underlying Map for the current Thread context
 int hashCode()
           
 boolean isEmpty()
           
 Set<K> keySet()
           
 V put(K key, V value)
           
 void putAll(Map<? extends K,? extends V> m)
           
 void remove()
          Clears the Map reference for the current Thread.
 V remove(Object key)
           
 ThreadLocalMap<K,V> setMap(Map<K,V> map)
          Sets the underlying Map for the current Thread
 void setMapFactory(Factory<Map<K,V>> mapFactory)
          Sets the Factory for new Map instances.
 int size()
           
 Collection<V> values()
           
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

threadLocalForMap

protected ThreadLocal<Map<K,V>> threadLocalForMap

mapFactory

protected Factory<Map<K,V>> mapFactory
Constructor Detail

ThreadLocalMap

public ThreadLocalMap()
Method Detail

clear

public void clear()
Specified by:
clear in interface Map<K,V>
See Also:
Map.clear()

containsKey

public boolean containsKey(Object key)
Specified by:
containsKey in interface Map<K,V>
Parameters:
key -
Returns:
See Also:
Map.containsKey(java.lang.Object)

containsValue

public boolean containsValue(Object value)
Specified by:
containsValue in interface Map<K,V>
Parameters:
value -
Returns:
See Also:
Map.containsValue(java.lang.Object)

entrySet

public Set<Map.Entry<K,V>> entrySet()
Specified by:
entrySet in interface Map<K,V>
Returns:
See Also:
Map.entrySet()

equals

public boolean equals(Object o)
Specified by:
equals in interface Map<K,V>
Overrides:
equals in class Object
Parameters:
o -
Returns:
See Also:
Map.equals(java.lang.Object)

get

public V get(Object key)
Specified by:
get in interface Map<K,V>
Parameters:
key -
Returns:
See Also:
Map.get(java.lang.Object)

getMap

public Map<K,V> getMap()
Gets the underlying Map for the current Thread context

Returns:

hashCode

public int hashCode()
Specified by:
hashCode in interface Map<K,V>
Overrides:
hashCode in class Object
Returns:
See Also:
Map.hashCode()

isEmpty

public boolean isEmpty()
Specified by:
isEmpty in interface Map<K,V>
Returns:
See Also:
Map.isEmpty()

keySet

public Set<K> keySet()
Specified by:
keySet in interface Map<K,V>
Returns:
See Also:
Map.keySet()

put

public V put(K key,
             V value)
Specified by:
put in interface Map<K,V>
Parameters:
key -
value -
Returns:
See Also:
Map.put(java.lang.Object, java.lang.Object)

putAll

public void putAll(Map<? extends K,? extends V> m)
Specified by:
putAll in interface Map<K,V>
Parameters:
m -
See Also:
Map.putAll(java.util.Map)

remove

public V remove(Object key)
Specified by:
remove in interface Map<K,V>
Parameters:
key -
Returns:
See Also:
Map.remove(java.lang.Object)

setMap

public ThreadLocalMap<K,V> setMap(Map<K,V> map)
Sets the underlying Map for the current Thread

Parameters:
map -
Returns:

setMapFactory

public void setMapFactory(Factory<Map<K,V>> mapFactory)
Sets the Factory for new Map instances. This factory is used initially if no Map instance exists for the current Thread

Parameters:
mapFactory -

size

public int size()
Specified by:
size in interface Map<K,V>
Returns:
See Also:
Map.size()

values

public Collection<V> values()
Specified by:
values in interface Map<K,V>
Returns:
See Also:
Map.values()

remove

public void remove()
Clears the Map reference for the current Thread. This allows the garbage collector to pickup the map reference.

See Also:
ThreadLocal.remove()


Copyright © 2013. All Rights Reserved.