commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pj...@apache.org
Subject cvs commit: jakarta-commons/collections/src/test/org/apache/commons/collections TestCollectionUtils.java
Date Sat, 10 Aug 2002 00:36:35 GMT
pjack       2002/08/09 17:36:34

  Modified:    collections/src/java/org/apache/commons/collections
                        CollectionUtils.java
               collections/src/test/org/apache/commons/collections
                        TestCollectionUtils.java
  Log:
  PR:10168
  Submitted by:Maarten Coene (MacBelgium at toughguy dot net)
  
  Revision  Changes    Path
  1.9       +22 -21    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.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- CollectionUtils.java	12 Jun 2002 03:59:15 -0000	1.8
  +++ CollectionUtils.java	10 Aug 2002 00:36:34 -0000	1.9
  @@ -463,14 +463,7 @@
           else if(obj instanceof Map) {
               Map map = (Map)obj;
               Iterator iterator = map.keySet().iterator();
  -            while(iterator.hasNext()) {
  -                idx--;
  -                if(idx == -1) {
  -                    return iterator.next();
  -                } else {
  -                    iterator.next();
  -                }
  -            }
  +            return index(iterator, idx);
           } 
           else if(obj instanceof List) {
               return ((List)obj).get(idx);
  @@ -490,17 +483,25 @@
               }
           } 
           else if(obj instanceof Iterator) {
  -            Iterator iterator = (Iterator)obj;
  -            while(iterator.hasNext()) {
  -                idx--;
  -                if(idx == -1) {
  -                    return iterator.next();
  -                } else {
  -                    iterator.next();
  -                }
  -            }
  +            return index((Iterator)obj, idx);
  +        }
  +        else if(obj instanceof Collection) {
  +            Iterator iterator = ((Collection)obj).iterator();
  +            return index(iterator, idx);
           }
           return obj;
  +    }
  +
  +    private static Object index(Iterator iterator, int idx) {
  +        while(iterator.hasNext()) {
  +            idx--;
  +            if(idx == -1) {
  +                return iterator.next();
  +            } else {
  +                iterator.next();
  +            }
  +        }
  +        return iterator;
       }
   
       /** Returns an Iterator for the given object. Currently this method can handle
  
  
  
  1.3       +22 -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.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- TestCollectionUtils.java	14 Jul 2001 23:33:27 -0000	1.2
  +++ TestCollectionUtils.java	10 Aug 2002 00:36:34 -0000	1.3
  @@ -310,4 +310,22 @@
           assertTrue(CollectionUtils.isEqualCollection(a,b));
           assertTrue(CollectionUtils.isEqualCollection(b,a));
       }
  +
  +
  +    public void testIndex() {
  +        Map map = new HashMap();
  +        map.put(new Integer(0), "element");
  +        Object test = CollectionUtils.index(map, 0);
  +        assertTrue(test.equals("element"));
  +
  +        List list = new ArrayList();
  +        list.add("element");
  +        test = CollectionUtils.index(list, 0);
  +        assertTrue(test.equals("element"));
  +
  +        Bag bag = new HashBag();
  +        bag.add("element", 1);
  +        test = CollectionUtils.index(bag, 0);
  +        assertTrue(test.equals("element"));
  +    }
   }
  
  
  

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


Mime
View raw message