Mercurial > hg4j
comparison src/org/tmatesoft/hg/util/FileWalker.java @ 74:6f1b88693d48
Complete refactoring to org.tmatesoft
author | Artem Tikhomirov <tikhomirov.artem@gmail.com> |
---|---|
date | Mon, 24 Jan 2011 03:14:45 +0100 |
parents | src/com/tmate/hgkit/fs/FileWalker.java@4cfc47bc14cc |
children | a3a2e5deb320 |
comparison
equal
deleted
inserted
replaced
73:0d279bcc4442 | 74:6f1b88693d48 |
---|---|
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@svnkit.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 { | |
29 | |
30 private final File startDir; | |
31 private final LinkedList<File> dirQueue; | |
32 private final LinkedList<File> fileQueue; | |
33 private File nextFile; | |
34 private String nextPath; | |
35 | |
36 // FilenameFilter is used in a non-standard way - first argument, dir, is always startDir, | |
37 // while second arg, name, is startDir-relative path to the file in question | |
38 public FileWalker(File startDir) { | |
39 this.startDir = startDir; | |
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 = path(nextFile); | |
63 } | |
64 | |
65 public String name() { | |
66 return nextPath; | |
67 } | |
68 | |
69 public File file() { | |
70 return nextFile; | |
71 } | |
72 | |
73 private String path(File f) { | |
74 // XXX LocalHgRepo#normalize | |
75 String p = f.getPath().substring(startDir.getPath().length() + 1); | |
76 return p.replace('\\', '/').replace("//", "/"); | |
77 } | |
78 | |
79 private File[] listFiles(File f) { | |
80 // in case we need to solve os-related file issues (mac with some encodings?) | |
81 return f.listFiles(); | |
82 } | |
83 | |
84 // return true when fill added any elements to fileQueue. | |
85 private boolean fill() { | |
86 while (fileQueue.isEmpty()) { | |
87 if (dirQueue.isEmpty()) { | |
88 return false; | |
89 } | |
90 while (!dirQueue.isEmpty()) { | |
91 File dir = dirQueue.removeFirst(); | |
92 for (File f : listFiles(dir)) { | |
93 if (f.isDirectory()) { | |
94 if (!".hg".equals(f.getName())) { | |
95 dirQueue.addLast(f); | |
96 } | |
97 } else { | |
98 fileQueue.addLast(f); | |
99 } | |
100 } | |
101 break; | |
102 } | |
103 } | |
104 return !fileQueue.isEmpty(); | |
105 } | |
106 } |