ca.uhn.hl7v2.concurrent
Class BlockingHashMap<K,V>

java.lang.Object
  extended by ca.uhn.hl7v2.concurrent.BlockingHashMap<K,V>
Type Parameters:
K -
V -
All Implemented Interfaces:
BlockingMap<K,V>, Map<K,V>

public class BlockingHashMap<K,V>
extends Object
implements BlockingMap<K,V>

Default Implementation of a BlockingMap.

Note: While it is not actively prevented that more then one thread waits for an entry, it is not guaranteed that all waiting threads will receive the entry once it became available. Other implementations may choose to count the waiting threads and/or to remove an available value after a grace period.


Nested Class Summary
 
Nested classes/interfaces inherited from interface java.util.Map
Map.Entry<K,V>
 
Constructor Summary
BlockingHashMap()
           
BlockingHashMap(ExecutorService executor)
           
 
Method Summary
 Future<V> asyncPoll(K key, long timeout, TimeUnit unit)
          Polls for an entry in a background thread.
 Future<V> asyncTake(K key)
          Waits for an entry in a background thread.
 void clear()
          Clears all existing entries.
 boolean containsKey(Object key)
          Returns true if an entry with the given key is available
 boolean containsValue(Object value)
           
 Set<Map.Entry<K,V>> entrySet()
           
 V get(Object key)
          Returns an available entry without removing it from the map
 boolean give(K key, V value)
          Adds an entry only if there's already a consumer waiting for the value.
 boolean isEmpty()
          Returns true if no entry is available for consumers
 Set<K> keySet()
          Returns the keys of available entries
 V poll(K key, long timeout, TimeUnit unit)
          Waits for the specified amount of time for an entry with the given key and returns the associated value.
 V put(K key, V value)
           
 void putAll(Map<? extends K,? extends V> t)
           
 V remove(Object key)
          Removes an entry, regardless whether a value has been set or not.
 int size()
          Returns the number of available values
 V take(K key)
          Waits for an entry for the given key and returns the associated value.
 Collection<V> 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
 

Constructor Detail

BlockingHashMap

public BlockingHashMap()

BlockingHashMap

public BlockingHashMap(ExecutorService executor)
Method Detail

keySet

public Set<K> keySet()
Returns the keys of available entries

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

get

public V get(Object key)
Returns an available entry without removing it from the map

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

containsKey

public boolean containsKey(Object key)
Returns true if an entry with the given key is available

Specified by:
containsKey in interface Map<K,V>
See Also:
Map.containsKey(java.lang.Object)

put

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

give

public boolean give(K key,
                    V value)
Description copied from interface: BlockingMap
Adds an entry only if there's already a consumer waiting for the value.

Specified by:
give in interface BlockingMap<K,V>
Returns:
true if entry was added and a consumer is already waiting for the value, false otherwise
See Also:
BlockingMap.give(java.lang.Object, java.lang.Object)

take

public V take(K key)
       throws InterruptedException
Description copied from interface: BlockingMap
Waits for an entry for the given key and returns the associated value. May return null if the producer withdraws the entry without providing a value.

Specified by:
take in interface BlockingMap<K,V>
Returns:
the value of the entry
Throws:
InterruptedException

asyncTake

public Future<V> asyncTake(K key)
                    throws InterruptedException
Description copied from interface: BlockingMap
Waits for an entry in a background thread.

Specified by:
asyncTake in interface BlockingMap<K,V>
Returns:
Future the result
Throws:
InterruptedException

poll

public V poll(K key,
              long timeout,
              TimeUnit unit)
       throws InterruptedException
Description copied from interface: BlockingMap
Waits for the specified amount of time for an entry with the given key and returns the associated value. Returns null if no value was provided within the poll time. May return null if the producer withdraws the entry without providing a value.

Specified by:
poll in interface BlockingMap<K,V>
Returns:
Throws:
InterruptedException

asyncPoll

public Future<V> asyncPoll(K key,
                           long timeout,
                           TimeUnit unit)
Description copied from interface: BlockingMap
Polls for an entry in a background thread.

Specified by:
asyncPoll in interface BlockingMap<K,V>
Returns:
Future the result

isEmpty

public boolean isEmpty()
Returns true if no entry is available for consumers

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

size

public int size()
Returns the number of available values

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

remove

public V remove(Object key)
Removes an entry, regardless whether a value has been set or not. Waiting consumers will receive a null value.

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

clear

public void clear()
Clears all existing entries. Waiting consumers will receive a null value for each removed entry.

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

values

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

entrySet

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

putAll

public void putAll(Map<? extends K,? extends V> t)
Specified by:
putAll in interface Map<K,V>

containsValue

public boolean containsValue(Object value)
Specified by:
containsValue in interface Map<K,V>


Copyright © 2001-2012 University Health Network. All Rights Reserved.