I\'ve implemented a custom java.util.Iterator using a resource that should be released at the end using a close() method. That resource could
In your implementation you could close it your self, if when the iteratoror is exhausted.
public boolean hasNext() {
....
if( !hasNext ) {
this.close();
}
return hasNext;
}
And clearly document:
This iterator will invoke close() when hasNext() return false, if you need to dispose the iterator before make sure you call close your self
example:
void testIt() {
Iterator i = DbIterator.connect("db.config.info.here");
try {
while( i.hasNext() {
process( i.next() );
}
} finally {
if( i != null ) {
i.close();
}
}
}
Btw, you could while you're there you could implement Iterable and use the enhanced for loop.