comparison src/com/tmate/hgkit/fs/FileWalker.java @ 58:4cfc47bc14cc

Status against local working dir extracted into distinct class. Iterating over local files extracted for ease of os-dependant patching
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Mon, 17 Jan 2011 23:01:19 +0100
parents
children
comparison
equal deleted inserted replaced
57:8b0d6f1bd6b4 58:4cfc47bc14cc
1 /*
2 * Copyright (c) 2011 Artem Tikhomirov
3 */
4 package com.tmate.hgkit.fs;
5
6 import java.io.File;
7 import java.util.LinkedList;
8 import java.util.NoSuchElementException;
9
10 /**
11 *
12 * @author artem
13 */
14 public class FileWalker {
15
16 private final File startDir;
17 private final LinkedList<File> dirQueue;
18 private final LinkedList<File> fileQueue;
19 private File nextFile;
20 private String nextPath;
21
22 // FilenameFilter is used in a non-standard way - first argument, dir, is always startDir,
23 // while second arg, name, is startDir-relative path to the file in question
24 public FileWalker(File startDir) {
25 this.startDir = startDir;
26 dirQueue = new LinkedList<File>();
27 fileQueue = new LinkedList<File>();
28 reset();
29 }
30
31 public void reset() {
32 fileQueue.clear();
33 dirQueue.clear();
34 dirQueue.add(startDir);
35 nextFile = null;
36 nextPath = null;
37 }
38
39 public boolean hasNext() {
40 return fill();
41 }
42
43 public void next() {
44 if (!fill()) {
45 throw new NoSuchElementException();
46 }
47 nextFile = fileQueue.removeFirst();
48 nextPath = path(nextFile);
49 }
50
51 public String name() {
52 return nextPath;
53 }
54
55 public File file() {
56 return nextFile;
57 }
58
59 private String path(File f) {
60 // XXX LocalHgRepo#normalize
61 String p = f.getPath().substring(startDir.getPath().length() + 1);
62 return p.replace('\\', '/').replace("//", "/");
63 }
64
65 private File[] listFiles(File f) {
66 // in case we need to solve os-related file issues (mac with some encodings?)
67 return f.listFiles();
68 }
69
70 // return true when fill added any elements to fileQueue.
71 private boolean fill() {
72 while (fileQueue.isEmpty()) {
73 if (dirQueue.isEmpty()) {
74 return false;
75 }
76 while (!dirQueue.isEmpty()) {
77 File dir = dirQueue.removeFirst();
78 for (File f : listFiles(dir)) {
79 if (f.isDirectory()) {
80 if (!".hg".equals(f.getName())) {
81 dirQueue.addLast(f);
82 }
83 } else {
84 fileQueue.addLast(f);
85 }
86 }
87 break;
88 }
89 }
90 return !fileQueue.isEmpty();
91 }
92 }