Advertisement
duc-phan

Untitled

Dec 1st, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.63 KB | None | 0 0
  1. public class CodeAnalyzer implements JavaFileAnalysis {
  2.     private int lineCount;
  3.     private int maxLineWidth;
  4.     private int widestLineNumber;
  5.     private LineWidthHistogram lineWidthHistogram;
  6.     private int totalChars;
  7.     public CodeAnalyzer() {
  8.         lineWidthHistogram = new LineWidthHistogram();
  9.     }
  10.     public static List<File> findJavaFiles(File parentDirectory) {
  11.         List<File> files = new ArrayList<File>();
  12.         findJavaFiles(parentDirectory, files);
  13.         return files;
  14.     }
  15.     private static void findJavaFiles(File parentDirectory, List<File> files) {
  16.         for (File file : parentDirectory.listFiles()) {
  17.             if (file.getName().endsWith(".java"))
  18.                 files.add(file);
  19.             else if (file.isDirectory())
  20.                 findJavaFiles(file, files);
  21.         }
  22.     }
  23.     public void analyzeFile(File javaFile) throws Exception {
  24.         BufferedReader br = new BufferedReader(new FileReader(javaFile));
  25.         String line;
  26.         while ((line = br.readLine()) != null)
  27.             measureLine(line);
  28.     }
  29.     private void measureLine(String line) {
  30.         lineCount++;
  31.         int lineSize = line.length();
  32.         totalChars += lineSize;
  33.         lineWidthHistogram.addLine(lineSize, lineCount);
  34.         recordWidestLine(lineSize);
  35.     }
  36.     private void recordWidestLine(int lineSize) {
  37.         if (lineSize > maxLineWidth) {
  38.             maxLineWidth = lineSize;
  39.             widestLineNumber = lineCount;
  40.         }
  41.     }
  42.     public int getLineCount() {
  43.         return lineCount;
  44.     }
  45.     public int getMaxLineWidth() {
  46.         return maxLineWidth;
  47.     }
  48.     public int getWidestLineNumber() {
  49.         return widestLineNumber;
  50.     }
  51.     public LineWidthHistogram getLineWidthHistogram() {
  52.         return lineWidthHistogram;
  53.     }
  54.     public double getMeanLineWidth() {
  55.         return (double)totalChars/lineCount;
  56.     }
  57.     public int getMedianLineWidth() {
  58.         Integer[] sortedWidths = getSortedWidths();
  59.         int cumulativeLineCount = 0;
  60.         for (int width : sortedWidths) {
  61.             cumulativeLineCount += lineCountForWidth(width);
  62.             if (cumulativeLineCount > lineCount/2)
  63.                 return width;
  64.         }
  65.         throw new Error("Cannot get here");
  66.     }
  67.     private int lineCountForWidth(int width) {
  68.         return lineWidthHistogram.getLinesforWidth(width).size();
  69.     }
  70.     private Integer[] getSortedWidths() {
  71.         Set<Integer> widths = lineWidthHistogram.getWidths();
  72.         Integer[] sortedWidths = (widths.toArray(new Integer[0]));
  73.         Arrays.sort(sortedWidths);
  74.         return sortedWidths;
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement