-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApplication.java
More file actions
127 lines (90 loc) · 3.62 KB
/
Application.java
File metadata and controls
127 lines (90 loc) · 3.62 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import com.opencsv.CSVWriter;
import com.sun.tools.jdeprscan.CSV;
import org.joda.time.DateTime;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class Application {
private enum Algorithm {
BUBBLE, QUICK, MERGE
}
private static class InputArgumentException extends Exception {
InputArgumentException(String message) {
super(message);
}
}
public static void main(String[] args) throws IOException {
try {
String fileName = args[0];
if(fileName == null || fileName.equals("")) {
throw new InputArgumentException("Enter a .log file path");
}
if(!fileName.endsWith(".log")) {
throw new InputArgumentException("Input file is not a log file");
}
File file = new File(fileName);
executeSort(file);
}
catch(InputArgumentException e) {
e.printStackTrace();
}
}
public static void executeSort(File file) throws IOException {
String[] stripPath = file.toString().split("/");
String fileName = stripPath[stripPath.length - 1].substring(0, stripPath[stripPath.length - 1].length() - 4);
String outputFile = fileName + "_Output.csv";
CSVWriter csvWriter = new CSVWriter(
new FileWriter(outputFile),
CSVWriter.DEFAULT_SEPARATOR,
CSVWriter.NO_QUOTE_CHARACTER,
CSVWriter.DEFAULT_ESCAPE_CHARACTER,
CSVWriter.DEFAULT_LINE_END
);
String[] headerRecord = {"Algorithm Name", "File Name", "File Size", "Load Time", "Sort Time", "Load + Sort Time"};
csvWriter.writeNext(headerRecord);
Algorithm[] algorithms = new Algorithm[]{Algorithm.BUBBLE, Algorithm.MERGE, Algorithm.QUICK};
LogSort logSort = new LogSort();
LogFileReader logFileReader = new LogFileReader();
for(Algorithm algorithm : algorithms) {
// 1. Read file here and store in a list of logs
long startLoad = System.nanoTime();
List<Log> logs = logFileReader.readFile(file);
long endLoad = System.nanoTime();
long loadTime = (endLoad - startLoad) / 1000l;
// 2. For each algorithm calculate execution time
long startSort = 0l;
long endSort = 0l;
long sortTime = 0l;
if(algorithm == Algorithm.QUICK) {
startSort = System.nanoTime();
logSort.quickSort(logs);
endSort = System.nanoTime();
sortTime = (endSort - startSort) / 1000l;
}
else if (algorithm == Algorithm.MERGE ) {
startSort = System.nanoTime();
logSort.mergeSort(logs);
endSort = System.nanoTime();
sortTime = (endSort - startSort) / 1000l;
}
else {
startSort = System.nanoTime();
logSort.bubbleSort(logs);
endSort = System.nanoTime();
sortTime = (endSort - startSort) / 1000l;
}
// 3. Write data to output.csv using csvWriter object
csvWriter.writeNext(new String[]{
String.valueOf(algorithm),
fileName,
String.valueOf(logs.size()),
String.valueOf(loadTime),
String.valueOf(sortTime),
String.valueOf(loadTime + sortTime)
});
}
csvWriter.close();
}
}