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


The following commit(s) were added to refs/heads/master by this push:
     new 2a0f86a94 Add FilterIterator.removeNext() #564
     new 332276998 Merge branch 'master' of 
https://gitbox.apache.org/repos/asf/commons-collections.git
2a0f86a94 is described below

commit 2a0f86a94d1a6808d12f04d6bf2d5ef8257a33ab
Author: Gary Gregory <garydgreg...@gmail.com>
AuthorDate: Fri Nov 1 16:11:38 2024 -0400

    Add FilterIterator.removeNext() #564
---
 src/changes/changes.xml                                 |  1 +
 .../commons/collections4/iterators/FilterIterator.java  | 12 ++++++++++++
 .../collections4/iterators/FilterIteratorTest.java      | 17 ++++++++++++++++-
 3 files changed, 29 insertions(+), 1 deletion(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 2843c6265..81c2f430e 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -59,6 +59,7 @@
     <action type="add" issue="COLLECTIONS-869" dev="ggregory" due-to="Gary 
Gregory">Add 
org.apache.commons.collections4.IteratorUtils.chainedIterator(Iterator&lt;? 
extends Iterator&lt;? extends E&gt;&gt;).</action>
     <action type="add" dev="ggregory" due-to="Peter De Maeyer" 
issue="COLLECTIONS-533">Add ArrayListValuedLinkedHashMap #560.</action>
     <action type="fix" dev="ggregory" due-to="Gary Gregory">Add missing test 
AbstractIteratorTest.testForEachRemaining().</action> 
+    <action type="fix" dev="ggregory" due-to="Gary Gregory, Claude Warren">Add 
FilterIterator.removeNext() #564.</action> 
     <!-- UPDATE -->
     <action type="update" dev="ggregory" due-to="Gary Gregory">Bump 
org.apache.commons:commons-parent from 71 to 78 #534, #545, #550 #555, 
#566.</action>
     <action type="update" issue="COLLECTIONS-857" dev="ggregory" 
due-to="Claude Warren">Update bloom filter documentation #508.</action>
diff --git 
a/src/main/java/org/apache/commons/collections4/iterators/FilterIterator.java 
b/src/main/java/org/apache/commons/collections4/iterators/FilterIterator.java
index 98047fe66..e9872a2b1 100644
--- 
a/src/main/java/org/apache/commons/collections4/iterators/FilterIterator.java
+++ 
b/src/main/java/org/apache/commons/collections4/iterators/FilterIterator.java
@@ -141,6 +141,18 @@ public class FilterIterator<E> implements Iterator<E> {
         iterator.remove();
     }
 
+    /**
+     * Returns the next item and removes it from the iterator.
+     *
+     * @return the next item from the iterator.
+     * @since 4.5.0-M3
+     */
+    public E removeNext() {
+        final E result = next();
+        remove();
+        return result;
+    }
+
     private Predicate<? super E> safePredicate(final Predicate<? super E> 
predicate) {
         return predicate != null ? predicate : TruePredicate.truePredicate();
     }
diff --git 
a/src/test/java/org/apache/commons/collections4/iterators/FilterIteratorTest.java
 
b/src/test/java/org/apache/commons/collections4/iterators/FilterIteratorTest.java
index f8ef27c6f..582e79a4d 100644
--- 
a/src/test/java/org/apache/commons/collections4/iterators/FilterIteratorTest.java
+++ 
b/src/test/java/org/apache/commons/collections4/iterators/FilterIteratorTest.java
@@ -172,6 +172,22 @@ public class FilterIteratorTest<E> extends 
AbstractIteratorTest<E> {
         assertTrue(actual.isEmpty());
     }
 
+    @Test
+    public void testRemoveNext() {
+        final FilterIterator<E> iter = makeObject();
+        final E i = iter.removeNext();
+        assertFalse(list.contains(i));
+        final List<E> actual = new ArrayList<>();
+        iter.forEachRemaining(actual::add);
+        assertEquals(list, actual);
+    }
+
+    @Test
+    public void testRemoveNextEmpty() {
+        final FilterIterator<E> empty = makeEmptyIterator();
+        assertThrows(NoSuchElementException.class, empty::removeNext);
+    }
+
     @Test
     public void testRepeatedHasNext() {
         for (int i = 0; i <= array.length; i++) {
@@ -188,7 +204,6 @@ public class FilterIteratorTest<E> extends 
AbstractIteratorTest<E> {
         verifyNoMoreElements();
     }
 
-
     @Test
     public void testReturnValues() {
         verifyElementsInPredicate(ArrayUtils.EMPTY_STRING_ARRAY);

Reply via email to