Return-Path: X-Original-To: apmail-commons-commits-archive@minotaur.apache.org Delivered-To: apmail-commons-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 749401019E for ; Fri, 26 Dec 2014 11:06:24 +0000 (UTC) Received: (qmail 76219 invoked by uid 500); 26 Dec 2014 11:06:24 -0000 Delivered-To: apmail-commons-commits-archive@commons.apache.org Received: (qmail 76143 invoked by uid 500); 26 Dec 2014 11:06:24 -0000 Mailing-List: contact commits-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@commons.apache.org Delivered-To: mailing list commits@commons.apache.org Received: (qmail 76132 invoked by uid 99); 26 Dec 2014 11:06:24 -0000 Received: from eris.apache.org (HELO hades.apache.org) (140.211.11.105) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 26 Dec 2014 11:06:24 +0000 Received: from hades.apache.org (localhost [127.0.0.1]) by hades.apache.org (ASF Mail Server at hades.apache.org) with ESMTP id 49596AC0907; Fri, 26 Dec 2014 11:06:24 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit 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 -0000 To: commits@commons.apache.org From: tn@apache.org X-Mailer: svnmailer-1.0.9 Message-Id: <20141226110624.49596AC0907@hades.apache.org> 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 @@ + + Added overloaded method "CollectionUtils#get(Enumeration, int)" and simplified + code for "CollectionUtils#get(Object, int)". + 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 index-th value in the {@link Enumeration}, throwing + * IndexOutOfBoundsException if there is no such element. + *

+ * The Enumeration is advanced to index (or to the end, if + * index 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 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 get(final Enumeration 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 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 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 vectorA = new Vector(collectionA); + final Enumeration 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