JThreadKitTM
v1.1.0
(public members only)

com.jthreadkit.collection
Class WrappedSizeMonitoredMap

java.lang.Object
  |
  +--com.jthreadkit.AbstractSizeMonitored
        |
        +--com.jthreadkit.collection.WrappedSizeMonitoredMap
All Implemented Interfaces:
AccessibleLock, Map, SimpleShutdown, SizeMonitored, SizeMonitoredMap, TimedOutExceptionOption
Direct Known Subclasses:
WrappedSizeMonitoredSortedMap

public class WrappedSizeMonitoredMap
extends AbstractSizeMonitored
implements SizeMonitoredMap, Map, SizeMonitored

Used to wrap an instance of Map with synchronization and implementation of all the methods of SizeMonitored. This class can be used like this:
  
SizeMonitoredMap smm = 
        new WrappedSizeMonitoredMap(new HashMap());
SizeMonitored sm = smm;  // can be viewed as a SizeMonitored
Map m = smm;  // can be viewed as a Map too
Note that when possible it's best to avoid keeping a direct reference to the underlying "raw" Map (like this example above). This makes accidental use of the un-synchronized one impossible.

All method calls are synchronized on the object returned by getLockObject(). The object to lock on can be specified during construction or defaults to this. [There is no need to wrap the raw or resulting wrapped object by using Collections.synchronizedMap().]

To safely iterate in an multithreaded environment, use code like this:
  
SizeMonitoredMap smm = //...
Set set = smm.keySet(); // safely gets the set of keys
//...
synchronized ( smm.getLockObject() ) {  // note: use smm lock
    Iterator iter = set.iterator();  // note: key iterator from set
    while ( iter.hasNext() ) {
        Object obj = iter.next();
        //...
    }
}
The methods of SizeMonitored are nicely combined so that we can have one thread doing something like this:
  
smm.waitUntilEmpty();
while another thread is modifying (adding and removing elements) the object.


Inner classes inherited from class java.util.Map
Map.Entry
 
Constructor Summary
WrappedSizeMonitoredMap(Map map)
           
WrappedSizeMonitoredMap(Map map, Object lock, boolean useTimedOutException)
           
 
Method Summary
 void clear()
           
 boolean containsKey(Object o)
           
 boolean containsValue(Object o)
           
 Set entrySet()
           
 boolean equals(Object o)
           
 Object get(Object key)
           
 int hashCode()
           
 Set keySet()
           
 Object put(Object key, Object value)
           
 void putAll(Map additions)
           
 Object remove(Object key)
           
 int setCapacity(int requestedCapacity)
          Not supported, UnsupportedOperationException will always be thrown.
 int size()
           
 Collection values()
           
 
Methods inherited from class com.jthreadkit.AbstractSizeMonitored
addSizeChangeListener, getCapacity, getLockObject, getSize, isEmpty, isFull, isUseTimedOutExceptionSet, removeSizeChangeListener, setUseTimedOutException, shutdown, shutdownWhenEmpty, waitForSizeToChange, waitForSizeToChange, waitForSizeToClimbTo, waitForSizeToClimbTo, waitForSizeToFallTo, waitForSizeToFallTo, waitUntilEmpty, waitUntilEmpty, waitUntilFull, waitUntilFull, waitUntilSizeInRange, waitUntilSizeInRange, waitUntilSizeIs, waitUntilSizeIs, waitWhileEmpty, waitWhileEmpty, waitWhileFull, waitWhileFull, waitWhileSizeInRange, waitWhileSizeInRange, waitWhileSizeIs, waitWhileSizeIs
 
Methods inherited from class java.lang.Object
getClass, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface com.jthreadkit.SizeMonitored
addSizeChangeListener, getCapacity, getSize, isEmpty, isFull, removeSizeChangeListener, shutdownWhenEmpty, waitForSizeToChange, waitForSizeToChange, waitForSizeToClimbTo, waitForSizeToClimbTo, waitForSizeToFallTo, waitForSizeToFallTo, waitUntilEmpty, waitUntilEmpty, waitUntilFull, waitUntilFull, waitUntilSizeInRange, waitUntilSizeInRange, waitUntilSizeIs, waitUntilSizeIs, waitWhileEmpty, waitWhileEmpty, waitWhileFull, waitWhileFull, waitWhileSizeInRange, waitWhileSizeInRange, waitWhileSizeIs, waitWhileSizeIs
 
Methods inherited from interface com.jthreadkit.AccessibleLock
getLockObject
 
Methods inherited from interface com.jthreadkit.SimpleShutdown
shutdown
 
Methods inherited from interface com.jthreadkit.TimedOutExceptionOption
isUseTimedOutExceptionSet, setUseTimedOutException
 
Methods inherited from interface java.util.Map
isEmpty
 

Constructor Detail

WrappedSizeMonitoredMap

public WrappedSizeMonitoredMap(Map map,
                               Object lock,
                               boolean useTimedOutException)

WrappedSizeMonitoredMap

public WrappedSizeMonitoredMap(Map map)
Method Detail

setCapacity

public int setCapacity(int requestedCapacity)
                throws UnsupportedOperationException
Not supported, UnsupportedOperationException will always be thrown. Capacity is effectively Integer.MAX_VALUE and can't be lowered.
Specified by:
setCapacity in interface SizeMonitored
Overrides:
setCapacity in class AbstractSizeMonitored
Following copied from interface: com.jthreadkit.SizeMonitored
Parameters:
requestedCapacity - the desired new capacity. This number is silently increased to minimum of 1 and/or the value returned from SizeMonitored.getSize() if necessary.
Returns:
the actual new capacity--may be larger than the requested capacity.
Throws:
ShutdownException - see explanation.

clear

public void clear()
           throws ShutdownException
Specified by:
clear in interface Map

containsKey

public boolean containsKey(Object o)
Specified by:
containsKey in interface Map

containsValue

public boolean containsValue(Object o)
Specified by:
containsValue in interface Map

entrySet

public Set entrySet()
Specified by:
entrySet in interface Map

equals

public boolean equals(Object o)
Specified by:
equals in interface Map
Overrides:
equals in class Object

get

public Object get(Object key)
Specified by:
get in interface Map

hashCode

public int hashCode()
Specified by:
hashCode in interface Map
Overrides:
hashCode in class Object

keySet

public Set keySet()
Specified by:
keySet in interface Map

put

public Object put(Object key,
                  Object value)
           throws ShutdownException
Specified by:
put in interface Map

putAll

public void putAll(Map additions)
            throws ShutdownException
Specified by:
putAll in interface Map

remove

public Object remove(Object key)
              throws ShutdownException
Specified by:
remove in interface Map

size

public int size()
Specified by:
size in interface Map

values

public Collection values()
Specified by:
values in interface Map

JThreadKitTM
v1.1.0
(public members only)

© Copyright 2000-2001 Programix Incorporated. All rights reserved. JThreadKit home