Advertisement
LukacikPavel

LineCounter

Nov 17th, 2018
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.56 KB | None | 0 0
  1. package sk.upjs.ics.agilnaRuka;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.FileReader;
  5. import java.io.IOException;
  6. import java.util.ArrayList;
  7. import java.util.List;
  8.  
  9. public class LineCounter {
  10.  
  11.     List<String> readFile(String filepath) {
  12.         List<String> fileLines = new ArrayList<>();
  13.         BufferedReader bufferedReader;
  14.         String currentLine;
  15.         try {
  16.             bufferedReader = new BufferedReader(new FileReader(filepath));
  17.             while ((currentLine = bufferedReader.readLine()) != null) {
  18.                 fileLines.add(currentLine);
  19.             }
  20.             bufferedReader.close();
  21.         } catch (IOException e) {
  22.             e.printStackTrace();
  23.         }
  24.  
  25.         return fileLines;
  26.     }
  27.  
  28.     List<String> trimEachLine(List<String> lines) {
  29.         List<String> linesCopy = new ArrayList<>(lines);
  30.         List<String> trimmedLines = new ArrayList<>();
  31.  
  32.         for (String line : linesCopy) {
  33.             trimmedLines.add(line.trim());
  34.         }
  35.  
  36.         return trimmedLines;
  37.     }
  38.  
  39.     List<String> removeEachComment(List<String> trimmedLines) {
  40.         List<String> removedWholeLineComments = removeWholeLineComments(trimmedLines);
  41.         return removePartialLineComments(removedWholeLineComments);
  42.     }
  43.  
  44.     boolean isWholeLineComment(String line) {
  45.         return (line != null && line.length() >= 2 && line.startsWith("//"));
  46.     }
  47.  
  48.     List<String> removeWholeLineComments(List<String> trimmedLines) {
  49.         List<String> linesCopy = new ArrayList<>(trimmedLines);
  50.  
  51.         for (String line : linesCopy) {
  52.             if (isWholeLineComment(line)) {
  53.                 linesCopy.set(linesCopy.indexOf(line), "");
  54.             }
  55.         }
  56.  
  57.         return linesCopy;
  58.     }
  59.  
  60.     List<String> removePartialLineComments(List<String> trimmedLines) {
  61.         List<String> linesCopy = new ArrayList<>(trimmedLines);
  62.         boolean isComment = false;
  63.         boolean isInString = false;
  64.  
  65.         for (String line : linesCopy) {
  66.             StringBuilder temp = new StringBuilder("");
  67.  
  68.             for (int j = 0; j < line.length(); j++) {
  69.  
  70.                 if (isComment == false) {
  71.                     isInString = isPartOfString(isInString, line, j);
  72.                 }
  73.  
  74.                 if (isInString) {
  75.                     temp.append(line.charAt(j));
  76.                 } else {
  77.  
  78.                     isComment = isPartOfComment(isComment, line, j);
  79.  
  80.                     if (isComment == false) {
  81.                         temp.append(line.charAt(j));
  82.                     }
  83.  
  84.                     isComment = isNextStillComment(isComment, line, j);
  85.  
  86.                 }
  87.             }
  88.             linesCopy.set(linesCopy.indexOf(line), temp.toString());
  89.         }
  90.  
  91.         return linesCopy;
  92.     }
  93.  
  94.     boolean isPartOfString(boolean isInString, String line, int j) {
  95.  
  96.         if (isInString == false) {
  97.             if (line.charAt(j) == '"') {
  98.                 isInString = true;
  99.             }
  100.             return isInString;
  101.         } else {
  102.             if (j - 1 >= 0 && line.charAt(j - 1) != '\\' && line.charAt(j) == '"') { // string " \" "
  103.                 isInString = false;
  104.             }
  105.             return isInString;
  106.         }
  107.     }
  108.  
  109.     private boolean isNextStillComment(boolean isComment, String line, int j) { // citatelne? pomocne premenne? vnorene
  110.                                                                                 // ify?
  111.         if (isComment == true && j - 1 >= 0 && line.charAt(j - 1) == '*' && line.charAt(j) == '/') {
  112.             isComment = false;
  113.         }
  114.  
  115.         return isComment;
  116.     }
  117.  
  118.     boolean isPartOfComment(boolean isComment, String line, int j) {
  119.         if (isComment == false && j + 1 < line.length() && line.charAt(j) == '/' && line.charAt(j + 1) == '*') {
  120.             return true;
  121.         }
  122.  
  123.         return isComment;
  124.     }
  125.  
  126.     int countLinesOfCode(String filepath) {
  127.         int numberOfLinesWithoutComment = 0;
  128.         List<String> fileLines = readFile("App.java");
  129.         List<String> trimFileLines = trimEachLine(fileLines);
  130.         List<String> withoutComments = removeEachComment(trimFileLines);
  131.  
  132.         for (String string : withoutComments) {
  133.             if (!string.isEmpty()) {
  134.                 numberOfLinesWithoutComment++;
  135.             }
  136.         }
  137.  
  138.         return numberOfLinesWithoutComment;
  139.     }
  140.  
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement