Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package sk.upjs.ics.agilnaRuka;
- import java.io.BufferedReader;
- import java.io.FileReader;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.List;
- public class LineCounter {
- List<String> readFile(String filepath) {
- List<String> fileLines = new ArrayList<>();
- BufferedReader bufferedReader;
- String currentLine;
- try {
- bufferedReader = new BufferedReader(new FileReader(filepath));
- while ((currentLine = bufferedReader.readLine()) != null) {
- fileLines.add(currentLine);
- }
- bufferedReader.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- return fileLines;
- }
- List<String> trimEachLine(List<String> lines) {
- List<String> linesCopy = new ArrayList<>(lines);
- List<String> trimmedLines = new ArrayList<>();
- for (String line : linesCopy) {
- trimmedLines.add(line.trim());
- }
- return trimmedLines;
- }
- List<String> removeEachComment(List<String> trimmedLines) {
- List<String> removedWholeLineComments = removeWholeLineComments(trimmedLines);
- return removePartialLineComments(removedWholeLineComments);
- }
- boolean isWholeLineComment(String line) {
- return (line != null && line.length() >= 2 && line.startsWith("//"));
- }
- List<String> removeWholeLineComments(List<String> trimmedLines) {
- List<String> linesCopy = new ArrayList<>(trimmedLines);
- for (String line : linesCopy) {
- if (isWholeLineComment(line)) {
- linesCopy.set(linesCopy.indexOf(line), "");
- }
- }
- return linesCopy;
- }
- List<String> removePartialLineComments(List<String> trimmedLines) {
- List<String> linesCopy = new ArrayList<>(trimmedLines);
- boolean isComment = false;
- boolean isInString = false;
- for (String line : linesCopy) {
- StringBuilder temp = new StringBuilder("");
- for (int j = 0; j < line.length(); j++) {
- if (isComment == false) {
- isInString = isPartOfString(isInString, line, j);
- }
- if (isInString) {
- temp.append(line.charAt(j));
- } else {
- isComment = isPartOfComment(isComment, line, j);
- if (isComment == false) {
- temp.append(line.charAt(j));
- }
- isComment = isNextStillComment(isComment, line, j);
- }
- }
- linesCopy.set(linesCopy.indexOf(line), temp.toString());
- }
- return linesCopy;
- }
- boolean isPartOfString(boolean isInString, String line, int j) {
- if (isInString == false) {
- if (line.charAt(j) == '"') {
- isInString = true;
- }
- return isInString;
- } else {
- if (j - 1 >= 0 && line.charAt(j - 1) != '\\' && line.charAt(j) == '"') { // string " \" "
- isInString = false;
- }
- return isInString;
- }
- }
- private boolean isNextStillComment(boolean isComment, String line, int j) { // citatelne? pomocne premenne? vnorene
- // ify?
- if (isComment == true && j - 1 >= 0 && line.charAt(j - 1) == '*' && line.charAt(j) == '/') {
- isComment = false;
- }
- return isComment;
- }
- boolean isPartOfComment(boolean isComment, String line, int j) {
- if (isComment == false && j + 1 < line.length() && line.charAt(j) == '/' && line.charAt(j + 1) == '*') {
- return true;
- }
- return isComment;
- }
- int countLinesOfCode(String filepath) {
- int numberOfLinesWithoutComment = 0;
- List<String> fileLines = readFile("App.java");
- List<String> trimFileLines = trimEachLine(fileLines);
- List<String> withoutComments = removeEachComment(trimFileLines);
- for (String string : withoutComments) {
- if (!string.isEmpty()) {
- numberOfLinesWithoutComment++;
- }
- }
- return numberOfLinesWithoutComment;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement