001: package com.quadcap.io;
002:
003: /* Copyright 1997 - 2003 Quadcap Software. All rights reserved.
004: *
005: * This software is distributed under the Quadcap Free Software License.
006: * This software may be used or modified for any purpose, personal or
007: * commercial. Open Source redistributions are permitted. Commercial
008: * redistribution of larger works derived from, or works which bundle
009: * this software requires a "Commercial Redistribution License"; see
010: * http://www.quadcap.com/purchase.
011: *
012: * Redistributions qualify as "Open Source" under one of the following terms:
013: *
014: * Redistributions are made at no charge beyond the reasonable cost of
015: * materials and delivery.
016: *
017: * Redistributions are accompanied by a copy of the Source Code or by an
018: * irrevocable offer to provide a copy of the Source Code for up to three
019: * years at the cost of materials and delivery. Such redistributions
020: * must allow further use, modification, and redistribution of the Source
021: * Code under substantially the same terms as this license.
022: *
023: * Redistributions of source code must retain the copyright notices as they
024: * appear in each source code file, these license terms, and the
025: * disclaimer/limitation of liability set forth as paragraph 6 below.
026: *
027: * Redistributions in binary form must reproduce this Copyright Notice,
028: * these license terms, and the disclaimer/limitation of liability set
029: * forth as paragraph 6 below, in the documentation and/or other materials
030: * provided with the distribution.
031: *
032: * The Software is provided on an "AS IS" basis. No warranty is
033: * provided that the Software is free of defects, or fit for a
034: * particular purpose.
035: *
036: * Limitation of Liability. Quadcap Software shall not be liable
037: * for any damages suffered by the Licensee or any third party resulting
038: * from use of the Software.
039: */
040:
041: import java.io.File;
042: import java.io.FileFilter;
043:
044: import java.util.Iterator;
045: import java.util.Stack;
046:
047: /**
048: *
049: *
050: * @author Stan Bailes
051: */
052: public class RecursiveFileIterator implements Iterator {
053: File root;
054: FileFilter filter;
055: Stack s = new Stack();
056: File nextFile = null;
057: boolean done = false;
058:
059: class IEntry {
060: File f;
061: File[] files;
062: int pos;
063:
064: public IEntry(File f) {
065: this .f = f;
066: this .files = f.listFiles();
067: if (files == null)
068: files = new File[0];
069: this .pos = 0;
070: }
071: }
072:
073: public RecursiveFileIterator(File root, FileFilter filter) {
074: this .root = root;
075: this .filter = filter;
076: if (root.isDirectory())
077: s.push(new IEntry(root));
078: }
079:
080: public boolean hasNext() {
081: while (nextFile == null && !done) {
082: if (s.size() == 0) {
083: done = true;
084: } else {
085: IEntry ie = (IEntry) s.peek();
086: if (ie.pos >= ie.files.length) {
087: s.pop();
088: } else {
089: File f1 = ie.files[ie.pos++];
090: if (f1.isDirectory()) {
091: s.push(new IEntry(f1));
092: } else {
093: if (filter.accept(f1))
094: nextFile = f1;
095: }
096: }
097: }
098: }
099: return !done;
100: }
101:
102: public Object next() {
103: if (nextFile == null && !done) {
104: hasNext();
105: }
106: Object ret = nextFile;
107: nextFile = null;
108: return ret;
109: }
110:
111: public void remove() {
112: throw new RuntimeException("not implemented");
113: }
114:
115: }
|