|
From: Thijs J. <ito...@th...> - 2004-04-20 10:20:37
|
Dear all,
Erik and I discoverd a bug in the FilterIterator. It wasn't possible to
iterate over a collection wit null elements. We've added a test to
FilterIteratorTest to demonstrate the problem:
public void testIteratorWithNullValues(){
Collection coll = Arrays.asList(new Object[]{null,null,null,"bla"});
Iterator it = new FilterIterator(coll.iterator(), new AlwaysTruePredicate());
assertTrue(it.hasNext());
assertNull(it.next());
assertNull(it.next());
assertNull(it.next());
assertEquals("bla",it.next());
assertFalse(it.hasNext());
}
We solved the problem in the class IteratorAsStack which couldn't handle null
elements. We added a new attribute topElementSet and changed the method
isEmpty() and pop();
public class IteratorAsStack implements Stack {
private Iterator delegate;
private Object topElement;
private boolean topElementSet=false;
public IteratorAsStack(Iterator iterator) {
this.delegate = iterator;
}
public Object peek() {
if (isEmpty())
throw new NoSuchElementException();
return topElement;
}
public Object pop() {
Object result = peek();
topElementSet = false;
return result;
}
public void push(Object object) {
throw new UnsupportedOperationException();
}
public boolean isEmpty() {
if (!topElementSet && delegate.hasNext()) {
topElement = delegate.next();
topElementSet = true;
}
return !topElementSet;
}
}
The changes are committed to the cq2-itor branch.
Best regards,
Thijs
--
Thijs Janssen
th...@cq...
ito...@th...
Seek You Too B.V.
http://www.cq2.nl
|