net.sf.beanlib.utils
Class CollectionUtils

java.lang.Object
  extended by net.sf.beanlib.utils.CollectionUtils

public class CollectionUtils
extends Object

Collection related utility methods.

Author:
Joe D. Velopar

Nested Class Summary
(package private) static class CollectionUtils.SynchronizedCollection<E>
          Basically cloned from and identification to java.util.Collections.SynchronizedCollection.
private static class CollectionUtils.SynchronizedMap<K,V>
          Basically cloned from and identification to java.util.Collections.SynchronizedMap.
(package private) static class CollectionUtils.SynchronizedSet<E>
          Basically cloned from and identification to java.util.Collections.SynchronizedSet.
(package private) static class CollectionUtils.SynchronizedSortedMap<K,V>
          Basically cloned from and identification to java.util.Collections.SynchronizedSortedMap.
(package private) static class CollectionUtils.SynchronizedSortedRangeMap<T,K,V>
          A thread-safe SortedRangeMap.
 
Constructor Summary
private CollectionUtils()
           
 
Method Summary
static
<T,K,V> SortedRangeMap<T,K,V>
synchronizedSortedRangeMap(SortedRangeMap<T,K,V> m)
          Returns a synchronized (thread-safe) sorted range map backed by the specified sorted range map.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CollectionUtils

private CollectionUtils()
Method Detail

synchronizedSortedRangeMap

public static <T,K,V> SortedRangeMap<T,K,V> synchronizedSortedRangeMap(SortedRangeMap<T,K,V> m)
Returns a synchronized (thread-safe) sorted range map backed by the specified sorted range map. In order to guarantee serial access, it is critical that all access to the backing sorted range map is accomplished through the returned sorted range map (or its views).

It is imperative that the user manually synchronize on the returned sorted range map when iterating over any of its collection views, or the collections views of any of its subMap, headMap or tailMap views.

  SortedRangeMap m = CollectionUtils.synchronizedSortedRangeMap(new RangeTreeMap());
      ...
  Set s = m.keySet();  // Needn't be in synchronized block
      ...
  synchronized(m) {  // Synchronizing on m, not s!
      Iterator i = s.iterator(); // Must be in synchronized block
      while (i.hasNext())
          foo(i.next());
  }
 
or:
  SortedRangeMap m = CollectionUtils.synchronizedSortedRangeMap(new RangeTreeMap());
  SortedMap m2 = m.subMap(foo, bar);
      ...
  Set s2 = m2.keySet();  // Needn't be in synchronized block
      ...
  synchronized(m) {  // Synchronizing on m, not m2 or s2!
      Iterator i = s.iterator(); // Must be in synchronized block
      while (i.hasNext())
          foo(i.next());
  }
 
Failure to follow this advice may result in non-deterministic behavior.

The returned sorted range map will be serializable if the specified sorted range map is serializable.

Parameters:
m - the sorted range map to be "wrapped" in a synchronized sorted range map.
Returns:
a synchronized view of the specified sorted range map.