package archiver.util;
import java.io.file;
import java.util.arraylist;
import java.util.arrays;
import java.util.iterator;
import java.util.list;
public class fileiterator implements iterator, iterable {
private iterator iterator;
private list paths;
private list dirs;
public fileiterator(list paths) {
this.paths = new arraylist(paths);
this.dirs = new arraylist();
this.iterator = this.paths.iterator();
}
public file next() {
file file = null;
if (hasnext()) {
file = new file(iterator.next());
if (file.isdirectory()) {
dirs.add(file);
}
}
return file;
}
public boolean hasnext() {
if (!iterator.hasnext()) {
paths.clear();
for (file dir : dirs) {
for (string name : dir.list()) {
paths.add(dir.getpath()+file.separator+name);
}
}
dirs.clear();
if (paths.size() == 0) {
return false;
}
iterator = paths.iterator();
}
return true;
}
@override
public void remove() {
throw new unsupportedoperationexception();
}
@override
public iterator iterator() {
return this;
}
public static void main(string[] args) {
fileiterator iter = new fileiterator(arrays.aslist("."));
for (file file : iter) {
system.out.println(file);
}
}
}