
net.anotheria.util.tools.Walker Maven / Gradle / Ivy
package net.anotheria.util.tools;
import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
/**
* Walker class.
*
* @author another
* @version $Id: $Id
*/
public class Walker {
private Worker worker;
private int touched;
private int dirs;
private int files;
/**
* Constructor for Walker.
*/
public Walker(){
touched = dirs = files = 0;
}
/**
* Constructor for Walker.
*
* @param aWorker a {@link net.anotheria.util.tools.Worker} object.
*/
public Walker(Worker aWorker){
this();
worker = aWorker;
}
/**
* Setter for the field worker
.
*
* @param aWorker a {@link net.anotheria.util.tools.Worker} object.
*/
public void setWorker(Worker aWorker){
worker = aWorker;
}
/**
* start.
*
* @param directories a {@link java.lang.Iterable} object.
*/
public void start(Iterable directories){
if (worker==null)
throw new IllegalStateException("No worker configured!");
for (String directory : directories)
process(new File(directory));
System.out.println("Scanned file "+touched+", directories: "+dirs+", files: "+files);
}
/**
* start.
*/
public void start(){
Collection toDo = new ArrayList<>(1);
toDo.add(".");
start(toDo);
}
private void process(File f){
touched++;
if (touched/10000*10000==touched)
System.out.println("Scanning file "+touched+", directories: "+dirs+", files: "+files);
if (f.isDirectory())
processDir(f);
else
processFile(f);
}
private void processFile(File f){
files++;
worker.processFile(f);
}
private void processDir(File f){
dirs++;
File[] ff = f.listFiles();
for (File file : ff)
process(file);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy