diff --git a/RELEASE-NOTES.html b/RELEASE-NOTES.html
index 8464895b5..6f87cd7f2 100644
--- a/RELEASE-NOTES.html
+++ b/RELEASE-NOTES.html
@@ -104,6 +104,7 @@ If this causes major headaches to anyone please contact commons-dev at jakarta.a
UnboundedFifoBuffer - Fix deserialization to work with subsequant object manipulation [35763]
BlockingBuffer - Fix internal locking code (internal fix, no effect on users of BlockingBuffer) [37028]
IteratorChain.remove() - Fix to avoid IllegalStateException when one of the underlying iterators is a FilterIterator [34267]
+FilterIterator - Correctly handle setting of iterator and predicate after object creation [38074]
ExtendedProperties.convertProperties() - Fix to handle default properties maps correctly [32204]
Add casts to avoid some JDK1.5 compilation warnings [35474]
Make serialization version ids private [37106]
diff --git a/project.xml b/project.xml
index 9af3431e9..7077af42a 100644
--- a/project.xml
+++ b/project.xml
@@ -340,6 +340,9 @@
Daniel Rall
+
+ Huw Roberts
+
Henning P. Schmiedehausen
diff --git a/src/java/org/apache/commons/collections/iterators/FilterIterator.java b/src/java/org/apache/commons/collections/iterators/FilterIterator.java
index 70f87547f..92f16d100 100644
--- a/src/java/org/apache/commons/collections/iterators/FilterIterator.java
+++ b/src/java/org/apache/commons/collections/iterators/FilterIterator.java
@@ -148,6 +148,8 @@ public class FilterIterator implements Iterator {
*/
public void setIterator(Iterator iterator) {
this.iterator = iterator;
+ nextObject = null;
+ nextObjectSet = false;
}
//-----------------------------------------------------------------------
@@ -167,6 +169,8 @@ public class FilterIterator implements Iterator {
*/
public void setPredicate(Predicate predicate) {
this.predicate = predicate;
+ nextObject = null;
+ nextObjectSet = false;
}
//-----------------------------------------------------------------------
diff --git a/src/test/org/apache/commons/collections/iterators/TestFilterIterator.java b/src/test/org/apache/commons/collections/iterators/TestFilterIterator.java
index dadc7c22a..f5fef8882 100644
--- a/src/test/org/apache/commons/collections/iterators/TestFilterIterator.java
+++ b/src/test/org/apache/commons/collections/iterators/TestFilterIterator.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2001-2004 The Apache Software Foundation
+ * Copyright 2001-2004,2006 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@ package org.apache.commons.collections.iterators;
import java.util.ArrayList;
import java.util.Arrays;
+import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
@@ -25,14 +26,17 @@ import junit.framework.Test;
import junit.framework.TestSuite;
import org.apache.commons.collections.Predicate;
+import org.apache.commons.collections.functors.NotNullPredicate;
+import org.apache.commons.collections.functors.TruePredicate;
/**
* Test the filter iterator.
*
* @version $Revision$ $Date$
*
- * @author Jan Sorensen
+ * @author Jan Sorensen
* @author Ralph Wagner
+ * @author Huw Roberts
*/
public class TestFilterIterator extends AbstractTestIterator {
@@ -111,6 +115,41 @@ public class TestFilterIterator extends AbstractTestIterator {
verifyElementsInPredicate(new String[] { "a", "b", "c" });
}
+ /**
+ * Test that when the iterator is changed, the hasNext method returns the
+ * correct response for the new iterator.
+ */
+ public void testSetIterator() {
+ Iterator iter1 = Collections.singleton(new Object()).iterator();
+ Iterator iter2 = Collections.EMPTY_LIST.iterator();
+
+ FilterIterator filterIterator = new FilterIterator(iter1);
+ filterIterator.setPredicate(TruePredicate.getInstance());
+ // this iterator has elements
+ assertEquals(true, filterIterator.hasNext());
+
+ // this iterator has no elements
+ filterIterator.setIterator(iter2);
+ assertEquals(false, filterIterator.hasNext());
+ }
+
+ /**
+ * Test that when the predicate is changed, the hasNext method returns the
+ * correct response for the new predicate.
+ */
+ public void testSetPredicate() {
+ Iterator iter = Collections.singleton(null).iterator();
+
+ FilterIterator filterIterator = new FilterIterator(iter);
+ filterIterator.setPredicate(TruePredicate.getInstance());
+ // this predicate matches
+ assertEquals(true, filterIterator.hasNext());
+
+ // this predicate doesn't match
+ filterIterator.setPredicate(NotNullPredicate.getInstance());
+ assertEquals(false, filterIterator.hasNext());
+ }
+
private void verifyNoMoreElements() {
assertTrue(!iterator.hasNext());
try {