1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  package mk.fsgrep.find;
26  
27  
28  import java.io.*;
29  import java.util.*;
30  
31  import mk.fsgrep.Fsgrep;
32  import mk.fsgrep.util.output.OutputDestination;
33  import mk.fsgrep.util.ResultCounter;
34  import mk.fsgrep.util.TargetFile;
35  import mk.fsgrep.util.Timer;
36  
37  
38  /***
39   * The agent which collects the list of files to search.
40   * 
41   * @author  Murali Krishnan
42   *
43   */
44  public class Finder {
45  
46      
47      
48  
49  
50  
51      
52      
53  
54  
55  
56      
57      
58  
59      private Fsgrep _model = null;
60      private ScanProfile _profile = null;
61      private Collection<TargetFile> _fileList = new TreeSet<TargetFile>();
62      private LinkedList<File> _remaining = new LinkedList<File>();
63      private ResultCounter _fileCount = new ResultCounter();
64  
65  
66  
67      
68      
69  
70      public Finder(Fsgrep pModel, ScanProfile pProfile) {
71          _model = pModel;
72          _profile = pProfile;
73      }
74  
75  
76      protected Finder() {
77      }
78  
79  
80      
81      
82  
83      protected Fsgrep getModel() {return _model;}
84      protected LinkedList<File> getRemaining() {return _remaining;}
85      public Collection<TargetFile> getFileList() {return _fileList;}
86      public ScanProfile getProfile() {return _profile;}
87      public ResultCounter getFileCount() {return _fileCount;}
88  
89  
90  
91      
92      
93  
94      public void setFileCount(ResultCounter val) {_fileCount=val;}
95      public void setProfile(ScanProfile val) {_profile = val;}
96  
97  
98  
99      
100     
101 
102     public File getRoot() {return getProfile().getRoot();}
103 
104     protected FilenameFilter getFilter() {return getProfile().getFilter();}
105 
106     protected OutputDestination getReportOutput() {return getModel().getReportOutput();}
107 
108     protected OutputDestination getStatusOutput() {return getModel().getStatusOutput();}
109 
110 
111     
112     
113 
114     protected synchronized void addFiles(File[] files) {
115         if (files != null) {
116             for (File file : files) {
117                 getFileList().add(new TargetFile(file));
118             }
119 
120             getFileCount().increment(files.length);
121         }
122     }
123 
124 
125     protected synchronized void addSubDirs(File[] subdirs) {
126         if (subdirs != null) {
127             for (File subdir : subdirs) {
128                 getRemaining().addFirst(subdir);
129             }
130         }
131     }
132 
133 
134     
135     
136 
137     public String toString() {
138         
139         String spec = "";
140         if (getProfile() != null) {
141             spec = getProfile().createSpecification();
142         }
143         String result = "Finder[" + spec + "]";
144 
145         return result;
146     }
147 
148 
149     public String toProfileString() {
150         String result = "[no profile]";
151 
152         if (getProfile() != null) {
153             result = getProfile().createSpecification();
154         }
155 
156         return result;
157     }
158 
159 
160     public synchronized void reset() {
161         getFileList().clear();
162         getRemaining().clear();
163         getFileCount().reset();
164     }
165 
166 
167     public void scan() {
168         reset();
169         getStatusOutput().print("Looking for files ...");
170 
171         getRemaining().addFirst(getRoot());
172 
173         Timer timer = new Timer();
174         int numberDirsExamined = 0;
175 
176         getReportOutput().print("Scanning directories for files ...");
177 
178         while (!getModel().isAborted() && !getRemaining().isEmpty()) {
179             File dir = getRemaining().removeFirst();
180             ++numberDirsExamined;
181             DirScan scan = new DirScan(this, dir);
182             scan.run();
183         }
184 
185         getReportOutput().println(" examined " + numberDirsExamined + " directories.");
186 
187         timer.stop();
188         long total = timer.getTotal();
189         int count = getFileList().size();
190         double avg = timer.findAverage(count);
191 
192         getReportOutput().println("Scan time: " + total + " ms, " +
193                                   count + " files, " + avg + " ms per file.");
194 
195         
196         
197         if (!getRemaining().isEmpty()) {
198             reset();
199         }
200     }
201 
202 
203     
204     
205 
206 
207 
208     
209     
210 
211 
212 
213     
214     
215 
216 
217 
218 }