com.twelvemonkeys.util
Class FilterIterator<E>

java.lang.Object
  extended by com.twelvemonkeys.util.FilterIterator<E>
All Implemented Interfaces:
Iterator<E>

public class FilterIterator<E>
extends Object
implements Iterator<E>

Wraps (decorates) an Iterator with extra functionality, to allow element filtering. Each element is filtered against the given Filter, and only elements that are accepted are returned by the next method.

The optional remove operation is implemented, but may throw UnsupportedOperationException if the underlying iterator does not support the remove operation.

Version:
$Id: //depot/branches/personal/haraldk/twelvemonkeys/release-2/twelvemonkeys-core/src/main/java/com/twelvemonkeys/util/FilterIterator.java#1 $
Author:
Harald Kuhr, last modified by $Author: haku $
See Also:
FilterIterator.Filter

Nested Class Summary
static interface FilterIterator.Filter<E>
          Used to tests whether or not an element fulfills certain criteria, and hence should be accepted by the FilterIterator instance.
 
Field Summary
protected  FilterIterator.Filter<E> filter
           
protected  Iterator<E> iterator
           
 
Constructor Summary
FilterIterator(Iterator<E> pIterator, FilterIterator.Filter<E> pFilter)
          Creates a FilterIterator that wraps the Iterator.
 
Method Summary
 boolean hasNext()
          Returns true if the iteration has more elements.
 E next()
          Returns the next element in the iteration.
 void remove()
          Removes from the underlying collection the last element returned by the iterator (optional operation).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

filter

protected final FilterIterator.Filter<E> filter

iterator

protected final Iterator<E> iterator
Constructor Detail

FilterIterator

public FilterIterator(Iterator<E> pIterator,
                      FilterIterator.Filter<E> pFilter)
Creates a FilterIterator that wraps the Iterator. Each element is filtered against the given Filter, and only elements that are accepted are returned by the next method.

Parameters:
pIterator - the iterator to filter
pFilter - the filter
See Also:
FilterIterator.Filter
Method Detail

hasNext

public boolean hasNext()
Returns true if the iteration has more elements. (In other words, returns true if next would return an element rather than throwing an exception.)

Specified by:
hasNext in interface Iterator<E>
Returns:
true if the iterator has more elements.
See Also:
FilterIterator.Filter.accept(E)

next

public E next()
Returns the next element in the iteration.

Specified by:
next in interface Iterator<E>
Returns:
the next element in the iteration.
See Also:
FilterIterator.Filter.accept(E)

remove

public void remove()
Removes from the underlying collection the last element returned by the iterator (optional operation). This method can be called only once per call to next. The behavior of an iterator is unspecified if the underlying collection is modified while the iteration is in progress in any way other than by calling this method.

Specified by:
remove in interface Iterator<E>


Copyright © 2015. All Rights Reserved.