-
Notifications
You must be signed in to change notification settings - Fork 0
/
timeFilter.java
92 lines (71 loc) · 1.98 KB
/
timeFilter.java
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
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
public class timeFilter {
public static void main(String[] args) {
File file = new File(args[0]);
String[] temp;
String lastTripId = "";
String lastTime = "";
ArrayList<String> trip = new ArrayList<String>();
int tripIndex = -1, timeIndex = -1;
boolean brokenTrip = false;
int lines = 0;
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
String start = br.readLine();
System.out.println(start);
temp = start.split(",");
tripIndex = getIndex("TripID", temp);
timeIndex = getIndex("unixtime", temp);
for (String line; (line = br.readLine()) != null;) {
temp = line.split(",");
if (lastTripId.equals(temp[tripIndex])) {
if (!brokenTrip && checkTime(timeIndex, lastTime, temp)) {
trip.add(line);
}
else {
trip = new ArrayList<String>();
brokenTrip = true;
}
}
else {
for (String s : trip) {
System.out.println(s);
}
brokenTrip = false;
}
lastTripId = temp[tripIndex];
lastTime = temp[timeIndex];
}
// line is not visible here.
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for (String s: trip) {
System.out.println(s);
}
}
public static boolean checkTime(int timeIndex, String lastTime, String[] temp) {
long lTime = Long.parseLong(lastTime);
long nTime = Long.parseLong(temp[timeIndex]);
if ((long) (nTime - lTime) > 6000) {
return false;
}
return true;
}
private static int getIndex(String header, String[] arr) {
for (int i = 0; i < arr.length; i++) {
if (arr[i].equals(header)) {
return i;
}
}
return -1;
}
}