commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From scolebou...@apache.org
Subject cvs commit: jakarta-commons/collections/src/java/org/apache/commons/collections CollectionUtils.java
Date Wed, 31 Mar 2004 21:43:27 GMT
scolebourne    2004/03/31 13:43:27

  Modified:    collections RELEASE-NOTES.html project.xml
               collections/src/test/org/apache/commons/collections
                        TestCollectionUtils.java
               collections/src/java/org/apache/commons/collections
                        CollectionUtils.java
  Log:
  Add size(Object) method to find the size of various collection-like objects
  bug 27909, from Steven Melzer
  
  Revision  Changes    Path
  1.18      +1 -0      jakarta-commons/collections/RELEASE-NOTES.html
  
  Index: RELEASE-NOTES.html
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/collections/RELEASE-NOTES.html,v
  retrieving revision 1.17
  retrieving revision 1.18
  diff -u -r1.17 -r1.18
  --- RELEASE-NOTES.html	17 Mar 2004 21:14:10 -0000	1.17
  +++ RELEASE-NOTES.html	31 Mar 2004 21:43:27 -0000	1.18
  @@ -34,6 +34,7 @@
   <li>AbstractHashedMap,AbstractLinkedMap - Add methods to access entry methods when
protected scope blocks</li>
   <li>Functors - Add get methods to retrieve internal state [27515]</li>
   <li>MultiHashMap - Add five methods to improve the API</li>
  +<li>CollectionUtils - Add size(Object) method to find the size of various collection-like
