scolebourne    2003/04/04 14:22:29

  Modified:    collections/src/test/org/apache/commons/collections
                        TestListUtils.java TestSetUtils.java
                        TestMapUtils.java TestCollectionUtils.java
               collections/src/java/org/apache/commons/collections
                        SetUtils.java MapUtils.java ListUtils.java
                        CollectionUtils.java BagUtils.java
  Added:       collections/src/test/org/apache/commons/collections
                        TestTypedCollection.java
  Log:
  Add typed decorator collections to utility classes
  from Matthew Hawthorne
  
  Revision  Changes    Path
  1.7       +31 -3     
jakarta-commons/collections/src/test/org/apache/commons/collections/TestListUtils.java
  
  Index: TestListUtils.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/TestListUtils.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- TestListUtils.java        4 Apr 2003 20:42:02 -0000       1.6
  +++ TestListUtils.java        4 Apr 2003 22:22:28 -0000       1.7
  @@ -72,6 +72,7 @@
    * 
    * @author Stephen Colebourne
    * @author Neil O'Toole
  + * @author Matthew Hawthorne
    */
   public class TestListUtils extends BulkTest {
   
  @@ -98,6 +99,33 @@
                   return new TestList("") {
                       public List makeEmptyList() {
                           return (List)predicatedCollection();
  +                    }
  +
  +                    public Object[] getFullElements() {
  +                        return getFullNonNullStringElements();
  +                    }
  +
  +                    public Object[] getOtherElements() {
  +                        return getOtherNonNullStringElements();
  +                    }
  +
  +                };
  +            }
  +        };
  +    }
  +
  +    public BulkTest bulkTestTypedList() {
  +        return new TestTypedCollection("") {
  +
  +            public Collection typedCollection() {
  +                Class type = getType();
  +                return ListUtils.typedList(new ArrayList(), type);
  +            }
  +
  +            public BulkTest bulkTestAll() {
  +                return new TestList("") {
  +                    public List makeEmptyList() {
  +                        return (List)typedCollection();
                       }
   
                       public Object[] getFullElements() {
  
  
  
  1.5       +30 -3     
jakarta-commons/collections/src/test/org/apache/commons/collections/TestSetUtils.java
  
  Index: TestSetUtils.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/TestSetUtils.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- TestSetUtils.java 4 Apr 2003 20:42:03 -0000       1.4
  +++ TestSetUtils.java 4 Apr 2003 22:22:28 -0000       1.5
  @@ -73,6 +73,7 @@
    * 
    * @author Stephen Colebourne
    * @author Neil O'Toole
  + * @author Matthew Hawthorne
    */
   public class TestSetUtils extends BulkTest {
   
  @@ -114,6 +115,32 @@
           };
       }
   
  +    public BulkTest bulkTestTypedSet() {
  +        return new TestTypedCollection("") {
  +
  +            public Collection typedCollection() {
  +                Class type = getType();
  +                return SetUtils.typedSet(new HashSet(), type);
  +            }
  +
  +            public BulkTest bulkTestAll() {
  +                return new TestSet("") {
  +                    public Set makeEmptySet() {
  +                        return (Set)typedCollection();
  +                    }
  +
  +                    public Object[] getFullElements() {
  +                        return getFullNonNullStringElements();
  +                    }
  +
  +                    public Object[] getOtherElements() {
  +                        return getOtherNonNullStringElements();
  +                    }
  +                };
  +            }
  +        };
  +    }
  +     
       public void testEquals() {
           Collection data = Arrays.asList( new String[] { "a", "b", "c" });
           
  
  
  
  1.3       +64 -3     
jakarta-commons/collections/src/test/org/apache/commons/collections/TestMapUtils.java
  
  Index: TestMapUtils.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/TestMapUtils.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- TestMapUtils.java 12 Oct 2002 22:36:22 -0000      1.2
  +++ TestMapUtils.java 4 Apr 2003 22:22:28 -0000       1.3
  @@ -134,6 +134,51 @@
           }
       }
   
  +    // Since a typed map is a predicated map, I copied the tests for predicated map
  +    public void testTypedMapIllegalPut() {
  +        final Map map = MapUtils.typedMap(new HashMap(), String.class, 
String.class);
  +        
  +        try {
  +            map.put("Hi", new Integer(3));
  +            fail("Illegal value should raise IllegalArgument");
  +        } catch (IllegalArgumentException e) {
  +            // expected
  +        }
  +
  +        try {
  +            map.put(new Integer(3), "Hi");
  +            fail("Illegal key should raise IllegalArgument");
  +        } catch (IllegalArgumentException e) {
  +            // expected
  +        }
  +
  +        assertTrue(!map.containsKey(new Integer(3)));
  +        assertTrue(!map.containsValue(new Integer(3)));
  +
  +        Map map2 = new HashMap();
  +        map2.put("A", "a");
  +        map2.put("B", "b");
  +        map2.put("C", "c");
  +        map2.put("c", new Integer(3));
  +
  +        try {
  +            map.putAll(map2);
  +            fail("Illegal value should raise IllegalArgument");
  +        } catch (IllegalArgumentException e) {
  +            // expected
  +        }
  +
  +        map.put("E", "e");
  +        Iterator iterator = map.entrySet().iterator();
  +        try {
  +            Map.Entry entry = (Map.Entry)iterator.next();
  +            entry.setValue(new Integer(3));
  +            fail("Illegal value should raise IllegalArgument");
  +        } catch (IllegalArgumentException e) {
  +            // expected
  +        }
  +    
  +    }
   
       public BulkTest bulkTestPredicatedMap() {
           return new TestMap("") {
  @@ -148,6 +193,22 @@
               public Map makeEmptyMap() {
                   Predicate p = getPredicate();
                   return MapUtils.predicatedMap(new HashMap(), p, p);
  +            }
  +        };
  +    }
  +    
  +    public BulkTest bulkTestTypedMap() {
  +        return new TestMap("") {
  +            public boolean useNullKey() {
  +                return false;
  +            }
  +
  +            public boolean useNullValue() {
  +                return false;
  +            }
  +
  +            public Map makeEmptyMap() {
  +                return MapUtils.typedMap(new HashMap(), String.class, String.class);
               }
           };
       }
  
  
  
  1.15      +46 -4     
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.14
  retrieving revision 1.15
  diff -u -r1.14 -r1.15
  --- TestCollectionUtils.java  19 Feb 2003 20:33:11 -0000      1.14
  +++ TestCollectionUtils.java  4 Apr 2003 22:22:28 -0000       1.15
  @@ -75,7 +75,12 @@
   import junit.framework.TestSuite;
   
   /**
  + * Tests for CollectionUtils.
  + * 
    * @author Rodney Waldhoff
  + * @author Matthew Hawthorne
  + * @author Stephen Colebourne
  + * 
    * @version $Revision$ $Date$
    */
   public class TestCollectionUtils extends TestCase {
  @@ -590,6 +595,43 @@
           };
       }
   
  +    public BulkTest bulkTestTypedCollection() {
  +        return new TestTypedCollection("") {
  +            public Collection typedCollection() {
  +                return CollectionUtils.typedCollection(
  +                    new ArrayList(),
  +                    super.getType());
  +            }
  + 
  +            public BulkTest bulkTestAll() {
  +                return new TestCollection("") {
  +                    public Collection makeCollection() {
  +                        return typedCollection();
  +                    }
  + 
  +                    public Collection makeConfirmedCollection() {
  +                        return new ArrayList();
  +                    }
  + 
  +                    public Collection makeConfirmedFullCollection() {
  +                        ArrayList list = new ArrayList();
  +                        list.addAll(java.util.Arrays.asList(getFullElements()));
  +                        return list;
  +                    }
  + 
  +                    public Object[] getFullElements() {
  +                        return getFullNonNullStringElements();
  +                    }
  + 
  +                    public Object[] getOtherElements() {
  +                        return getOtherNonNullStringElements();
  +                    }
  + 
  +                };
  +            }
  +        };
  +    }
  +    
       public void testIsFull() {
           Set set = new HashSet();
           set.add("1");
  
  
  
  1.1                  
jakarta-commons/collections/src/test/org/apache/commons/collections/TestTypedCollection.java
  
  Index: TestTypedCollection.java
  ===================================================================
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2003 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and
   *    "Apache Turbine" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact [EMAIL PROTECTED]
   *
   * 5. Products derived from this software may not be called "Apache",
   *    "Apache Turbine", nor may "Apache" appear in their name, without
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  package org.apache.commons.collections;
  
  import java.util.ArrayList;
  import java.util.Collection;
  import java.util.List;
  
  /**
   * 
   * @author Stephen Colebourne
   */
  public abstract class TestTypedCollection extends BulkTest {
  
      public TestTypedCollection(String name) {
          super(name);
      }
  
  
      protected abstract Collection typedCollection();
  
      protected Class getType() {
          return String.class;
      }
  
  
      public void testIllegalAdd() {
          Collection c = typedCollection();
          Integer i = new Integer(3);
          try {
              c.add(i);
              fail("Integer should fail string predicate.");
          } catch (IllegalArgumentException e) {
              // expected
          }
          assertTrue("Collection shouldn't contain illegal element", 
           !c.contains(i));   
      }
  
  
      public void testIllegalAddAll() {
          Collection c = typedCollection();
          List elements = new ArrayList();
          elements.add("one");
          elements.add("two");
          elements.add(new Integer(3));
          elements.add("four");
          try {
              c.addAll(elements);
              fail("Integer should fail string predicate.");
          } catch (IllegalArgumentException e) {
              // expected
          }
          assertTrue("Collection shouldn't contain illegal element", 
           !c.contains("one"));   
          assertTrue("Collection shouldn't contain illegal element", 
           !c.contains("two"));   
          assertTrue("Collection shouldn't contain illegal element", 
           !c.contains(new Integer(3)));   
          assertTrue("Collection shouldn't contain illegal element", 
           !c.contains("four"));   
      }
  
  }
  
  
  
  1.11      +30 -3     
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.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- SetUtils.java     4 Apr 2003 20:40:28 -0000       1.10
  +++ SetUtils.java     4 Apr 2003 22:22:29 -0000       1.11
  @@ -57,7 +57,6 @@
    */
   package org.apache.commons.collections;
   
  -import java.util.Set;
   import java.util.Collections;
   import java.util.Comparator;
   import java.util.Iterator;
  @@ -75,6 +74,7 @@
    * @author Paul Jack
    * @author Stephen Colebourne
    * @author Neil O'Toole
  + * @author Matthew Hawthorne
    */
   public class SetUtils {
   
  @@ -277,6 +277,20 @@
       }
   
       /**
  +     * Returns a typed set backed by the given set.
  +     * <p>
  +     * Only objects of the specified type can be added to the set.
  +     * 
  +     * @param set  the set to limit to a specific type, must not be null
  +     * @param type  the type of objects which may be added to the set
  +     * @return a typed set backed by the specified set
  +     */
  +    public static Set typedSet(Set set, Class type) {
  +        return predicatedSet(set, new CollectionUtils.InstanceofPredicate(type));
  +    }
  +    
  +    //-----------------------------------------------------------------------
  +    /**
        * Returns a synchronized sorted set backed by the given sorted set.
        * <p>
        * You must manually synchronize on the returned buffer's iterator to 
  @@ -331,4 +345,17 @@
           return new PredicatedSortedSet(set, predicate);
       }
   
  +    /**
  +     * Returns a typed sorted set backed by the given set.
  +     * <p>
  +     * Only objects of the specified type can be added to the set.
  +     * 
  +     * @param set  the set to limit to a specific type, must not be null
  +     * @param type  the type of objects which may be added to the set
  +     * @return a typed set backed by the specified set
  +     */
  +    public static SortedSet typedSortedSet(SortedSet set, Class type) {
  +        return predicatedSortedSet(set, new 
CollectionUtils.InstanceofPredicate(type));
  +    }
  +    
   }
  
  
  
  1.17      +52 -13    
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.16
  retrieving revision 1.17
  diff -u -r1.16 -r1.17
  --- MapUtils.java     19 Feb 2003 20:14:25 -0000      1.16
  +++ MapUtils.java     4 Apr 2003 22:22:29 -0000       1.17
  @@ -61,6 +61,7 @@
   package org.apache.commons.collections;
   
   import java.io.PrintStream;
  +import java.io.Serializable;
   import java.text.NumberFormat;
   import java.text.ParseException;
   import java.util.Collections;
  @@ -80,14 +81,16 @@
    *
    * It also provides the following decorators:
    *
  - *  <UL>
  - *  <LI>[EMAIL PROTECTED] #fixedSizeMap(Map)}
  - *  <LI>[EMAIL PROTECTED] #fixedSizeSortedMap(SortedMap)}
  - *  <LI>[EMAIL PROTECTED] #lazyMap(Map,Factory)}
  - *  <LI>[EMAIL PROTECTED] #lazySortedMap(SortedMap,Factory)}
  - *  <LI>[EMAIL PROTECTED] #predicatedMap(Map,Predicate,Predicate)}
  - *  <LI>[EMAIL PROTECTED] #predicatedSortedMap(SortedMap,Predicate,Predicate)}
  - *  </UL>
  + *  <ul>
  + *  <li>[EMAIL PROTECTED] #fixedSizeMap(Map)}
  + *  <li>[EMAIL PROTECTED] #fixedSizeSortedMap(SortedMap)}
  + *  <li>[EMAIL PROTECTED] #lazyMap(Map,Factory)}
  + *  <li>[EMAIL PROTECTED] #typedMap(Map, Class, Class)}
  + *  <li>[EMAIL PROTECTED] #lazySortedMap(SortedMap,Factory)}
  + *  <li>[EMAIL PROTECTED] #predicatedMap(Map,Predicate,Predicate)}
  + *  <li>[EMAIL PROTECTED] #predicatedSortedMap(SortedMap,Predicate,Predicate)}
  + *  <li>[EMAIL PROTECTED] #typedSortedMap(Map, Class, Class)}
  + *  </ul>
    *
    * @since 1.0
    * @author <a href="mailto:[EMAIL PROTECTED]">James Strachan</a>
  @@ -95,6 +98,7 @@
    * @author <a href="mailto:[EMAIL PROTECTED]">Kasper Nielsen</a>
    * @author Paul Jack
    * @author Stephen Colebourne
  + * @author Matthew Hawthorne
    */
   public class MapUtils {
       
  @@ -750,7 +754,7 @@
           }
       }
   
  -
  +    //-----------------------------------------------------------------------
       static class PredicatedMap 
               extends ProxyMap {
   
  @@ -1078,7 +1082,7 @@
   
       }
   
  -
  +    //-----------------------------------------------------------------------
       /**
        * Returns a synchronized map backed by the given map.
        * <p>
  @@ -1136,6 +1140,23 @@
       }
   
       /**
  +     * Returns a typed map backed by the given map.
  +     * <p>
  +     * Only keys and values of the specified types can be added to the map.
  +     * 
  +     * @param map  the map to limit to a specific type, must not be null
  +     * @param keyType  the type of keys which may be added to the map
  +     * @param valueType  the type of values which may be added to the map
  +     * @return a typed map backed by the specified map
  +     */
  +    public static Map typedMap(Map map, Class keyType, Class valueType) {
  +        return predicatedMap(
  +            map, 
  +            new CollectionUtils.InstanceofPredicate(keyType),
  +            new CollectionUtils.InstanceofPredicate(valueType));
  +    }
  +    
  +    /**
        * Returns a fixed-sized map backed by the given map.
        * Elements may not be added or removed from the returned map, but 
        * existing elements can be changed (for instance, via the 
  @@ -1182,6 +1203,7 @@
           return new LazyMap(map, factory);
       }
   
  +    //-----------------------------------------------------------------------
       /**
        * Returns a synchronized sorted map backed by the given sorted map.
        * <p>
  @@ -1238,6 +1260,23 @@
           return new PredicatedSortedMap(map, keyPred, valuePred);
       }
   
  +    /**
  +     * Returns a typed sorted map backed by the given map.
  +     * <p>
  +     * Only keys and values of the specified types can be added to the map.
  +     * 
  +     * @param map  the map to limit to a specific type, must not be null
  +     * @param keyType  the type of keys which may be added to the map
  +     * @param valueType  the type of values which may be added to the map
  +     * @return a typed map backed by the specified map
  +     */
  +    public static SortedMap typedSortedMap(SortedMap map, Class keyType, Class 
valueType) {
  +        return predicatedSortedMap(
  +            map, 
  +            new CollectionUtils.InstanceofPredicate(keyType),
  +            new CollectionUtils.InstanceofPredicate(valueType));
  +    }
  +    
       /**
        * Returns a fixed-sized sorted map backed by the given sorted map.
        * Elements may not be added or removed from the returned map, but 
  
  
  
  1.15      +16 -2     
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.14
  retrieving revision 1.15
  diff -u -r1.14 -r1.15
  --- ListUtils.java    4 Apr 2003 20:40:28 -0000       1.14
  +++ ListUtils.java    4 Apr 2003 22:22:29 -0000       1.15
  @@ -76,6 +76,7 @@
    * @author Paul Jack
    * @author Stephen Colebourne
    * @author Neil O'Toole
  + * @author Matthew Hawthorne
    */
   public class ListUtils {
   
  @@ -596,6 +597,19 @@
           return new PredicatedList(list, predicate);
       }
   
  +    /**
  +     * Returns a typed list backed by the given list.
  +     * <p>
  +     * Only objects of the specified type can be added to the list.
  +     * 
  +     * @param list  the list to limit to a specific type, must not be null
  +     * @param type  the type of objects which may be added to the list
  +     * @return a typed list backed by the specified list
  +     */
  +    public static List typedList(List list, Class type) {
  +        return predicatedList(list, new CollectionUtils.InstanceofPredicate(type));
  +    }
  +    
       /**
        * Returns a "lazy" list whose elements will be created on demand.<P>
        * <p>
  
  
  
  1.29      +55 -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.28
  retrieving revision 1.29
  diff -u -r1.28 -r1.29
  --- CollectionUtils.java      25 Jan 2003 11:40:26 -0000      1.28
  +++ CollectionUtils.java      4 Apr 2003 22:22:29 -0000       1.29
  @@ -57,6 +57,7 @@
    */
   package org.apache.commons.collections;
   
  +import java.io.Serializable;
   import java.util.ArrayList;
   import java.util.Collection;
   import java.util.Collections;
  @@ -85,6 +86,7 @@
    * @author Steve Downey
    * @author <a href="[EMAIL PROTECTED]">Herve Quiroz</a>
    * @author Peter KoBek
  + * @author Matthew Hawthorne
    */
   public class CollectionUtils {
   
  @@ -894,6 +896,7 @@
           return -1;
       }
   
  +    //-----------------------------------------------------------------------
       /**
        * Base class for collection decorators.  I decided to do it this way
        * because it seemed to result in the most reuse.  
  @@ -997,7 +1000,9 @@
   
       }
   
  -
  +    /**
  +     * Implementation of a collection that checks entries.
  +     */
       static class PredicatedCollection 
               extends CollectionWrapper {
   
  @@ -1034,7 +1039,9 @@
   
       }
   
  -
  +    /**
  +     * Implementation of a collection that is unmodifiable.
  +     */
       static class UnmodifiableCollection 
               extends CollectionWrapper {
   
  @@ -1072,7 +1079,9 @@
   
       }
   
  -
  +    /**
  +     * Implementation of a collection that is synchronized.
  +     */
       static class SynchronizedCollection {
   
           protected final Collection collection;
  @@ -1150,7 +1159,35 @@
   
       }
   
  +    /**
  +     * <code>Predicate</code> implementation that checks the type of an object.
  +     * This class may eventually be replaced by 
  +     * 
<code>org.apache.commons.lang.functor.PredicateUtils.instanceofPredicate()</code>.
  +     */
  +    static class InstanceofPredicate implements Predicate, Serializable {
  +        private final Class type;
   
  +        /**
  +         * Constructor
  +         */
  +        public InstanceofPredicate(Class type) {
  +            if (type == null) {
  +                throw new IllegalArgumentException("Type must not be null");
  +            }
  +            this.type = type;
  +        }
  +
  +        /**
  +         * Return true if the object is an instanceof the type of the predicate.
  +         * @param object an <code>Object</code>
  +         * @return <code>true</code> if the object is an instanceof the type of the 
predicate
  +         */
  +        public boolean evaluate(Object object) {
  +            return type.isInstance(object);
  +        }
  +    }
  +
  +    //-----------------------------------------------------------------------
       /**
        * Returns a synchronized collection backed by the given collection.
        * <p>
  @@ -1206,4 +1243,17 @@
           return new PredicatedCollection(collection, predicate);
       }
   
  +    /**
  +     * Returns a typed collection backed by the given collection.
  +     * <p>
  +     * Only objects of the specified type can be added to the collection.
  +     * 
  +     * @param collection  the collection to limit to a specific type, must not be 
null
  +     * @param type  the type of objects which may be added to the collection
  +     * @return a typed collection backed by the specified collection
  +     */
  +    public static Collection typedCollection(Collection collection, Class type) {
  +        return predicatedCollection(collection, new InstanceofPredicate(type));
  +    }
  +    
   }
  
  
  
  1.9       +78 -49    
