blob: c60ed6b1a0b1f9830e242f047aaeaa38320e82c6 [file] [log] [blame]
Stuart McCulloch26e7a5a2011-10-17 10:31:43 +00001package aQute.libg.fileiterator;
2
3import java.io.*;
4import java.util.*;
5
6public class FileIterator implements Iterator<File> {
7 File dir;
8 int n = 0;
9 FileIterator next;
10
11 public FileIterator(File nxt) {
12 assert nxt.isDirectory();
13 this.dir = nxt;
14 }
15
16 public boolean hasNext() {
17 if (next != null)
18 return next.hasNext();
19 else
20 return n < dir.list().length;
21 }
22
23 public File next() {
24 if (next != null) {
25 File answer = next.next();
26 if (!next.hasNext())
27 next = null;
28 return answer;
29 } else {
30 File nxt = dir.listFiles()[n++];
31 if (nxt.isDirectory()) {
32 next = new FileIterator(nxt);
33 return nxt;
34 } else if (nxt.isFile()) {
35 return nxt;
36 } else
37 throw new IllegalStateException("File disappeared");
38 }
39 }
40
41 public void remove() {
42 throw new UnsupportedOperationException(
43 "Cannot remove from a file iterator");
44 }
45}