-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssignment1.java
More file actions
100 lines (94 loc) · 4.41 KB
/
Copy pathAssignment1.java
File metadata and controls
100 lines (94 loc) · 4.41 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
/**
* @author REYYAN OFLAZ
* measured times for different data set is:
*
*/
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.IllegalFormatConversionException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Assignment1 {
protected static final String csvSplitBy = ",";
private static int sortingKeyIdx;
private static boolean saveFile = false;
private static int k;
public static void main(String[] args) throws IOException,ParseException,ClassCastException{
sortingKeyIdx=Integer.parseInt(args[1]);
if(args[2].toLowerCase().contains("t")) saveFile=true;
String absolutePath = Assignment1.class.getProtectionDomain().getCodeSource().getLocation().getFile();
absolutePath = absolutePath.substring(0, absolutePath.lastIndexOf("/"))+args[0].substring(args[0].indexOf("/"), args[0].length());
FileManager.buffReadManager=FileManager.openFile(absolutePath);
SimpleDateFormat dateFormat=new SimpleDateFormat("dd/MM/yyyy hh:mm:ss");
ArrayList<Double> featureDouble=new ArrayList<Double>();
ArrayList<Date> featureDate=new ArrayList<Date>();
ArrayList<String> featureString=new ArrayList<String>();
HashMap<Integer,ArrayList<String>> trafficFlowData=new HashMap<Integer,ArrayList<String>>();
String line;
StopWatch timer1=new StopWatch();
String flowLine=FileManager.buffReadManager.readLine();
String flowKey=flowLine.split(csvSplitBy)[sortingKeyIdx];
boolean timeStampFlag = false;
if(flowKey.toLowerCase().contains("Timestamp".toLowerCase())) timeStampFlag=true;
while((line = FileManager.buffReadManager.readLine()) != null) {
if(timeStampFlag==true) processLine(line,dateFormat.parse(line.split(csvSplitBy)[sortingKeyIdx]),featureDate,trafficFlowData);
else processLine(line,Double.parseDouble(line.split(csvSplitBy)[sortingKeyIdx]),featureDouble,trafficFlowData);
}
/*SELECTION SORT ALGORITHM
* if(timeStampFlag==true) new SelectionSort<Date>().selectionSort(featureDate, trafficFlowData);
* else new SelectionSort<Double>().selectionSort(featureDouble, trafficFlowData);
*/
if(timeStampFlag==true) new QuickSort<Date>().sort(featureDate, trafficFlowData);
else new QuickSort<Double>().sort(featureDouble, trafficFlowData);
/* if(timeStampFlag==true) new HeapSort<Date>().sort(featureDate,trafficFlowData);
* else new HeapSort<Double>().sort(featureDouble,trafficFlowData);
*/
/*RADIX SORT ALGORITHM
* featureString=(timeStampFlag==true) ? createArrayList(featureDate) : createArrayList(featureDouble);
* RadixSort.sort(featureString);
*/
double time1 = timer1.elapsedTime();
System.out.println("measured time is"+time1);
FileManager.closeReaderFile(FileManager.buffReadManager);
Iterator it = trafficFlowData.entrySet().iterator();
while (it.hasNext()) {
Map.Entry me = (Map.Entry)it.next();
System.out.println("Key is after: "+me.getKey() + " & after value is: "+me.getValue());
}
if(saveFile==true) {
FileManager.buffWriteManager=FileManager.writeFile(absolutePath);
for (Entry<Integer, ArrayList<String>> entry : trafficFlowData.entrySet()) {
FileManager.buffWriteManager.write(entry.getValue().toString());
FileManager.buffWriteManager.newLine();
}
}
}
/*@SuppressWarnings("unused") //Implemented for Radix Sort
private static <T extends Comparable<T>> ArrayList<String> createArrayList(ArrayList<T> arrList)
throws NumberFormatException,IllegalFormatConversionException{
ArrayList<String> featureString=new ArrayList<String>();
String currValue;
for(T s : arrList) {
currValue = String.format("%.0f",(double)s*Math.pow(10, 8));
System.out.println(currValue);
featureString.add(currValue);
}
return featureString;
}
*/
private static <T extends Comparable<T>>void processLine(String lineHandler,T keyData,ArrayList<T> featureDataHandler,HashMap<Integer,ArrayList<String>> trafficFlowDataHandler)
throws NumberFormatException,IllegalFormatConversionException{
String[] line=lineHandler.split(csvSplitBy);
featureDataHandler.add(keyData);
trafficFlowDataHandler.put(k, new ArrayList<>(Stream.of(line).collect(Collectors.toList())));
k++;
}
}