create changelog entry
[debian/openrocket] / core / src / net / sf / openrocket / file / iterator / FileIterator.java
1 package net.sf.openrocket.file.iterator;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.util.Iterator;
6 import java.util.NoSuchElementException;
7
8 import net.sf.openrocket.logging.LogHelper;
9 import net.sf.openrocket.startup.Application;
10 import net.sf.openrocket.util.Pair;
11
12 /**
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.
16  * <p>
17  * Concrete implementations must implement the method {@link #findNext()} and possibly
18  * {@link #close()}.
19  * 
20  * @author Sampo Niskanen <sampo.niskanen@iki.fi>
21  */
22 public abstract class FileIterator implements Iterator<Pair<String, InputStream>> {
23         private static final LogHelper logger = Application.getLogger();
24         
25         private Pair<String, InputStream> next = null;
26         private int fileCount = 0;
27         
28         @Override
29         public boolean hasNext() {
30                 if (next != null)
31                         return true;
32                 
33                 next = findNext();
34                 return (next != null);
35         }
36         
37         
38         @Override
39         public Pair<String, InputStream> next() {
40                 if (next == null) {
41                         next = findNext();
42                 }
43                 if (next == null) {
44                         throw new NoSuchElementException("No more files");
45                 }
46                 
47                 Pair<String, InputStream> n = next;
48                 next = null;
49                 fileCount++;
50                 return n;
51         }
52         
53         
54         @Override
55         public void remove() {
56                 throw new UnsupportedOperationException("remove() not supported");
57         }
58         
59         
60
61         /**
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.
65          */
66         public void close() {
67                 if (next != null) {
68                         try {
69                                 next.getV().close();
70                         } catch (IOException e) {
71                                 logger.error("Error closing file " + next.getU());
72                         }
73                         next = null;
74                 }
75         }
76         
77         
78         /**
79          * Return the number of files that have so far been returned by this iterator.
80          * 
81          * @return      the number of files that this iterator has returned so far.
82          */
83         public int getFileCount() {
84                 return fileCount;
85         }
86         
87         /**
88          * Return the next pair of file name and InputStream.
89          * 
90          * @return      a pair with the file name and input stream reading the file.
91          */
92         protected abstract Pair<String, InputStream> findNext();
93         
94 }