-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRedundancyManager.java
More file actions
105 lines (98 loc) · 3.19 KB
/
RedundancyManager.java
File metadata and controls
105 lines (98 loc) · 3.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import java.io.*;
import java.util.*;
public class RedundancyManager {
private Map<Integer, Set<Integer>> lineMap;
public RedundancyManager() {
lineMap = new TreeMap<Integer, Set<Integer>>();
}
// compares if 2 lines are similar and checks if a line is longer than 100 characters
public void compare(File inputFile, String outputFileName) throws FileNotFoundException {
PrintStream output = new PrintStream(new File(outputFileName));
Scanner comparingLine = new Scanner(inputFile);
int mainLineNumber = 0;
int aheadLineNumber = 0;
int great = 0;
double same = 0;
double total = 0;
String aheadLines = "";
while (comparingLine.hasNextLine()) {
String lineToCompare = comparingLine.nextLine();
Scanner otherLines = new Scanner(inputFile);
mainLineNumber++;
aheadLineNumber = 0;
boolean longLine = false;
if (lineToCompare.length() >= 100) {
longLine = true;
}
if (lineToCompare.contains("//")) {
continue;
}
lineToCompare = lineToCompare.replace(" ", "");
int comparingLineLength = lineToCompare.length();
while (otherLines.hasNextLine()) {
aheadLines = otherLines.nextLine();
aheadLineNumber++;
aheadLines = aheadLines.replace(" ", "");
int aheadLinesLength = aheadLines.length();
if (aheadLinesLength > comparingLineLength) {
great = comparingLineLength;
} else {
great = aheadLinesLength;
}
for (int i = 0; i < great; i++) {
if (aheadLines.charAt(i) != '{' && aheadLines.charAt(i) != '}') {
if (aheadLines.charAt(i) == lineToCompare.charAt(i)) {
same++;
}
total++;
}
}
output(
same,
total,
mainLineNumber,
aheadLineNumber,
aheadLines,
lineToCompare,
output,
longLine);
total = 0;
same = 0;
}
if (longLine) {
output.println(">> Line number " + mainLineNumber + " has more than 100 characters");
output.println();
}
}
String stringy = Arrays.toString(lineMap.entrySet().toArray());
System.out.println(stringy);
for (int i = 1; i < stringy.length() - 1; i++) {
if (stringy.charAt(i) == ']') {
output.println();
i += 2;
} else if (stringy.charAt(i) != '[') {
output.print(stringy.charAt(i));
}
}
}
public void output(
double same,
double total,
int mainLineNumber,
int aheadLineNumber,
String aheadLines,
String lineToCompare,
PrintStream output,
boolean longLine) {
double percentage = (same / total) * 100;
if (percentage >= 98 && mainLineNumber < aheadLineNumber && same != 0) {
if (lineMap.containsKey(mainLineNumber)) {
lineMap.get(mainLineNumber).add(aheadLineNumber);
} else {
Set<Integer> aheadLineSet = new TreeSet<Integer>();
aheadLineSet.add(aheadLineNumber);
lineMap.put(mainLineNumber, aheadLineSet);
}
}
}
}