Repository: commons-collections Updated Branches: refs/heads/master db136b93d -> b5b45d326
http://git-wip-us.apache.org/repos/asf/commons-collections/blob/b5b45d32/src/main/java/org/apache/commons/collections4/IteratorUtils.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/collections4/IteratorUtils.java b/src/main/java/org/apache/commons/collections4/IteratorUtils.java index e4b30d3..86a2c09 100644 --- a/src/main/java/org/apache/commons/collections4/IteratorUtils.java +++ b/src/main/java/org/apache/commons/collections4/IteratorUtils.java @@ -1413,6 +1413,23 @@ public class IteratorUtils { } /** + * Returns the <code>first</code> value in {@link Iterator}, throwing + * <code>IndexOutOfBoundsException</code> if there is no such element. + * <p> + * The Iterator 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 type of object in the {@link Iterator} + * @param iterator the iterator to get a value from + * @return the first object + * @throws IndexOutOfBoundsException if the request is invalid + * @since 4.2 + */ + public static <E> E first(final Iterator<E> iterator) { + return get(iterator, 0); + } + + /** * Returns the number of elements contained in the given iterator. * <p> * A <code>null</code> or empty iterator returns {@code 0}. http://git-wip-us.apache.org/repos/asf/commons-collections/blob/b5b45d32/src/test/java/org/apache/commons/collections4/IteratorUtilsTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/collections4/IteratorUtilsTest.java b/src/test/java/org/apache/commons/collections4/IteratorUtilsTest.java index 7210388..5f966b9 100644 --- a/src/test/java/org/apache/commons/collections4/IteratorUtilsTest.java +++ b/src/test/java/org/apache/commons/collections4/IteratorUtilsTest.java @@ -1005,7 +1005,7 @@ public class IteratorUtilsTest { // ----------------------------------------------------------------------- @Test - public void forEach() { + public void testForEach() { final List<Integer> listA = new ArrayList<>(); listA.add(1); @@ -1033,7 +1033,7 @@ public class IteratorUtilsTest { } @Test - public void forEachButLast() { + public void testForEachButLast() { final List<Integer> listA = new ArrayList<>(); listA.add(1); @@ -1065,7 +1065,7 @@ public class IteratorUtilsTest { } @Test - public void find() { + public void testFind() { Predicate<Number> testPredicate = equalPredicate((Number) 4); Integer test = IteratorUtils.find(iterableA.iterator(), testPredicate); assertTrue(test.equals(4)); @@ -1082,7 +1082,7 @@ public class IteratorUtilsTest { } @Test - public void indexOf() { + public void testIndexOf() { Predicate<Number> testPredicate = equalPredicate((Number) 4); int index = IteratorUtils.indexOf(iterableA.iterator(), testPredicate); assertEquals(6, index); @@ -1099,7 +1099,7 @@ public class IteratorUtilsTest { } @Test - public void getFromIterator() throws Exception { + public void testGetAtIndexFromIterator() throws Exception { // Iterator, entry exists Iterator<Integer> iterator = iterableA.iterator(); assertEquals(1, (int) IteratorUtils.get(iterator, 0)); @@ -1117,6 +1117,13 @@ public class IteratorUtilsTest { } @Test + public void testFirstFromIterator() throws Exception { + // Iterator, entry exists + Iterator<Integer> iterator = iterableA.iterator(); + assertEquals(1, (int) IteratorUtils.first(iterator)); + } + + @Test public void testGetIterator() { final Object[] objArray = {"a", "b", "c"}; final Map<String, String> inMap = new HashMap<>();