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
Just define your own sub-interface of Iterator that includes a close method, and make sure you use that instead of the regular Iterator class. For example, create this interface:
import java.io.Closeable;
import java.util.Iterator;
public interface CloseableIterator extends Iterator, Closeable {}
And then an implementation might look like this:
List someList = Arrays.asList( "what","ever" );
final Iterator delegate = someList.iterator();
return new CloseableIterator() {
public void close() throws IOException {
//Do something special here, where you have easy
//access to the vars that created the iterator
}
public boolean hasNext() {
return delegate.hasNext();
}
public String next() {
return delegate.next();
}
public void remove() {
delegate.remove();
}
};