kitaev@213: /* kitaev@213: * Copyright (c) 2011 TMate Software Ltd kitaev@213: * kitaev@213: * This program is free software; you can redistribute it and/or modify kitaev@213: * it under the terms of the GNU General Public License as published by kitaev@213: * the Free Software Foundation; version 2 of the License. kitaev@213: * kitaev@213: * This program is distributed in the hope that it will be useful, kitaev@213: * but WITHOUT ANY WARRANTY; without even the implied warranty of kitaev@213: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the kitaev@213: * GNU General Public License for more details. kitaev@213: * kitaev@213: * For information on how to redistribute this software under kitaev@213: * the terms of a license other than GNU General Public License kitaev@213: * contact TMate Software at support@hg4j.com kitaev@213: */ kitaev@213: package org.tmatesoft.hg.util; kitaev@213: kitaev@213: import java.io.File; kitaev@213: import java.util.LinkedList; kitaev@213: import java.util.NoSuchElementException; kitaev@213: kitaev@213: /** kitaev@213: * kitaev@213: * @author Artem Tikhomirov kitaev@213: * @author TMate Software Ltd. kitaev@213: */ kitaev@213: public class FileWalker implements FileIterator { kitaev@213: kitaev@213: private final File startDir; kitaev@213: private final Path.Source pathHelper; kitaev@213: private final LinkedList dirQueue; kitaev@213: private final LinkedList fileQueue; kitaev@213: private File nextFile; kitaev@213: private Path nextPath; kitaev@213: kitaev@213: public FileWalker(File dir, Path.Source pathFactory) { kitaev@213: startDir = dir; kitaev@213: pathHelper = pathFactory; kitaev@213: dirQueue = new LinkedList(); kitaev@213: fileQueue = new LinkedList(); kitaev@213: reset(); kitaev@213: } kitaev@213: kitaev@213: public void reset() { kitaev@213: fileQueue.clear(); kitaev@213: dirQueue.clear(); kitaev@213: dirQueue.add(startDir); kitaev@213: nextFile = null; kitaev@213: nextPath = null; kitaev@213: } kitaev@213: kitaev@213: public boolean hasNext() { kitaev@213: return fill(); kitaev@213: } kitaev@213: kitaev@213: public void next() { kitaev@213: if (!fill()) { kitaev@213: throw new NoSuchElementException(); kitaev@213: } kitaev@213: nextFile = fileQueue.removeFirst(); kitaev@213: nextPath = pathHelper.path(nextFile.getPath()); kitaev@213: } kitaev@213: kitaev@213: public Path name() { kitaev@213: return nextPath; kitaev@213: } kitaev@213: kitaev@213: public File file() { kitaev@213: return nextFile; kitaev@213: } kitaev@213: kitaev@213: private File[] listFiles(File f) { kitaev@213: // in case we need to solve os-related file issues (mac with some encodings?) kitaev@213: return f.listFiles(); kitaev@213: } kitaev@213: kitaev@213: // return true when fill added any elements to fileQueue. kitaev@213: private boolean fill() { kitaev@213: while (fileQueue.isEmpty()) { kitaev@213: if (dirQueue.isEmpty()) { kitaev@213: return false; kitaev@213: } kitaev@213: while (!dirQueue.isEmpty()) { kitaev@213: File dir = dirQueue.removeFirst(); kitaev@213: for (File f : listFiles(dir)) { kitaev@213: if (f.isDirectory()) { kitaev@213: if (!".hg".equals(f.getName())) { kitaev@213: dirQueue.addLast(f); kitaev@213: } kitaev@213: } else { kitaev@213: fileQueue.addLast(f); kitaev@213: } kitaev@213: } kitaev@213: break; kitaev@213: } kitaev@213: } kitaev@213: return !fileQueue.isEmpty(); kitaev@213: } kitaev@213: }