jakarta-commons/collections/src/java/org/apache/commons/collections/BagUtils.java
  
  Index: BagUtils.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/BagUtils.java,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- BagUtils.java     20 Feb 2003 23:14:03 -0000      1.8
  +++ BagUtils.java     4 Apr 2003 22:22:29 -0000       1.9
  @@ -70,6 +70,7 @@
    * @author Paul Jack
    * @author Stephen Colebourne
    * @author Andrew Freeman
  + * @author Matthew Hawthorne
    */
   public class BagUtils {
   
  @@ -80,6 +81,7 @@
       public BagUtils() {
       }
   
  +    //-----------------------------------------------------------------------
       /**
        * Implementation of a Bag that validates elements before they are added.
        */
  @@ -266,35 +268,7 @@
           }
       }
   
  -
  -    /**
  -     * Returns a predicated bag backed by the given bag.  Only objects
  -     * that pass the test in the given predicate can be added to the bag.
  -     * It is important not to use the original bag after invoking this 
  -     * method, as it is a backdoor for adding unvalidated objects.
  -     *
  -     * @param bag  the bag to predicate, must not be null
  -     * @param predicate  the predicate for the bag, must not be null
  -     * @return a predicated bag backed by the given bag
  -     * @throws IllegalArgumentException  if the Bag or Predicate is null
  -     */
  -    public static Bag predicatedBag(Bag bag, Predicate predicate) {
  -        return new PredicatedBag(bag, predicate);
  -    }
  -
  -    /**
  -     * Returns an unmodifiable view of the given bag.  Any modification
  -     * attempts to the returned bag will raise an 
  -     * [EMAIL PROTECTED] UnsupportedOperationException}.
  -     *
  -     * @param bag  the bag whose unmodifiable view is to be returned, must not be 
null
  -     * @return an unmodifiable view of that bag
  -     * @throws IllegalArgumentException  if the Bag is null
  -     */
  -    public static Bag unmodifiableBag(Bag bag) {
  -        return new UnmodifiableBag(bag);
  -    }
  -
  +    //-----------------------------------------------------------------------
       /**
        * Returns a synchronized (thread-safe) bag backed by the given bag.
        * In order to guarantee serial access, it is critical that all 
  @@ -326,34 +300,47 @@
       }
   
       /**
  -     * Returns a predicated sorted bag backed by the given sorted bag.  
  -     * Only objects that pass the test in the given predicate can be 
  -     * added to the bag.
  +     * Returns an unmodifiable view of the given bag.  Any modification
  +     * attempts to the returned bag will raise an 
  +     * [EMAIL PROTECTED] UnsupportedOperationException}.
  +     *
  +     * @param bag  the bag whose unmodifiable view is to be returned, must not be 
null
  +     * @return an unmodifiable view of that bag
  +     * @throws IllegalArgumentException  if the Bag is null
  +     */
  +    public static Bag unmodifiableBag(Bag bag) {
  +        return new UnmodifiableBag(bag);
  +    }
  +    
  +    /**
  +     * Returns a predicated bag backed by the given bag.  Only objects
  +     * that pass the test in the given predicate can be added to the bag.
        * It is important not to use the original bag after invoking this 
        * method, as it is a backdoor for adding unvalidated objects.
        *
  -     * @param bag  the sorted bag to predicate, must not be null
  +     * @param bag  the bag to predicate, must not be null
        * @param predicate  the predicate for the bag, must not be null
        * @return a predicated bag backed by the given bag
  -     * @throws IllegalArgumentException  if the SortedBag or Predicate is null
  +     * @throws IllegalArgumentException  if the Bag or Predicate is null
        */
  -    public static SortedBag predicatedSortedBag(SortedBag bag, Predicate predicate) 
{
  -        return new PredicatedSortedBag(bag, predicate);
  +    public static Bag predicatedBag(Bag bag, Predicate predicate) {
  +        return new PredicatedBag(bag, predicate);
       }
   
       /**
  -     * Returns an unmodifiable view of the given sorted bag.  Any modification
  -     * attempts to the returned bag will raise an 
  -     * [EMAIL PROTECTED] UnsupportedOperationException}.
  -     *
  -     * @param bag  the bag whose unmodifiable view is to be returned, must not be 
null
  -     * @return an unmodifiable view of that bag
  -     * @throws IllegalArgumentException  if the SortedBag is null
  +     * Returns a typed bag backed by the given bag.
  +     * <p>
  +     * Only objects of the specified type can be added to the bag.
  +     * 
  +     * @param bag  the bag to limit to a specific type, must not be null
  +     * @param type  the type of objects which may be added to the bag
  +     * @return a typed bag backed by the specified bag
        */
  -    public static SortedBag unmodifiableSortedBag(SortedBag bag) {
  -        return new UnmodifiableSortedBag(bag);
  +    public static Bag typedBag(Bag bag, Class type) {
  +        return predicatedBag(bag, new CollectionUtils.InstanceofPredicate(type));
       }
  -
  +    
  +    //-----------------------------------------------------------------------
       /**
        * Returns a synchronized (thread-safe) sorted bag backed by the given 
        * sorted bag.
  @@ -384,5 +371,47 @@
       public static SortedBag synchronizedSortedBag(SortedBag bag) {
           return new SynchronizedSortedBag(bag);
       }
  -
  +    
  +    /**
  +     * Returns an unmodifiable view of the given sorted bag.  Any modification
  +     * attempts to the returned bag will raise an 
  +     * [EMAIL PROTECTED] UnsupportedOperationException}.
  +     *
  +     * @param bag  the bag whose unmodifiable view is to be returned, must not be 
null
  +     * @return an unmodifiable view of that bag
  +     * @throws IllegalArgumentException  if the SortedBag is null
  +     */
  +    public static SortedBag unmodifiableSortedBag(SortedBag bag) {
  +        return new UnmodifiableSortedBag(bag);
  +    }
  +    
  +    /**
  +     * Returns a predicated sorted bag backed by the given sorted bag.  
  +     * Only objects that pass the test in the given predicate can be 
  +     * added to the bag.
  +     * It is important not to use the original bag after invoking this 
  +     * method, as it is a backdoor for adding unvalidated objects.
  +     *
  +     * @param bag  the sorted bag to predicate, must not be null
  +     * @param predicate  the predicate for the bag, must not be null
  +     * @return a predicated bag backed by the given bag
  +     * @throws IllegalArgumentException  if the SortedBag or Predicate is null
  +     */
  +    public static SortedBag predicatedSortedBag(SortedBag bag, Predicate predicate) 
{
  +        return new PredicatedSortedBag(bag, predicate);
  +    }
  +    
  +    /**
  +     * Returns a typed sorted bag backed by the given bag.
  +     * <p>
  +     * Only objects of the specified type can be added to the bag.
  +     * 
  +     * @param bag  the bag to limit to a specific type, must not be null
  +     * @param type  the type of objects which may be added to the bag
  +     * @return a typed bag backed by the specified bag
  +     */
  +    public static SortedBag typedSortedBag(SortedBag bag, Class type) {
  +        return predicatedSortedBag(bag, new 
CollectionUtils.InstanceofPredicate(type));
  +    }
  +        
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to