commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From neiloto...@apache.org
Subject cvs commit: jakarta-commons/collections/src/test/org/apache/commons/collections TestCollectionUtils.java
Date Sat, 11 Dec 2004 06:30:38 GMT
neilotoole    2004/12/10 22:30:38

  Modified:    collections/src/java/org/apache/commons/collections
                        CollectionUtils.java
               collections/src/test/org/apache/commons/collections
                        TestCollectionUtils.java
  Log:
  Added new methods to CollectionUtils (plus associated test cases)
  #retainAll(Collection, Collection)
  #removeAll(Collection, Collection)
  #unmodifiableCollectionCopy(Collection)
  
  Revision  Changes    Path
  1.63      +57 -2     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.62
  retrieving revision 1.63
  diff -u -r1.62 -r1.63
  --- CollectionUtils.java	17 Jul 2004 21:38:33 -0000	1.62
  +++ CollectionUtils.java	11 Dec 2004 06:30:38 -0000	1.63
  @@ -51,6 +51,7 @@
    * @author Phil Steitz
    * @author Steven Melzer
    * @author Jon Schewe
  + * @author Neil O'Toole
    */
   public class CollectionUtils {
   
  @@ -1048,6 +1049,22 @@
           return UnmodifiableCollection.decorate(collection);
       }
   
  +	/**
  +	 * Returns an unmodifiable copy of the collection.
  +	 * <p>
  +     * This method uses the implementation in the decorators subpackage.
  +     * 
  +	 * @param collection the <code>Collection</code> to copy.
  +	 * @return an unmodifiable <code>Collection</code>.
  +	 * @throws IllegalArgumentException if collection is null
  +	 */
  +	public static Collection unmodifiableCollectionCopy(final Collection collection){
  +		if (collection == null) throw new IllegalArgumentException("null not permitted.");
  +		
  +		final Collection copy = new ArrayList(collection.size());
  +		copy.addAll(collection);
  +		return UnmodifiableCollection.decorate(copy);
  +	}
       /**
        * Returns a predicated (validating) collection backed by the given collection.
        * <p>
  @@ -1093,5 +1110,43 @@
       public static Collection transformedCollection(Collection collection, Transformer transformer)
{
           return TransformedCollection.decorate(collection, transformer);
       }
  -    
  +   
  +	/**
  +	 * Returns a collection containing all the elements in <code>collection</code>
  +	 * that are also in <code>retain</code>. The cardinality of an element <code>e</code>
  +	 * in the returned collection is the same as the cardinality of <code>e</code>
  +	 * in <code>collection</code> unless <code>retain</code> does
not contain <code>e</code>, in which
  +	 * case the cardinality is zero. This method is useful if you do not wish to modify
  +	 * the collection <code>c</code> and thus cannot call <code>c.retainAll(retain);</code>.
  +	 * 
  +	 * @param collection the collection whose contents are the target of the #retailAll operation
  +	 * @param retain the collection containing the elements to be retained in the returned
collection
  +	 * @return a <code>Collection</code> containing all the elements of <code>collection</code>
  +	 * that occur at least once in <code>retain</code>.
  +	 * @throws NullPointerException if either parameter is null
  +	 */
  +	public static Collection retainAll(final Collection collection, final Collection retain)
{
  +		return ListUtils.retainAll(collection, retain);
  +	}
  +
  +	/**
  +	 * Removes the elements in <code>remove</code> from <code>collection</code>.
That is, this
  +	 * method returns a collection containing all the elements in <code>c</code>
  +	 * that are not in <code>remove</code>. The cardinality of an element <code>e</code>
  +	 * in the returned collection is the same as the cardinality of <code>e</code>
  +	 * in <code>collection</code> unless <code>remove</code> contains
<code>e</code>, in which
  +	 * case the cardinality is zero. This method is useful if you do not wish to modify
  +	 * the collection <code>c</code> and thus cannot call <code>collection.removeAll(remove);</code>.
  +	 * 
  +	 * @param collection the collection from which items are removed (in the returned collection)
  +	 * @param remove the items to be removed from the returned <code>collection</code>
  +	 * @return a <code>Collection</code> containing all the elements of <code>collection</code>
except
  +	 * any elements that also occur in <code>remove</code>.
  +	 * @throws NullPointerException if either parameter is null
  +	 */
  +	public static Collection removeAll(final Collection collection, final Collection remove)
{
  +		return ListUtils.retainAll(collection, remove);
  +	}
  +	
  +
   }
  
  
  
  1.42      +31 -2     jakarta-commons/collections/src/test/org/apache/commons/collections/TestCollectionUtils.java
  
  Index: TestCollectionUtils.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/TestCollectionUtils.java,v
  retrieving revision 1.41
  retrieving revision 1.42
  diff -u -r1.41 -r1.42
  --- TestCollectionUtils.java	3 Aug 2004 18:20:41 -0000	1.41
  +++ TestCollectionUtils.java	11 Dec 2004 06:30:38 -0000	1.42
  @@ -17,6 +17,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;
  @@ -49,6 +50,7 @@
    * @author Stephen Colebourne
    * @author Phil Steitz
    * @author Steven Melzer
  + * @author Neil O'Toole
    * 
    * @version $Revision$ $Date$
    */
  @@ -1194,5 +1196,32 @@
               // expected
           }  
       }
  -
  +    
  +    public void testUnmodifiableCollectionCopy() {
  +		Collection collection = new ArrayList();
  +		collection.add("a");
  +    	Collection copy = CollectionUtils.unmodifiableCollectionCopy(collection);
  +
  +		assertTrue(copy instanceof Unmodifiable);
  +		assertTrue(CollectionUtils.isEqualCollection(collection, copy));
  +		collection.clear();
  +		assertTrue(copy.isEmpty() == false);
  +
  +		try
  +		{
  +			copy.clear();
  +			fail("should be unmodifiable.");
  +		}
  +		catch (UnsupportedOperationException uoe)
  +		{} // this is what we want
  +		
  +		try
  +		{
  +			copy = CollectionUtils.unmodifiableCollectionCopy(null);
  +			fail("should throw IllegalArgumentException");
  +		}
  +		catch(IllegalArgumentException iae)
  +		{}
  +	}
  +    
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message