/* * File: Metrics.java * Author: David Green * Vers: 1.0.0 10/21/2010 dgg - initial coding */ import java.io.File; /** * * @author David Green */ public class Metrics { private int topLevelFolderCount; private int topLevelFileCount; private int totalFolderCount; private int totalFileCount; private File directory; private Metrics() { } public static Metrics computeDirectoryMetrics(File directory) { Metrics metrics = new Metrics(); metrics.setDirectory(directory); File aFile; File [] files = directory.listFiles(); for ( int i = 0; i < files.length; i++) { aFile = files[i]; System.out.println("Now processing " + aFile.getName()); if ( aFile.isDirectory() ) { metrics.countTopLevelFolder(); Metrics subMetrics = computeDirectoryMetrics(aFile); metrics.merge(subMetrics); } else { metrics.countTopLevelFile(); } } return metrics; } public File getDirectory() { return directory; } public void setDirectory(File directory) { this.directory = directory; } public int getTopLevelFileCount() { return topLevelFileCount; } /** * increment the top level and total file counts */ public void countTopLevelFile() { topLevelFileCount++; totalFileCount++; } public int getTopLevelFolderCount() { return topLevelFolderCount; } /** * Increment the topLevel and total folder counts */ public void countTopLevelFolder() { topLevelFolderCount++; totalFolderCount++; } public int getTotalFileCount() { return totalFileCount; } public int getTotalFolderCount() { return totalFolderCount; } /** * merge the totals from moreMetrics in with the metrics of the totals * of this metric object. * * @param moreMetrics */ public void merge(Metrics moreMetrics ) { this.totalFileCount += moreMetrics.getTotalFileCount(); this.totalFolderCount += moreMetrics.getTotalFolderCount(); } }