Skip to content

Commit

Permalink
size complexity calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
Pasan Godamune committed Aug 16, 2019
1 parent 80db95a commit 3f6eb35
Show file tree
Hide file tree
Showing 2 changed files with 175 additions and 2 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20180813</version>
</dependency>
</dependencies>

<build>
Expand Down
172 changes: 170 additions & 2 deletions src/main/java/com/sliit/spm/complexity/SizeComplexity.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.json.JSONArray;
import org.json.JSONObject;


public class SizeComplexity {

Expand All @@ -15,6 +19,11 @@ public class SizeComplexity {
private String detectedWord;
private int count;

private JSONObject json;
private JSONObject tempJSON;
private JSONArray tokenArray;
private JSONArray tempArr;

int lineNo = 1;

public SizeComplexity(File file) {
Expand Down Expand Up @@ -44,13 +53,20 @@ public void setReadLine(String readline) {
public int calculateTotalSizeComplexity() throws FileNotFoundException {

Scanner scanner = new Scanner(this.getFile());
String fileExt = file.getName().substring(file.getName().lastIndexOf("."));

json = new JSONObject();


while (scanner.hasNext()) {

this.readLine = scanner.nextLine();
String currentLine = this.readLine.trim();
String[] skipKeys = { "/", "*" };
String fileExt = file.getName().substring(file.getName().lastIndexOf("."));

this.tempArr = new JSONArray();
this.tempJSON = new JSONObject();
this.tokenArray = new JSONArray();

this.count = 0;

Expand All @@ -72,6 +88,14 @@ public int calculateTotalSizeComplexity() throws FileNotFoundException {
this.sizeOfKeywords(currentLine);
this.sizeOfManipulators(currentLine);
this.sizeOfQuotes(currentLine);
this.sizeOfClasses(currentLine);
this.sizeOfMethods(currentLine);
this.sizeOfObjects(currentLine);
this.sizeOfVariables(currentLine);
this.sizeOfArrays(currentLine);


this.tempJSON.put("tokens", this.tempArr);

this.lineNo++;

Expand Down Expand Up @@ -148,6 +172,31 @@ public void sizeOfManipulators(String currentLine) {
public void sizeOfQuotes(String currentLine) {
this.cs += this.textInQuotes(currentLine);
}

public void sizeOfClasses(String currentLine) {
this.cs += this.countClasses(currentLine);
}

public void sizeOfMethods(String currentLine) {
this.cs += this.countMethods(currentLine);
}

public void sizeOfObjects(String currentLine) {
this.cs += this.countObjects(currentLine);
}

public void sizeOfVariables(String currentLine) {
this.cs += this.countVariables(currentLine);
}

public void sizeOfArrays(String currentLine) {
this.cs += this.countArrays(currentLine);
}

public void sizeOfNumbers(String currentLine) {
this.cs += this.countNumbers(currentLine);
}


public int countDuplicateKeywords(String currentLine, String[] keys) {

Expand All @@ -159,6 +208,7 @@ public int countDuplicateKeywords(String currentLine, String[] keys) {
for (int j = 0; j < keys.length; j++) {
if (words[i].contains(keys[j])) {
count++;
this.tempArr.put(keys[j]);
System.out.println(this.lineNo + "| " + this.readLine + "\t\t token: [" + keys[j] + "] count: " + count + "[Cs: " + this.cs + "]");
}
}
Expand All @@ -177,11 +227,13 @@ public int countDuplicateOperators(String currentLine, String[] keys) {
for (int j = 0; j < keys.length; j++) {
if (words[i].equals(keys[j])) {
count++;
this.tempArr.put(keys[j]);
System.out.println(this.lineNo + "| " + this.readLine + "\t\t token: [" + keys[j] + "] count: " + count + "[Cs: " + this.cs + "]");
} else {
if (words[i].contains(keys[j])) {
String[] tempWord = words[i].split(Pattern.quote(keys[j]));
count += (tempWord.length - 1);
this.tempArr.put(keys[j]);
System.out.println(this.lineNo + "| " + this.readLine + "\t\t token: [" + keys[j] + "] count: " + count + "[Cs: " + this.cs + "]");
}
}
Expand All @@ -203,18 +255,21 @@ public int countArithmeticOperators(String currentLine, String[] keys, String[][
if (isValidKey(regex, words[i - 1])) {
this.detectedWord = words[i];
count++;
this.tempArr.put(keys[j]);
System.out.println(this.lineNo + "| " + this.readLine + "\t\t token: [" + keys[j] + "] count: " + count + "[Cs: " + this.cs + "]");
}
else if (isValidKey(regex, words[i + 1])) {
this.detectedWord = words[i];
count++;
this.tempArr.put(keys[j]);
System.out.println(this.lineNo + "| " + this.readLine + "\t\t token: [" + keys[j] + "] count: " + count + "[Cs: " + this.cs + "]");
}
} else {
if (words[i].contains(keys[j])) {
String[] tempWord = words[i].split(Pattern.quote(keys[j]));
this.detectedWord = words[i];
count += (tempWord.length - 1);
this.tempArr.put(keys[j]);
System.out.println(this.lineNo + "| " + this.readLine + "\t\t token: [" + keys[j] + "] count: " + count + "[Cs: " + this.cs + "]");
}
}
Expand All @@ -232,12 +287,124 @@ public int textInQuotes(String currentLine) {
Matcher m = p.matcher(currentLine);
while (m.find()) {
count++;
// System.out.println("Quotes: " + m.group(1) + " " + count);
this.tempArr.put(m.group(1));
System.out.println(this.lineNo + "| " + this.readLine + "\t\t token: [" + m.group(1) +"] count: " + count + "[Cs: " + this.cs + "]");
}

return count;
}

public int countClasses(String currentLine) {

String[] words = currentLine.split(" ");

int count = 0;

for (int i = 0; i < words.length; i++) {
if ((i + 1) < words.length) {
if (words[i].equals("class")) {
count++;
this.tempArr.put(words[i + 1]);
System.out.println(this.lineNo + "| " + this.readLine + "\t\t token: [" + words[i + 1] + "] count: " + count + "[Cs: " + this.cs + "]");
}
}
}

return count;
}

public int countMethods(String currentLine) {

String[] words = currentLine.split(" ");

int count = 0;

for (int i = 0; i < words.length; i++) {
if ((i + 1) < words.length) {
if (Pattern.matches("[a-zA-Z0-9_]*\\s[a-zA-Z0-9_]*\\(", words[i] + " " + words[i + 1])) {
count++;
this.tempArr.put(words[i + 1]);
System.out.println(this.lineNo + "| " + this.readLine + "\t\t token: [" + words[i + 1] + "] count: " + count + "[Cs: " + this.cs + "]");
}
}
}

return count;
}

public int countObjects(String currentLine) {

String[] words = currentLine.split(" ");

int count = 0;

for (int i = 0; i < words.length; i++) {
if ((i + 2) < words.length) {
if (Pattern.matches("\\=\\s(new)\\s[a-zA-Z0-9_]*\\(", words[i] + " " + words[i + 1] + " " + words[i + 2])) {
count++;
this.tempArr.put(words[i + 1]);
System.out.println(this.lineNo + "| " + this.readLine + "\t\t token: [" + words[i - 1] + "] count: " + count + "[Cs: " + this.cs + "]");
}
}
}

return count;
}

public int countVariables(String currentLine) {

String[] words = currentLine.split(" ");

int count = 0;

for (int i = 0; i < words.length; i++) {
if ((i + 1) < words.length) {
if (Pattern.matches("(int|String|float|double)\\s[a-zA-Z0-9_]*", words[i] + " " + words[i + 1])) {
count++;
this.tempArr.put(words[i + 1]);
System.out.println(this.lineNo + "| " + this.readLine + "\t\t token: [" + words[i + 1] + "] count: " + count + "[Cs: " + this.cs + "]");
}
}
}

return count;
}

public int countArrays(String currentLine) {

String[] words = currentLine.split(" ");

int count = 0;

for (int i = 0; i < words.length; i++) {
if ((i + 1) < words.length) {
if (Pattern.matches("[a-zA-Z0-9_]*\\[]\\s[a-zA-Z0-9_]*", words[i] + " " + words[i + 1])) {
count++;
this.tempArr.put(words[i + 1]);
System.out.println(this.lineNo + "| " + this.readLine + "\t\t token: [" + words[i + 1] + "] count: " + count + "[Cs: " + this.cs + "]");
}
}
}

return count;
}

public int countNumbers(String currentLine) {

String[] words = currentLine.split(" ");

int count = 0;

for (int i = 0; i < words.length; i++) {
if (Pattern.matches("^[0-9]+", words[i])) {
count++;
this.tempArr.put(words[i]);
System.out.println(this.lineNo + "| " + this.readLine + "\t\t token: [" + words[i] + "] count: " + count + "[Cs: " + this.cs + "]");
}
}

return count;
}

public boolean isValidKey(String regex[][], String previousOrAfter) {

Expand All @@ -253,6 +420,7 @@ public boolean isValidKey(String regex[][], String previousOrAfter) {
}

public boolean canSkip(String currentLine, String[] skipKey) {

for (int i = 0; i < skipKey.length; i++) {
if (currentLine.startsWith(skipKey[i])) {
this.lineNo++;
Expand Down

0 comments on commit 3f6eb35

Please sign in to comment.