1 package net.sf.openrocket.file.iterator;
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.util.Iterator;
6 import java.util.NoSuchElementException;
8 import net.sf.openrocket.logging.LogHelper;
9 import net.sf.openrocket.startup.Application;
10 import net.sf.openrocket.util.Pair;
13 * An abstract class for iterating over files fulfilling some condition. The files are
14 * returned as pairs of open InputStreams and file names. Conditions can be for example
15 * files in a directory matching a specific FileFilter.
17 * Concrete implementations must implement the method {@link #findNext()} and possibly
20 * @author Sampo Niskanen <sampo.niskanen@iki.fi>
22 public abstract class FileIterator implements Iterator<Pair<String, InputStream>> {
23 private static final LogHelper logger = Application.getLogger();
25 private Pair<String, InputStream> next = null;
26 private int fileCount = 0;
29 public boolean hasNext() {
34 return (next != null);
39 public Pair<String, InputStream> next() {
44 throw new NoSuchElementException("No more files");
47 Pair<String, InputStream> n = next;
55 public void remove() {
56 throw new UnsupportedOperationException("remove() not supported");
62 * Closes the resources related to this iterator. This method should be
63 * overridden if the iterator needs to close any resources of its own, but
64 * must call this method as well.
70 } catch (IOException e) {
71 logger.error("Error closing file " + next.getU());
79 * Return the number of files that have so far been returned by this iterator.
81 * @return the number of files that this iterator has returned so far.
83 public int getFileCount() {
88 * Return the next pair of file name and InputStream.
90 * @return a pair with the file name and input stream reading the file.
92 protected abstract Pair<String, InputStream> findNext();