Fix set iterator and predicate methods

bug 38074, from Huw Roberts

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/trunk@366576 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stephen Colebourne 2006-01-06 22:07:07 +00:00
parent 8ee4f391d0
commit ad2987c5ad
4 changed files with 49 additions and 2 deletions

View File

@ -104,6 +104,7 @@ If this causes major headaches to anyone please contact commons-dev at jakarta.a
<li>UnboundedFifoBuffer - Fix deserialization to work with subsequant object manipulation [35763]</li>
<li>BlockingBuffer - Fix internal locking code (internal fix, no effect on users of BlockingBuffer) [37028]</li>
<li>IteratorChain.remove() - Fix to avoid IllegalStateException when one of the underlying iterators is a FilterIterator [34267]</li>
<li>FilterIterator - Correctly handle setting of iterator and predicate after object creation [38074]</li>
<li>ExtendedProperties.convertProperties() - Fix to handle default properties maps correctly [32204]</li>
<li>Add casts to avoid some JDK1.5 compilation warnings [35474]</li>
<li>Make serialization version ids private [37106]</li>

View File

@ -340,6 +340,9 @@
<contributor>
<name>Daniel Rall</name>
</contributor>
<contributor>
<name>Huw Roberts</name>
</contributor>
<contributor>
<name>Henning P. Schmiedehausen</name>
</contributor>

View File

@ -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;
}
//-----------------------------------------------------------------------

View File

@ -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 {