|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.thavam.util.concurrent.BlockingHashMap<K,V>
K
- the type of keys maintained by this mapV
- the type of mapped valuespublic class BlockingHashMap<K,V>
This is similar to "unbounded buffer", in which the synchronizer elements inserted by producers and extracted by consumers. The only twist is that each product has a key & consumers know which product they are interested in. Attempts to put/offer an element into the map will always succeed because this is an unbound map; attempts to take element corresponding to a key that is not available on the map will block.
This map can be shutdown using clear. All consumers blocked on the map while invoking clear will be throw InterruptedException or return with null. Attempting any operation after shutdown will throw IllegalStateException.
This class implements some of optional methods of the Map
.
Nested Class Summary |
---|
Nested classes/interfaces inherited from interface java.util.Map |
---|
java.util.Map.Entry<K,V> |
Constructor Summary | |
---|---|
BlockingHashMap()
|
Method Summary | |
---|---|
void |
clear()
Shuts down this blocking map & removes all mappings from this map. |
boolean |
containsKey(java.lang.Object key)
Returns true if this map contains a mapping for the specified key. |
boolean |
containsValue(java.lang.Object value)
Returns true if this map maps one or more keys to the specified value. |
java.util.Set<java.util.Map.Entry<K,V>> |
entrySet()
Not supported Semantics of addition/removal to map outside the producer/consumer methods not defined |
V |
get(java.lang.Object key)
Returns the value to which the specified key is mapped, or null
if this map contains no mapping for the key. |
boolean |
isEmpty()
Returns true if this map contains no key-value mappings. |
boolean |
isKeyAvailable(K key)
Returns true if this map contains a mapping for the specified key. |
java.util.Set<K> |
keySet()
Not supported Semantics of addition/removal to map outside the producer/consumer methods not defined |
V |
offer(K key,
V value)
Associates the specified value with the specified key in this map. |
V |
offer(K key,
V value,
long timeout,
java.util.concurrent.TimeUnit unit)
Associates the specified value with the specified key in this map. |
V |
put(K key,
V value)
Associates the specified value with the specified key in this map. |
void |
putAll(java.util.Map<? extends K,? extends V> m)
To be supported |
V |
remove(java.lang.Object key)
Removes the mapping for a key from this map if it is present. |
int |
size()
Returns the number of key-value mappings in this map |
V |
take(java.lang.Object key)
Retrieves and removes the mapping for a key from this map if it is present, waiting if necessary until the mapping becomes available. |
V |
take(java.lang.Object key,
long timeout,
java.util.concurrent.TimeUnit unit)
Retrieves and removes the mapping for a key from this map if it is present, waiting if necessary until the mapping becomes available or the specified time elapses. |
java.util.Collection<V> |
values()
Not supported Semantics of addition/removal to map outside the producer/consumer methods not defined |
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 |
---|
public BlockingHashMap()
Method Detail |
---|
public boolean isKeyAvailable(K key)
isKeyAvailable
in interface BlockingMap<K,V>
key
- key whose presence in this map is to be tested
java.lang.ClassCastException
- if the key is of an inappropriate type for
this map
java.lang.NullPointerException
- if the specified key is null
java.lang.IllegalStateException
- if the map has been shut-downpublic boolean containsKey(java.lang.Object key)
containsKey
in interface java.util.Map<K,V>
key
- key whose presence in this map is to be tested
java.lang.ClassCastException
- if the key is of an inappropriate type for
this map (optional)
java.lang.NullPointerException
- if the specified key is null and this map
does not permit null keys (optional)
java.lang.IllegalStateException
- if the map has been shut-downpublic V get(java.lang.Object key)
null
if this map contains no mapping for the key.
Note that null
is used as a special marker to indicate the
absence of the requested key
get
in interface java.util.Map<K,V>
get
in interface BlockingMap<K,V>
key
- the key whose associated value is to be returned
null
if this map contains no mapping for the key
java.lang.ClassCastException
- if the key is of an inappropriate type for
this map
java.lang.NullPointerException
- if the specified key is null and this map
does not permit null keys (optional)
java.lang.IllegalStateException
- if the map has been shut-downpublic V put(K key, V value)
If the Map is bounded and there is no space to put the new mapping, this method returns with null. put on an unbound map will always succeed
Producers cannot put on a key that is already available on the map. Attempts to put a mapping whose key is already available on the map are ignored. However, the same mapping can be put in to the map after it is taken by consumer(s)
put
in interface java.util.Map<K,V>
put
in interface BlockingMap<K,V>
key
- key with which the specified value is to be associatedvalue
- value to be associated with the specified key
java.lang.UnsupportedOperationException
- if the put operation is
not supported by this map
java.lang.ClassCastException
- if the class of the specified key or value
prevents it from being stored in this map
java.lang.NullPointerException
- if the specified key or value is null and
this map does not permit null keys or values
java.lang.IllegalArgumentException
- if some property of the specified key or
value prevents it from being stored in this mappublic V remove(java.lang.Object key)
Returns the value to which this map previously associated the key, or null if the map contained no mapping for the key.
The map will not contain a mapping for the specified key once the call returns.
remove
in interface java.util.Map<K,V>
remove
in interface BlockingMap<K,V>
key
- key whose mapping is to be removed from the map
java.lang.UnsupportedOperationException
- if the remove operation is
not supported by this map
java.lang.ClassCastException
- if the key is of an inappropriate type for
this map (optional)
java.lang.NullPointerException
- if the specified key is null and this map
does not permit null keys (optional)
java.lang.IllegalStateException
- if the map has been shut-downpublic V offer(K key, V value) throws java.lang.InterruptedException
If the Map is bounded and there is no space to put the new mapping, this method blocks till space becomes available. offer on an unbound map will always succeed
Producers cannot offer a mapping on a key that is already available on the map. Attempts to such a mapping are ignored. However, the same mapping can be successfully offered after the existing mapping is taken by consumer(s)
offer
in interface BlockingMap<K,V>
key
- key with which the specified value is to be associatedvalue
- value to be associated with the specified key
java.lang.InterruptedException
- if interrupted while waiting
java.lang.ClassCastException
- if the class of the specified element prevents
it from being added to this queue
java.lang.NullPointerException
- if the specified element is null
java.lang.IllegalArgumentException
- if some property of the specified
element prevents it from being added to this queuepublic V take(java.lang.Object key) throws java.lang.InterruptedException
take
in interface BlockingMap<K,V>
key
- key whose mapping is to be removed from the map
java.lang.UnsupportedOperationException
- if the remove operation is
not supported by this map
java.lang.ClassCastException
- if the key is of an inappropriate type for
this map (optional)
java.lang.NullPointerException
- if the specified key is null and this map
does not permit null keys (optional)
java.lang.InterruptedException
- if interrupted while waiting
java.lang.IllegalStateException
- if the map has been shut-downpublic V offer(K key, V value, long timeout, java.util.concurrent.TimeUnit unit) throws java.lang.InterruptedException
If the Map is bounded and there is no space to put the new mapping, this method blocks till space becomes available or the specified time elapses. offer on an unbound map will always succeed
Producers cannot offer a mapping on a key that is already available on the map. Attempts to such a mapping are ignored. However, the same mapping can be successfully offered after the existing mapping is taken by consumer(s)
offer
in interface BlockingMap<K,V>
key
- key with which the specified value is to be associatedvalue
- value to be associated with the specified keytimeout
- how long to wait before giving up, in units of
unitunit
- a TimeUnit determining how to interpret the
timeout parameter
java.lang.InterruptedException
- if interrupted while waiting
java.lang.ClassCastException
- if the class of the specified element prevents
it from being added to this queue
java.lang.NullPointerException
- if the specified element is null
java.lang.IllegalArgumentException
- if some property of the specified
element prevents it from being added to this queue
java.lang.IllegalStateException
- if the map has been shut-downpublic V take(java.lang.Object key, long timeout, java.util.concurrent.TimeUnit unit) throws java.lang.InterruptedException
take
in interface BlockingMap<K,V>
key
- key with which the specified value is to be associatedtimeout
- how long to wait before giving up, in units of
unitunit
- a TimeUnit determining how to interpret the
java.lang.UnsupportedOperationException
- if the remove operation is
not supported by this map
java.lang.ClassCastException
- if the key is of an inappropriate type for
this map (optional)
java.lang.NullPointerException
- if the specified key is null and this map
does not permit null keys (optional)
java.lang.InterruptedException
- if interrupted while waiting
java.lang.IllegalStateException
- if the map has been shut-downpublic void clear()
Interrupts any threads waiting on any key in map before clearing. This is done to prevent threads being blocked forever
clear
in interface java.util.Map<K,V>
java.lang.IllegalStateException
- if the map has been shut-downpublic boolean containsValue(java.lang.Object value)
containsValue
in interface java.util.Map<K,V>
value
- value whose presence in this map is to be tested
java.lang.IllegalStateException
- if the map has been shut-down
java.lang.UnsupportedOperationException
public boolean isEmpty()
isEmpty
in interface java.util.Map<K,V>
public int size()
size
in interface java.util.Map<K,V>
java.lang.IllegalStateException
- if the map has been shut-downpublic java.util.Set<java.util.Map.Entry<K,V>> entrySet()
entrySet
in interface java.util.Map<K,V>
java.lang.IllegalStateException
- if the map has been shut-down
java.lang.UnsupportedOperationException
public java.util.Set<K> keySet()
keySet
in interface java.util.Map<K,V>
java.lang.IllegalStateException
- if the map has been shut-down
java.lang.UnsupportedOperationException
public java.util.Collection<V> values()
values
in interface java.util.Map<K,V>
java.lang.IllegalStateException
- if the map has been shut-down
java.lang.UnsupportedOperationException
public void putAll(java.util.Map<? extends K,? extends V> m)
putAll
in interface java.util.Map<K,V>
java.lang.UnsupportedOperationException
java.lang.IllegalStateException
- if the map has been shut-down
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |