comparison hg4j/src/main/java/org/tmatesoft/hg/util/FileWalker.java @ 213:6ec4af642ba8 gradle

Project uses Gradle for build - actual changes
author Alexander Kitaev <kitaev@gmail.com>
date Tue, 10 May 2011 10:52:53 +0200
parents
children
comparison
equal deleted inserted replaced
212:edb2e2829352 213:6ec4af642ba8
1 /*
2 * Copyright (c) 2011 TMate Software Ltd
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * For information on how to redistribute this software under
14 * the terms of a license other than GNU General Public License
15 * contact TMate Software at support@hg4j.com
16 */
17 package org.tmatesoft.hg.util;
18
19 import java.io.File;
20 import java.util.LinkedList;
21 import java.util.NoSuchElementException;
22
23 /**
24 *
25 * @author Artem Tikhomirov
26 * @author TMate Software Ltd.
27 */
28 public class FileWalker implements FileIterator {
29
30 private final File startDir;
31 private final Path.Source pathHelper;
32 private final LinkedList<File> dirQueue;
33 private final LinkedList<File> fileQueue;
34 private File nextFile;
35 private Path nextPath;
36
37 public FileWalker(File dir, Path.Source pathFactory) {
38 startDir = dir;
39 pathHelper = pathFactory;
40 dirQueue = new LinkedList<File>();
41 fileQueue = new LinkedList<File>();
42 reset();
43 }
44
45 public void reset() {
46 fileQueue.clear();
47 dirQueue.clear();
48 dirQueue.add(startDir);
49 nextFile = null;
50 nextPath = null;
51 }
52
53 public boolean hasNext() {
54 return fill();
55 }
56
57 public void next() {
58 if (!fill()) {
59 throw new NoSuchElementException();
60 }
61 nextFile = fileQueue.removeFirst();
62 nextPath = pathHelper.path(nextFile.getPath());
63 }
64
65 public Path name() {
66 return nextPath;
67 }
68
69 public File file() {
70 return nextFile;
71 }
72
73 private File[] listFiles(File f) {
74 // in case we need to solve os-related file issues (mac with some encodings?)
75 return f.listFiles();
76 }
77
78 // return true when fill added any elements to fileQueue.
79 private boolean fill() {
80 while (fileQueue.isEmpty()) {
81 if (dirQueue.isEmpty()) {
82 return false;
83 }
84 while (!dirQueue.isEmpty()) {
85 File dir = dirQueue.removeFirst();
86 for (File f : listFiles(dir)) {
87 if (f.isDirectory()) {
88 if (!".hg".equals(f.getName())) {
89 dirQueue.addLast(f);
90 }
91 } else {
92 fileQueue.addLast(f);
93 }
94 }
95 break;
96 }
97 }
98 return !fileQueue.isEmpty();
99 }
100 }