mirror of
https://github.com/apache/commons-collections.git
synced 2025-02-26 04:35:47 +00:00
Patch that stops next() continually returning null if hasNext is not
called in the standard way. ie) (cf: "Juozas Baliuka" <baliuka@mwm.lt>) while(true) { filterIterator.next(); } This should eventually throw NoSuchElementException, but it returns null forever. Submitted by: Jan Sorensen <jan.sorensen@aragost.com> git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/trunk@130505 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
d2d3f62e25
commit
21500f60e7
@ -1,72 +1,103 @@
|
||||
/*
|
||||
* Copyright (C) The Apache Software Foundation. All rights reserved.
|
||||
*
|
||||
* This software is published under the terms of the Apache Software License
|
||||
* version 1.1, a copy of which has been included with this distribution in
|
||||
* the LICENSE file.
|
||||
*/
|
||||
package org.apache.commons.collections;
|
||||
|
||||
import java.util.Enumeration;
|
||||
import java.util.Iterator;
|
||||
|
||||
/** A Proxy {@link Iterator Iterator} which takes a {@link Predicate Predicate} instance to filter
|
||||
* out objects from an underlying {@link Iterator Iterator} instance.
|
||||
*
|
||||
* @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
|
||||
*/
|
||||
|
||||
public class FilterIterator extends ProxyIterator {
|
||||
|
||||
/** Holds value of property predicate. */
|
||||
private Predicate predicate;
|
||||
private Object nextObject;
|
||||
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
public FilterIterator() {
|
||||
}
|
||||
|
||||
public FilterIterator( Iterator iterator ) {
|
||||
super( iterator );
|
||||
}
|
||||
|
||||
public FilterIterator( Iterator iterator, Predicate predicate ) {
|
||||
super( iterator );
|
||||
this.predicate = predicate;
|
||||
}
|
||||
|
||||
// Iterator interface
|
||||
//-------------------------------------------------------------------------
|
||||
public boolean hasNext() {
|
||||
Iterator iterator = getIterator();
|
||||
Predicate predicate = getPredicate();
|
||||
while ( iterator.hasNext() ) {
|
||||
Object object = iterator.next();
|
||||
if ( predicate.evaluate( object ) ) {
|
||||
nextObject = object;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public Object next() {
|
||||
return nextObject;
|
||||
}
|
||||
|
||||
// Properties
|
||||
//-------------------------------------------------------------------------
|
||||
/** Getter for property predicate.
|
||||
* @return Value of property predicate.
|
||||
*/
|
||||
public Predicate getPredicate() {
|
||||
return predicate;
|
||||
}
|
||||
/** Setter for property predicate.
|
||||
* @param predicate New value of property predicate.
|
||||
*/
|
||||
public void setPredicate(Predicate predicate) {
|
||||
this.predicate = predicate;
|
||||
}
|
||||
}
|
||||
/*
|
||||
* Copyright (C) The Apache Software Foundation. All rights reserved.
|
||||
*
|
||||
* This software is published under the terms of the Apache Software License
|
||||
* version 1.1, a copy of which has been included with this distribution in
|
||||
* the LICENSE file.
|
||||
*/
|
||||
package org.apache.commons.collections;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
/** A Proxy {@link Iterator Iterator} which takes a {@link Predicate Predicate} instance to filter
|
||||
* out objects from an underlying {@link Iterator Iterator} instance.
|
||||
*
|
||||
* @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
|
||||
* @author Jan Sorensen
|
||||
*/
|
||||
|
||||
public class FilterIterator extends ProxyIterator {
|
||||
|
||||
/** Holds value of property predicate. */
|
||||
private Predicate predicate;
|
||||
|
||||
private Object nextObject;
|
||||
private boolean nextObjectSet = false;
|
||||
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
public FilterIterator() {
|
||||
}
|
||||
|
||||
public FilterIterator( Iterator iterator ) {
|
||||
super( iterator );
|
||||
}
|
||||
|
||||
public FilterIterator( Iterator iterator, Predicate predicate ) {
|
||||
super( iterator );
|
||||
this.predicate = predicate;
|
||||
}
|
||||
|
||||
// Iterator interface
|
||||
//-------------------------------------------------------------------------
|
||||
public boolean hasNext() {
|
||||
if ( nextObjectSet ) {
|
||||
return true;
|
||||
} else {
|
||||
return setNextObject();
|
||||
}
|
||||
}
|
||||
|
||||
public Object next() {
|
||||
if ( !nextObjectSet ) {
|
||||
if (!setNextObject()) {
|
||||
throw new NoSuchElementException();
|
||||
}
|
||||
}
|
||||
nextObjectSet = false;
|
||||
return nextObject;
|
||||
}
|
||||
|
||||
/**
|
||||
* Always throws UnsupportedOperationException as this class
|
||||
* does look-ahead with its internal iterator.
|
||||
*/
|
||||
public void remove() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
|
||||
// Properties
|
||||
//-------------------------------------------------------------------------
|
||||
/** Getter for property predicate.
|
||||
* @return Value of property predicate.
|
||||
*/
|
||||
public Predicate getPredicate() {
|
||||
return predicate;
|
||||
}
|
||||
/** Setter for property predicate.
|
||||
* @param predicate New value of property predicate.
|
||||
*/
|
||||
public void setPredicate(Predicate predicate) {
|
||||
this.predicate = predicate;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set nextObject to the next object. If there are no more
|
||||
* objects then return false. Otherwise, return true.
|
||||
*/
|
||||
private boolean setNextObject() {
|
||||
Iterator iterator = getIterator();
|
||||
Predicate predicate = getPredicate();
|
||||
while ( iterator.hasNext() ) {
|
||||
Object object = iterator.next();
|
||||
if ( predicate.evaluate( object ) ) {
|
||||
nextObject = object;
|
||||
nextObjectSet = true;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user