objects [27909]</li>
   </ul>
   
   <center><h3>BUG FIXES</h3></center>
  
  
  
  1.35      +3 -0      jakarta-commons/collections/project.xml
  
  Index: project.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/collections/project.xml,v
  retrieving revision 1.34
  retrieving revision 1.35
  diff -u -r1.34 -r1.35
  --- project.xml	13 Mar 2004 16:34:46 -0000	1.34
  +++ project.xml	31 Mar 2004 21:43:27 -0000	1.35
  @@ -175,6 +175,9 @@
         <name>Brian McCallister</name>
       </contributor>
       <contributor>
  +      <name>Steven Melzer</name>
  +    </contributor>
  +    <contributor>
         <name>Leon Messerschmidt</name>
       </contributor>
       <contributor>
  
  
  
  1.36      +61 -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.35
  retrieving revision 1.36
  diff -u -r1.35 -r1.36
  --- TestCollectionUtils.java	18 Feb 2004 01:20:35 -0000	1.35
  +++ TestCollectionUtils.java	31 Mar 2004 21:43:27 -0000	1.36
  @@ -46,6 +46,7 @@
    * @author Matthew Hawthorne
    * @author Stephen Colebourne
    * @author Phil Steitz
  + * @author Steven Melzer
    * 
    * @version $Revision$ $Date$
    */
  @@ -686,7 +687,65 @@
           }
       }
   
  -
  +    public void testSize_List() {
  +        List list = new ArrayList();
  +        assertEquals(0, CollectionUtils.size(list));
  +        list.add("a");
  +        assertEquals(1, CollectionUtils.size(list));
  +        list.add("b");
  +        assertEquals(2, CollectionUtils.size(list));
  +    }
  +    public void testSize_Map() {
  +        Map map = new HashMap();
  +        assertEquals(0, CollectionUtils.size(map));
  +        map.put("1", "a");
  +        assertEquals(1, CollectionUtils.size(map));
  +        map.put("2", "b");
  +        assertEquals(2, CollectionUtils.size(map));
  +    }
  +    public void testSize_Array() {
  +        Object[] objectArray = new Object[0];
  +        assertEquals(0, CollectionUtils.size(objectArray));
  +        
  +        String[] stringArray = new String[3];
  +        assertEquals(3, CollectionUtils.size(stringArray));
  +        stringArray[0] = "a";
  +        stringArray[1] = "b";
  +        stringArray[2] = "c";
  +        assertEquals(3, CollectionUtils.size(stringArray));
  +    }
  +    public void testSize_Enumeration() {
  +        Vector list = new Vector();
  +        assertEquals(0, CollectionUtils.size(list.elements()));
  +        list.add("a");
  +        assertEquals(1, CollectionUtils.size(list.elements()));
  +        list.add("b");
  +        assertEquals(2, CollectionUtils.size(list.elements()));
  +    }
  +    public void testSize_Iterator() {
  +        List list = new ArrayList();
  +        assertEquals(0, CollectionUtils.size(list.iterator()));
  +        list.add("a");
  +        assertEquals(1, CollectionUtils.size(list.iterator()));
  +        list.add("b");
  +        assertEquals(2, CollectionUtils.size(list.iterator()));
  +    }
  +    public void testSize_Other() {
  +        try {
  +            CollectionUtils.size(null);
  +            fail("Expecting IllegalArgumentException");
  +        } catch (IllegalArgumentException e) {}
  +        try {
  +            CollectionUtils.size("not a list");
  +            fail("Expecting IllegalArgumentException");
  +        } catch (IllegalArgumentException e) {}
  +        try {
  +            CollectionUtils.size(new int[0]);
  +            fail("Expecting IllegalArgumentException");
  +        } catch (IllegalArgumentException e) {}
  +    }
  +    
  +    //-----------------------------------------------------------------------
       private static Predicate EQUALS_TWO = new Predicate() {
           public boolean evaluate(Object input) {
               return (input.equals("Two"));
  
  
  
  1.57      +76 -39    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.56
  retrieving revision 1.57
  diff -u -r1.56 -r1.57
  --- CollectionUtils.java	18 Feb 2004 01:15:42 -0000	1.56
  +++ CollectionUtils.java	31 Mar 2004 21:43:27 -0000	1.57
  @@ -48,6 +48,7 @@
    * @author Matthew Hawthorne
    * @author Janek Bogucki
    * @author Phil Steitz
  + * @author Steven Melzer
    */
   public class CollectionUtils {
   
  @@ -739,13 +740,13 @@
               return ((Object[])obj)[idx];
           } 
           else if(obj instanceof Enumeration) {
  -            Enumeration enum = (Enumeration)obj;
  -            while(enum.hasMoreElements()) {
  +            Enumeration it = (Enumeration)obj;
  +            while(it.hasMoreElements()) {
                   idx--;
                   if(idx == -1) {
  -                    return enum.nextElement();
  +                    return it.nextElement();
                   } else {
  -                    enum.nextElement();
  +                    it.nextElement();
                   }
               }
           } 
  @@ -803,55 +804,91 @@
        */
       public static Object get(Object object, int index) {
           if (index < 0) {
  -            throw new IndexOutOfBoundsException("Index cannot be negative.");
  +            throw new IndexOutOfBoundsException("Index cannot be negative: " + index);
           }
  -        if(object instanceof Map) {
  -            Map map = (Map)object;
  +        if (object instanceof Map) {
  +            Map map = (Map) object;
               Iterator iterator = map.entrySet().iterator();
               return get(iterator, index);
  -        } 
  -        else if(object instanceof List) {
  -            return ((List)object).get(index);
  -        } 
  -        else if(object instanceof Object[]) {
  -            return ((Object[])object)[index];
  -        } 
  -        else if(object instanceof Enumeration) {
  -            Enumeration enum = (Enumeration)object;
  -            while(enum.hasMoreElements()) {
  +        } else if (object instanceof List) {
  +            return ((List) object).get(index);
  +        } else if (object instanceof Object[]) {
  +            return ((Object[]) object)[index];
  +        } else if (object instanceof Enumeration) {
  +            Enumeration it = (Enumeration) object;
  +            while (it.hasMoreElements()) {
                   index--;
  -                if(index == -1) {
  -                    return enum.nextElement();
  +                if (index == -1) {
  +                    return it.nextElement();
                   } else {
  -                    enum.nextElement();
  +                    it.nextElement();
                   }
               }
  -            throw new IndexOutOfBoundsException("Entry does not exist.");
  -        } 
  -        else if(object instanceof Iterator) {
  -            return get((Iterator)object, index);
  -        }
  -        else if(object instanceof Collection) {
  -            Iterator iterator = ((Collection)object).iterator();
  +            throw new IndexOutOfBoundsException("Entry does not exist: " + index);
  +        } else if (object instanceof Iterator) {
  +            Iterator it = (Iterator) object;
  +            while (it.hasNext()) {
  +                index--;
  +                if (index == -1) {
  +                    return it.next();
  +                } else {
  +                    it.next();
  +                }
  +            }
  +            throw new IndexOutOfBoundsException("Entry does not exist: " + index);
  +        } else if (object instanceof Collection) {
  +            Iterator iterator = ((Collection) object).iterator();
               return get(iterator, index);
           } else {
  -            throw new IllegalArgumentException("Unsupported object type.");
  +            throw new IllegalArgumentException("Unsupported object type: " +
  +                (object == null ? "null" : object.getClass().getName()));
           }
       }
       
  -    private static Object get(Iterator iterator, int index) {
  -        while(iterator.hasNext()) {
  -            index--;
  -            if(index == -1) {
  -                return iterator.next();
  -            } else {
  -                iterator.next();
  +    /** 
  +     * Gets the size of the collection/iterator specified.
  +     * <p>
  +     * This method can handles objects as follows
  +     * <ul>
  +     * <li>Collection - the collection size
  +     * <li>Map - the map size
  +     * <li>Object array - the array size
  +     * <li>Iterator - the number of elements remaining in the iterator
  +     * <li>Enumeration - the number of elements remaining in the enumeration
  +     * </ul>
  +     * 
  +     * @param object  the object to get the size of
  +     * @return the size of the specified collection
  +     * @throws IllegalArgumentException thrown if object is not recognised or null
  +     */
  +    public static int size(Object object) {
  +        int total = 0;
  +        if (object instanceof Map) {
  +            total = ((Map) object).size();
  +        } else if (object instanceof Collection) {
  +            total = ((Collection) object).size();
  +        } else if (object instanceof Object[]) {
  +            total = ((Object[]) object).length;
  +        } else if (object instanceof Iterator) {
  +            Iterator it = (Iterator) object;
  +            while (it.hasNext()) {
  +                total++;
  +                it.next();
  +            }
  +        } else if (object instanceof Enumeration) {
  +            Enumeration it = (Enumeration) object;
  +            while (it.hasMoreElements()) {
  +                total++;
  +                it.nextElement();
               }
  +        } else {
  +            throw new IllegalArgumentException("Unsupported object type: " +
  +                (object == null ? "null" : object.getClass().getName()));
           }
  -        throw new IndexOutOfBoundsException("Entry does not exist.");
  +        return total;
       }
  -
  -    /** 
  +    
  +    /**
        * Reverses the order of the given array.
        * 
        * @param array  the array to reverse
  
  
  

---------------------------------------------------------------------
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