commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From t.@apache.org
Subject svn commit: r1647955 - in /commons/proper/collections/trunk/src: changes/changes.xml main/java/org/apache/commons/collections4/CollectionUtils.java test/java/org/apache/commons/collections4/CollectionUtilsTest.java
Date Fri, 26 Dec 2014 11:06:24 GMT
Author: tn
Date: Fri Dec 26 11:06:23 2014
New Revision: 1647955

URL: http://svn.apache.org/r1647955
Log:
[COLLECTIONS-540] Added CollectionUtils#get(Enumeration, int), simplified code of CollectionUtils#get(Object,
int). Thanks to Daniel Stewart, Issam El Atif. This closes #6.

Modified:
    commons/proper/collections/trunk/src/changes/changes.xml
    commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/CollectionUtils.java
    commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/CollectionUtilsTest.java

Modified: commons/proper/collections/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/changes/changes.xml?rev=1647955&r1=1647954&r2=1647955&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/changes/changes.xml (original)
+++ commons/proper/collections/trunk/src/changes/changes.xml Fri Dec 26 11:06:23 2014
@@ -22,6 +22,10 @@
   <body>
 
   <release version="4.1" date="TBD" description="">
+    <action issue="COLLECTIONS-540" dev="tn" type="fix" due-to="Daniel Stewart, Issam
El Atif">
+      Added overloaded method "CollectionUtils#get(Enumeration, int)" and simplified
+      code for "CollectionUtils#get(Object, int)".
+    </action>
     <action issue="COLLECTIONS-518" dev="tn" type="fix" due-to="Dipanjan Laha">
       The abstract decorator "AbstractIterableGetMapDecorator" was not declared
       abstract.

Modified: commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/CollectionUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/CollectionUtils.java?rev=1647955&r1=1647954&r2=1647955&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/CollectionUtils.java
(original)
+++ commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/CollectionUtils.java
Fri Dec 26 11:06:23 2014
@@ -1199,6 +1199,35 @@ public class CollectionUtils {
     }
 
     /**
+     * Returns the <code>index</code>-th value in the {@link Enumeration}, throwing
+     * <code>IndexOutOfBoundsException</code> if there is no such element.
+     * <p>
+     * The Enumeration is advanced to <code>index</code> (or to the end, if
+     * <code>index</code> exceeds the number of entries) as a side effect of
this method.
+     *
+     * @param e  the enumeration to get a value from
+     * @param index  the index to get
+     * @param <T> the type of object in the {@link Enumeration}
+     * @return the object at the specified index
+     * @throws IndexOutOfBoundsException if the index is invalid
+     * @throws IllegalArgumentException if the object type is invalid
+     * @since 4.1
+     */
+    public static <T> T get(final Enumeration<T> e, final int index) {
+        int i = index;
+        checkIndexBounds(i);
+        while (e.hasMoreElements()) {
+            i--;
+            if (i == -1) {
+                return e.nextElement();
+            } else {
+                e.nextElement();
+            }
+        }
+        throw new IndexOutOfBoundsException("Entry does not exist: " + i);
+    }
+
+    /**
      * Ensures an index is not negative.
      * @param index the index to check.
      * @throws IndexOutOfBoundsException if the index is negative.
@@ -1272,28 +1301,13 @@ public class CollectionUtils {
             return ((Object[]) object)[i];
         } else if (object instanceof Iterator<?>) {
             final Iterator<?> it = (Iterator<?>) object;
-            while (it.hasNext()) {
-                i--;
-                if (i == -1) {
-                    return it.next();
-                }
-                it.next();
-            }
-            throw new IndexOutOfBoundsException("Entry does not exist: " + i);
+            return get(it, i);
         } else if (object instanceof Collection<?>) {
             final Iterator<?> iterator = ((Collection<?>) object).iterator();
             return get(iterator, i);
         } else if (object instanceof Enumeration<?>) {
             final Enumeration<?> it = (Enumeration<?>) object;
-            while (it.hasMoreElements()) {
-                i--;
-                if (i == -1) {
-                    return it.nextElement();
-                } else {
-                    it.nextElement();
-                }
-            }
-            throw new IndexOutOfBoundsException("Entry does not exist: " + i);
+            return get(it, i);
         } else if (object == null) {
             throw new IllegalArgumentException("Unsupported object type: null");
         } else {

Modified: commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/CollectionUtilsTest.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/CollectionUtilsTest.java?rev=1647955&r1=1647954&r2=1647955&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/CollectionUtilsTest.java
(original)
+++ commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/CollectionUtilsTest.java
Fri Dec 26 11:06:23 2014
@@ -1600,8 +1600,26 @@ public class CollectionUtilsTest extends
         assertEquals(2, CollectionUtils.get((Object)collectionA, 2));
         assertEquals(2, CollectionUtils.get((Object)collectionA.iterator(), 2));
         final Map<Integer, Integer> map = CollectionUtils.getCardinalityMap(collectionA);
-        assertEquals(map.entrySet().iterator().next(), CollectionUtils.get(
-                (Object)map, 0));
+        assertEquals(map.entrySet().iterator().next(), CollectionUtils.get((Object)map, 0));
+    }
+
+    @Test
+    public void getIterator() {
+        final Iterator<Integer> it = collectionA.iterator();
+        assertEquals(Integer.valueOf(2), CollectionUtils.get(it, 2));
+        assertTrue(it.hasNext());
+        assertEquals(Integer.valueOf(4), CollectionUtils.get(it, 6));
+        assertFalse(it.hasNext());
+    }
+
+    @Test
+    public void getEnumeration() {
+        final Vector<Integer> vectorA = new Vector<Integer>(collectionA);
+        final Enumeration<Integer> e = vectorA.elements();
+        assertEquals(Integer.valueOf(2), CollectionUtils.get(e, 2));
+        assertTrue(e.hasMoreElements());
+        assertEquals(Integer.valueOf(4), CollectionUtils.get(e, 6));
+        assertFalse(e.hasMoreElements());
     }
 
     @Test



Mime
View raw message