blob: 7cd73a25d321d1042262539086f9159c450d0fae [file] [log] [blame]
Stuart McCullochf3173222012-06-07 21:57:32 +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 return n < dir.list().length;
20 }
21
22 public File next() {
23 if (next != null) {
24 File answer = next.next();
25 if (!next.hasNext())
26 next = null;
27 return answer;
28 }
29 File nxt = dir.listFiles()[n++];
30 if (nxt.isDirectory()) {
31 next = new FileIterator(nxt);
32 return nxt;
33 } else if (nxt.isFile()) {
34 return nxt;
35 } else
36 throw new IllegalStateException("File disappeared");
37 }
38
39 public void remove() {
40 throw new UnsupportedOperationException(
41 "Cannot remove from a file iterator");
42 }
43}