FIM.Util
Class FilterIterator
java.lang.Object
|
+--FIM.Util.FilterIterator
- All Implemented Interfaces:
- Iterator
- public abstract class FilterIterator
- extends Object
- implements Iterator
An iterator of objects matching a certain criteria. It is based on another iterator from
which only elements will be returned, which match certain criteria. Subclasses must implement
the actual filtering function protected boolean matchesFilter(Object toTest)
. The
remove
method can only be called between next
and hasNext
(After hasNext
was called removal is no longer possible as the underlying iterator
is at another position already).
- Version:
- 1.0, 1.7.2000
- Author:
- Michael Sonntag
Constructor Summary |
protected |
FilterIterator()
Creates a new iterator for filtering another iterator. |
Method Summary |
boolean |
hasNext()
Returns true if the iteration has more elements. |
protected abstract boolean |
matchesFilter(Object toTest)
The function implementing the filtering function. |
Object |
next()
Returns the next matching element of this iterator. |
void |
remove()
Removes from the underlying collection the last element returned by the iterator. |
protected void |
setBaseIterator(Iterator fullIter)
Sets the iterator for filtering objects from another iterator.
|
Methods inherited from class java.lang.Object |
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
FilterIterator
protected FilterIterator()
- Creates a new iterator for filtering another iterator.
setBaseIterator
protected void setBaseIterator(Iterator fullIter)
- Sets the iterator for filtering objects from another iterator.
The filtering parameters of the subclass must be set when this function is
called. Calls to matchesFilter(Object toTest)
must be possible!
- Parameters:
fullIter
- the iterator of all elements which will be filtered- Throws:
IllegalArgumentException
- if the parameter is null
IllegalStateException
- if the base iterator is already set
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
- Returns:
true
if this iterator has more matching elements; false
otherwise- Throws:
IllegalStateException
- if the base iterator is not set
next
public Object next()
- Returns the next matching element of this iterator.
- Specified by:
next
in interface Iterator
- Returns:
- the next matching element of this iterator
- Throws:
NoSuchElementException
- if no more matching elements existIllegalStateException
- if the base iterator is not set
matchesFilter
protected abstract boolean matchesFilter(Object toTest)
- The function implementing the filtering function.
- Parameters:
totest
- the object to test if it matches the filter- Returns:
true
if the element should be returned; false
if this
element does not match the criteria and should be suppressed
remove
public void remove()
- Removes from the underlying collection the last element returned by the iterator. This
method can be called only once per call to next. The behavior is unspecified if the
underlying collection is modified while the iteration is in progress in any way
other than by calling this method.
If
hasNext
is called after next
, this will throw a
IllegalStateException
: We must move on at least one object to see if it
matches the criteria before we can tell the user whether at least one more matching
element exists. Therefore in the underlying iterator we are at a different object and
cannot anymore remove the last by us returned object.
- Specified by:
remove
in interface Iterator
- Throws:
IllegalStateException
- if the base iterator is not set or if the next
method has not yet been called, or the remove
method has already
been called after the last call to the next
method.
Submit a bug
Copyright 2001,2002 Michael Sonntag & Institute for Information Processing and Microprocessor Technology (FIM), Johannes-Kepler-University Linz, Altenbergerstr. 69, A-4040 Linz, Austria.