This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-collections.git
commit 37a660d2b40d3835831ab3c8c5381a3fa14af8d7 Author: Gary Gregory <garydgreg...@gmail.com> AuthorDate: Sat Oct 5 17:09:07 2024 -0400 Internal refactoring --- .../apache/commons/collections4/IteratorUtils.java | 40 ++++++++++++++++------ .../commons/collections4/IteratorUtilsTest.java | 15 ++++++-- 2 files changed, 41 insertions(+), 14 deletions(-) diff --git a/src/main/java/org/apache/commons/collections4/IteratorUtils.java b/src/main/java/org/apache/commons/collections4/IteratorUtils.java index 89b6ed69d..cbe052656 100644 --- a/src/main/java/org/apache/commons/collections4/IteratorUtils.java +++ b/src/main/java/org/apache/commons/collections4/IteratorUtils.java @@ -28,6 +28,7 @@ import java.util.List; import java.util.ListIterator; import java.util.Map; import java.util.Objects; +import java.util.function.IntFunction; import org.apache.commons.collections4.functors.EqualPredicate; import org.apache.commons.collections4.iterators.ArrayIterator; @@ -764,20 +765,38 @@ public class IteratorUtils { } /** - * Returns the {@code index}-th value in {@link Iterator}, throwing - * {@code IndexOutOfBoundsException} if there is no such element. + * Gets the {@code index}-th value in {@link Iterator}, throwing {@code IndexOutOfBoundsException} if there is no such element. * <p> - * The Iterator is advanced to {@code index} (or to the end, if - * {@code index} exceeds the number of entries) as a side effect of this method. + * The Iterator is advanced to {@code index} (or to the end, if {@code index} exceeds the number of entries) as a side effect of this method. + * </p> * - * @param <E> the type of object in the {@link Iterator} - * @param iterator the iterator to get a value from - * @param index the index to get - * @return the object at the specified index - * @throws IndexOutOfBoundsException if the index is invalid + * @param <E> the type of object in the {@link Iterator}. + * @param iterator the iterator to get a value from. + * @param index the index to get, 0-based. + * @return the object at the specified index. + * @throws IndexOutOfBoundsException if the index is invalid. * @since 4.1 */ public static <E> E get(final Iterator<E> iterator, final int index) { + return get(iterator, index, ioob -> { + throw new IndexOutOfBoundsException("Entry does not exist: " + ioob); + }); + } + + /** + * Gets the {@code index}-th value in {@link Iterator}, throwing {@code IndexOutOfBoundsException} if there is no such element. + * <p> + * The Iterator is advanced to {@code index} (or to the end, if {@code index} exceeds the number of entries) as a side effect of this method. + * </p> + * + * @param <E> the type of object in the {@link Iterator} + * @param iterator the iterator to get a value from + * @param index the index to get, 0-based. + * @param defaultSupplier supplies a default value at an index. + * @return the object at the specified index + * @throws IndexOutOfBoundsException if the index is invalid + */ + static <E> E get(final Iterator<E> iterator, final int index, final IntFunction<E> defaultSupplier) { int i = index; CollectionUtils.checkIndexBounds(i); while (iterator.hasNext()) { @@ -787,7 +806,7 @@ public class IteratorUtils { } iterator.next(); } - throw new IndexOutOfBoundsException("Entry does not exist: " + i); + return defaultSupplier.apply(i); } /** @@ -1417,7 +1436,6 @@ public class IteratorUtils { return new ZippingIterator<>(iterators); } - // Zipping /** * Returns an iterator that interleaves elements from the decorated iterators. * diff --git a/src/test/java/org/apache/commons/collections4/IteratorUtilsTest.java b/src/test/java/org/apache/commons/collections4/IteratorUtilsTest.java index ce05a4831..931234392 100644 --- a/src/test/java/org/apache/commons/collections4/IteratorUtilsTest.java +++ b/src/test/java/org/apache/commons/collections4/IteratorUtilsTest.java @@ -750,12 +750,21 @@ public class IteratorUtilsTest { assertEquals(1, (int) IteratorUtils.get(iterator, 0)); iterator = iterableA.iterator(); assertEquals(2, (int) IteratorUtils.get(iterator, 1)); - // Iterator, non-existent entry final Iterator<Integer> finalIterator = iterator; - assertThrows(IndexOutOfBoundsException.class, () -> IteratorUtils.get(finalIterator, 10), - "Expecting IndexOutOfBoundsException."); + assertThrows(IndexOutOfBoundsException.class, () -> IteratorUtils.get(finalIterator, 10), "Expecting IndexOutOfBoundsException."); + assertFalse(iterator.hasNext()); + } + @Test + public void testGetAtIndexFromIteratorDefault() throws Exception { + // Iterator, entry exists + Iterator<Integer> iterator = iterableA.iterator(); + assertEquals(1, (int) IteratorUtils.get(iterator, 0, i -> 0)); + iterator = iterableA.iterator(); + assertEquals(2, (int) IteratorUtils.get(iterator, 1, i -> 0)); + // Iterator, non-existent entry + assertEquals(111, (int) IteratorUtils.get(iterator, 10, i -> 111)); assertFalse(iterator.hasNext()); }