You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by sc...@apache.org on 2002/12/15 14:05:04 UTC

cvs commit: jakarta-commons/collections/src/java/org/apache/commons/collections SetUtils.java MapUtils.java ListUtils.java CollectionUtils.java BufferUtils.java

scolebourne    2002/12/15 05:05:04

  Modified:    collections/src/java/org/apache/commons/collections
                        SetUtils.java MapUtils.java ListUtils.java
                        CollectionUtils.java BufferUtils.java
  Log:
  Add empty unmodifiable implementations for all collections
  Add synchronized/unmodifiable implementations for all collections
  
  Revision  Changes    Path
  1.8       +98 -6     jakarta-commons/collections/src/java/org/apache/commons/collections/SetUtils.java
  
  Index: SetUtils.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/SetUtils.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- SetUtils.java	13 Oct 2002 00:38:36 -0000	1.7
  +++ SetUtils.java	15 Dec 2002 13:05:03 -0000	1.8
  @@ -60,9 +60,11 @@
    */
   package org.apache.commons.collections;
   
  +import java.util.Collections;
   import java.util.Comparator;
   import java.util.Set;
   import java.util.SortedSet;
  +import java.util.TreeSet;
   /**
    * Provides static utility methods and decorators for {@link Set} 
    * and {@link SortedSet} instances.
  @@ -75,9 +77,21 @@
   public class SetUtils {
   
       /**
  -     * Prevents instantiation.
  +     * An empty unmodifiable set.
  +     * This uses the {@link #java.util.Collections Collections} implementation 
  +     * and is provided for completeness.
        */
  -    private SetUtils() {
  +    public static final Set EMPTY_SET = Collections.EMPTY_SET;
  +    /**
  +     * An empty unmodifiable sorted set.
  +     * This is not provided in the JDK.
  +     */
  +    public static final SortedSet EMPTY_SORTED_SET = Collections.unmodifiableSortedSet(new TreeSet());
  +
  +    /**
  +     * <code>SetUtils</code> should not normally be instantiated.
  +     */
  +    public SetUtils() {
       }
   
   
  @@ -134,6 +148,45 @@
       }
   
       /**
  +     * Returns a synchronized set backed by the given set.
  +     * <p>
  +     * You must manually synchronize on the returned buffer's iterator to 
  +     * avoid non-deterministic behavior:
  +     *  
  +     * <pre>
  +     * Set s = SetUtils.synchronizedSet(mySet);
  +     * synchronized (s) {
  +     *     Iterator i = s.iterator();
  +     *     while (i.hasNext()) {
  +     *         process (i.next());
  +     *     }
  +     * }
  +     * </pre>
  +     * 
  +     * This method uses the implementation in {@link java.util.Collections Collections}.
  +     * 
  +     * @param set  the set to synchronize, must not be null
  +     * @return a synchronized set backed by the given set
  +     * @throws IllegalArgumentException  if the set is null
  +     */
  +    public static Set synchronizedSet(Set set) {
  +        return Collections.synchronizedSet(set);
  +    }
  +
  +    /**
  +     * Returns an unmodifiable set backed by the given set.
  +     * <p>
  +     * This method uses the implementation in {@link java.util.Collections Collections}.
  +     *
  +     * @param set  the set to make unmodifiable, must not be null
  +     * @return an unmodifiable set backed by the given set
  +     * @throws IllegalArgumentException  if the set is null
  +     */
  +    public static Set unmodifiableSet(Set set) {
  +        return Collections.unmodifiableSet(set);
  +    }
  +
  +    /**
        * Returns a predicated set backed by the given set.  Only objects
        * that pass the test in the given predicate can be added to the set.
        * It is important not to use the original set after invoking this 
  @@ -146,6 +199,45 @@
        */
       public static Set predicatedSet(Set set, Predicate predicate) {
           return new PredicatedSet(set, predicate);
  +    }
  +
  +    /**
  +     * Returns a synchronized sorted set backed by the given sorted set.
  +     * <p>
  +     * You must manually synchronize on the returned buffer's iterator to 
  +     * avoid non-deterministic behavior:
  +     *  
  +     * <pre>
  +     * Set s = SetUtils.synchronizedSet(mySet);
  +     * synchronized (s) {
  +     *     Iterator i = s.iterator();
  +     *     while (i.hasNext()) {
  +     *         process (i.next());
  +     *     }
  +     * }
  +     * </pre>
  +     * 
  +     * This method uses the implementation in {@link java.util.Collections Collections}.
  +     * 
  +     * @param set  the sorted set to synchronize, must not be null
  +     * @return a synchronized set backed by the given set
  +     * @throws IllegalArgumentException  if the set is null
  +     */
  +    public static SortedSet synchronizedSortedSet(SortedSet set) {
  +        return Collections.synchronizedSortedSet(set);
  +    }
  +
  +    /**
  +     * Returns an unmodifiable sorted set backed by the given sorted set.
  +     * <p>
  +     * This method uses the implementation in {@link java.util.Collections Collections}.
  +     *
  +     * @param set  the sorted set to make unmodifiable, must not be null
  +     * @return an unmodifiable set backed by the given set
  +     * @throws IllegalArgumentException  if the set is null
  +     */
  +    public static SortedSet unmodifiableSortedSet(SortedSet set) {
  +        return Collections.unmodifiableSortedSet(set);
       }
   
       /**
  
  
  
  1.15      +95 -4     jakarta-commons/collections/src/java/org/apache/commons/collections/MapUtils.java
  
  Index: MapUtils.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/MapUtils.java,v
  retrieving revision 1.14
  retrieving revision 1.15
  diff -u -r1.14 -r1.15
  --- MapUtils.java	13 Oct 2002 22:31:35 -0000	1.14
  +++ MapUtils.java	15 Dec 2002 13:05:03 -0000	1.15
  @@ -89,11 +89,22 @@
    * @author Stephen Colebourne
    */
   public class MapUtils {
  +    
  +    /**
  +     * An empty unmodifiable map.
  +     * This was not provided in JDK1.2.
  +     */
  +    public static final Map EMPTY_MAP = Collections.unmodifiableMap(new HashMap(1));
  +    /**
  +     * An empty unmodifiable sorted map.
  +     * This is not provided in the JDK.
  +     */
  +    public static final SortedMap EMPTY_SORTED_MAP = Collections.unmodifiableSortedMap(new TreeMap());
   
       private static int debugIndent = 0;
   
       /**
  -     *  Please don't instantiate a <Code>MapUtils</Code>.
  +     * <code>MapUtils</code> should not normally be instantiated.
        */
       public MapUtils() {
       }    
  @@ -1061,6 +1072,46 @@
   
   
       /**
  +     * Returns a synchronized map backed by the given map.
  +     * <p>
  +     * You must manually synchronize on the returned buffer's iterator to 
  +     * avoid non-deterministic behavior:
  +     *  
  +     * <pre>
  +     * Map m = MapUtils.synchronizedMap(myMap);
  +     * Set s = m.keySet();  // outside synchronized block
  +     * synchronized (m) {  // synchronized on MAP!
  +     *     Iterator i = s.iterator();
  +     *     while (i.hasNext()) {
  +     *         process (i.next());
  +     *     }
  +     * }
  +     * </pre>
  +     * 
  +     * This method uses the implementation in {@link java.util.Collections Collections}.
  +     * 
  +     * @param map  the map to synchronize, must not be null
  +     * @return a synchronized map backed by the given map
  +     * @throws IllegalArgumentException  if the map is null
  +     */
  +    public static Map synchronizedMap(Map map) {
  +        return Collections.synchronizedMap(map);
  +    }
  +
  +    /**
  +     * Returns an unmodifiable map backed by the given map.
  +     * <p>
  +     * This method uses the implementation in {@link java.util.Collections Collections}.
  +     *
  +     * @param map  the map to make unmodifiable, must not be null
  +     * @return an unmodifiable map backed by the given map
  +     * @throws IllegalArgumentException  if the map is null
  +     */
  +    public static Map unmodifiableMap(Map map) {
  +        return Collections.unmodifiableMap(map);
  +    }
  +
  +    /**
        * Returns a predicated map backed by the given map.  Only keys and
        * values that pass the given predicates can be added to the map.
        * It is important not to use the original map after invoking this 
  @@ -1121,6 +1172,46 @@
        */
       public static Map lazyMap(Map map, Factory factory) {
           return new LazyMap(map, factory);
  +    }
  +
  +    /**
  +     * Returns a synchronized sorted map backed by the given sorted map.
  +     * <p>
  +     * You must manually synchronize on the returned buffer's iterator to 
  +     * avoid non-deterministic behavior:
  +     *  
  +     * <pre>
  +     * Map m = MapUtils.synchronizedSortedMap(myMap);
  +     * Set s = m.keySet();  // outside synchronized block
  +     * synchronized (m) {  // synchronized on MAP!
  +     *     Iterator i = s.iterator();
  +     *     while (i.hasNext()) {
  +     *         process (i.next());
  +     *     }
  +     * }
  +     * </pre>
  +     * 
  +     * This method uses the implementation in {@link java.util.Collections Collections}.
  +     * 
  +     * @param map  the map to synchronize, must not be null
  +     * @return a synchronized map backed by the given map
  +     * @throws IllegalArgumentException  if the map is null
  +     */
  +    public static Map synchronizedSortedMap(SortedMap map) {
  +        return Collections.synchronizedSortedMap(map);
  +    }
  +
  +    /**
  +     * Returns an unmodifiable sorted map backed by the given sorted map.
  +     * <p>
  +     * This method uses the implementation in {@link java.util.Collections Collections}.
  +     *
  +     * @param map  the sorted map to make unmodifiable, must not be null
  +     * @return an unmodifiable map backed by the given map
  +     * @throws IllegalArgumentException  if the map is null
  +     */
  +    public static Map unmodifiableSortedMap(SortedMap map) {
  +        return Collections.unmodifiableSortedMap(map);
       }
   
       /**
  
  
  
  1.12      +51 -4     jakarta-commons/collections/src/java/org/apache/commons/collections/ListUtils.java
  
  Index: ListUtils.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/ListUtils.java,v
  retrieving revision 1.11
  retrieving revision 1.12
  diff -u -r1.11 -r1.12
  --- ListUtils.java	13 Oct 2002 00:38:36 -0000	1.11
  +++ ListUtils.java	15 Dec 2002 13:05:03 -0000	1.12
  @@ -62,6 +62,7 @@
   
   import java.util.ArrayList;
   import java.util.Collection;
  +import java.util.Collections;
   import java.util.Iterator;
   import java.util.List;
   import java.util.ListIterator;
  @@ -78,7 +79,14 @@
   public class ListUtils {
   
       /**
  -     *  Please don't ever instantiate a <Code>ListUtils</Code>.
  +     * An empty unmodifiable list.
  +     * This uses the {@link #java.util.Collections Collections} implementation 
  +     * and is provided for completeness.
  +     */
  +    public static final List EMPTY_LIST = Collections.EMPTY_LIST;
  +    
  +    /**
  +     * <code>ListUtils</code> should not normally be instantiated.
        */
       public ListUtils() {
       }
  @@ -443,6 +451,45 @@
   
       }
   
  +
  +    /**
  +     * Returns a synchronized list backed by the given list.
  +     * <p>
  +     * You must manually synchronize on the returned buffer's iterator to 
  +     * avoid non-deterministic behavior:
  +     *  
  +     * <pre>
  +     * List list = ListUtils.synchronizedList(myList);
  +     * synchronized (list) {
  +     *     Iterator i = list.iterator();
  +     *     while (i.hasNext()) {
  +     *         process (i.next());
  +     *     }
  +     * }
  +     * </pre>
  +     * 
  +     * This method uses the implementation in {@link java.util.Collections Collections}.
  +     * 
  +     * @param list  the list to synchronize, must not be null
  +     * @return a synchronized list backed by the given list
  +     * @throws IllegalArgumentException  if the list is null
  +     */
  +    public static List synchronizedList(List list) {
  +        return Collections.synchronizedList(list);
  +    }
  +
  +    /**
  +     * Returns an unmodifiable list backed by the given list.
  +     * <p>
  +     * This method uses the implementation in {@link java.util.Collections Collections}.
  +     *
  +     * @param list  the list to make unmodifiable, must not be null
  +     * @return an unmodifiable list backed by the given list
  +     * @throws IllegalArgumentException  if the list is null
  +     */
  +    public static List unmodifiableList(List list) {
  +        return Collections.unmodifiableList(list);
  +    }
   
       /**
        * Returns a predicated list backed by the given list.  Only objects
  
  
  
  1.24      +53 -5     jakarta-commons/collections/src/java/org/apache/commons/collections/CollectionUtils.java
  
  Index: CollectionUtils.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/CollectionUtils.java,v
  retrieving revision 1.23
  retrieving revision 1.24
  diff -u -r1.23 -r1.24
  --- CollectionUtils.java	11 Dec 2002 19:05:14 -0000	1.23
  +++ CollectionUtils.java	15 Dec 2002 13:05:03 -0000	1.24
  @@ -62,6 +62,7 @@
   
   import java.util.ArrayList;
   import java.util.Collection;
  +import java.util.Collections;
   import java.util.Enumeration;
   import java.util.HashMap;
   import java.util.HashSet;
  @@ -84,18 +85,26 @@
    * @author Steve Downey
    * @author <a href="herve.quiroz@esil.univ-mrs.fr">Herve Quiroz</a>
    * @author BluePhelix@web.de (Peter)
  + * @author Stephen Colebourne
    * @version $Revision$ $Date$
    */
   public class CollectionUtils {
   
       /**
  +     * An empty unmodifiable collection.
  +     * The JDK provides empty Set and List implementations which could be used for
  +     * this purpose. However they could be cast to Set or List which might be
  +     * undesirable. This implementation only implements Collection.
  +     */
  +    public static final Collection EMPTY_COLLECTION = Collections.unmodifiableCollection(new ArrayList());
  +    /**
        * The empty iterator (immutable).
        * @deprecated use IteratorUtils.EMPTY_ITERATOR
        */
       public static final Iterator EMPTY_ITERATOR = IteratorUtils.EMPTY_ITERATOR;
   
       /**
  -     * Please don't ever instantiate a <code>CollectionUtils</code>.
  +     * <code>CollectionUtils</code> should not normally be instantiated.
        */
       public CollectionUtils() {
       }
  @@ -1120,6 +1129,45 @@
   
       }
   
  +
  +    /**
  +     * Returns a synchronized collection backed by the given collection.
  +     * <p>
  +     * You must manually synchronize on the returned buffer's iterator to 
  +     * avoid non-deterministic behavior:
  +     *  
  +     * <pre>
  +     * Collection c = CollectionUtils.synchronizedCollection(myCollection);
  +     * synchronized (c) {
  +     *     Iterator i = c.iterator();
  +     *     while (i.hasNext()) {
  +     *         process (i.next());
  +     *     }
  +     * }
  +     * </pre>
  +     * 
  +     * This method uses the implementation in {@link java.util.Collections Collections}.
  +     * 
  +     * @param collection  the collection to synchronize, must not be null
  +     * @return a synchronized collection backed by the given collection
  +     * @throws IllegalArgumentException  if the collection is null
  +     */
  +    public static Collection synchronizedCollection(Collection collection) {
  +        return Collections.synchronizedCollection(collection);
  +    }
  +
  +    /**
  +     * Returns an unmodifiable collection backed by the given collection.
  +     * <p>
  +     * This method uses the implementation in {@link java.util.Collections Collections}.
  +     *
  +     * @param collection  the collection to make unmodifiable, must not be null
  +     * @return an unmodifiable collection backed by the given collection
  +     * @throws IllegalArgumentException  if the collection is null
  +     */
  +    public static Collection unmodifiableCollection(Collection collection) {
  +        return Collections.unmodifiableCollection(collection);
  +    }
   
       /**
        * Returns a predicated collection backed by the given collection.
  
  
  
  1.10      +11 -7     jakarta-commons/collections/src/java/org/apache/commons/collections/BufferUtils.java
  
  Index: BufferUtils.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/BufferUtils.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- BufferUtils.java	13 Oct 2002 00:38:36 -0000	1.9
  +++ BufferUtils.java	15 Dec 2002 13:05:03 -0000	1.10
  @@ -72,11 +72,15 @@
   public class BufferUtils {
   
       /**
  -     * Restrictive constructor
  +     * An empty unmodifiable buffer.
        */
  -    private BufferUtils() {
  +    public static final Buffer EMPTY_BUFFER = BufferUtils.unmodifiableBuffer(new ArrayStack());
  +    
  +    /**
  +     * <code>BufferUtils</code> should not normally be instantiated.
  +     */
  +    public BufferUtils() {
       }
  -
   
       /**
        * Returns a synchronized buffer backed by the given buffer.
  
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>